Skip to content

Commit

Permalink
Moved CLI part into seperate script. Updated setup.py accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
venthur committed Oct 7, 2017
1 parent 35aea27 commit 8b7b354
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 59 deletions.
67 changes: 67 additions & 0 deletions bin/gscholar
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python

import optparse
import logging
import sys
import os

import gscholar as gs


logger = logging.getLogger(__name__)

if __name__ == "__main__":
usage = 'Usage: %prog [options] {pdf | "search terms"}'
parser = optparse.OptionParser(usage)
parser.add_option("-a", "--all", action="store_true", dest="all",
default=False, help="show all bibtex results")
parser.add_option("-d", "--debug", action="store_true", dest="debug",
default=False, help="show debugging output")
parser.add_option("-r", "--rename", action="store_true", dest="rename",
default=False, help="rename file (asks before doing it)")
parser.add_option("-f", "--outputformat", dest='output',
default="bibtex",
help="Output format. Available formats are: bibtex, endnote, refman, wenxianwang [default: %default]")
parser.add_option("-s", "--startpage", dest='startpage',
help="Page number to start parsing PDF file at.")
(options, args) = parser.parse_args()
if options.debug is True:
logging.basicConfig(level=logging.DEBUG)
if options.output == 'bibtex':
outformat = gs.FORMAT_BIBTEX
elif options.output == 'endnote':
outformat = gs.FORMAT_ENDNOTE
elif options.output == 'refman':
outformat = gs.FORMAT_REFMAN
elif options.output == 'wenxianwang':
outformat = gs.FORMAT_WENXIANWANG
if len(args) != 1:
parser.error("No argument given, nothing to do.")
sys.exit(1)
args = args[0]
pdfmode = False
if os.path.exists(args):
logger.debug("File exist, assuming you want me to lookup the pdf: {filename}.".format(filename=args))
pdfmode = True
biblist = gs.pdflookup(args, all, outformat, options.startpage)
else:
logger.debug("Assuming you want me to lookup the query: {query}".format(query=args))
biblist = gs.query(args, outformat, options.all)
if len(biblist) < 1:
print("No results found, try again with a different query!")
sys.exit(1)
if options.all is True:
logger.debug("All results:")
for i in biblist:
print(i)
else:
logger.debug("First result:")
print(biblist[0])
if options.rename is True:
if not pdfmode:
print("You asked me to rename the pdf but didn't tell me which file to rename, aborting.")
sys.exit(1)
else:
gs.rename_file(args, biblist[0])

# vim: set filetype=python
2 changes: 2 additions & 0 deletions gscholar/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from gscholar.gscholar import *

__VERSION__ = '1.3.1'
62 changes: 4 additions & 58 deletions gscholar/gscholar.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
from html.entities import name2codepoint

import re
import sys
import os
import subprocess
import optparse
import logging


Expand All @@ -39,6 +37,9 @@
FORMAT_WENXIANWANG = 5


logger = logging.getLogger(__name__)


def query(searchstr, outformat=FORMAT_BIBTEX, allresults=False):
"""Query google scholar.
Expand All @@ -59,7 +60,7 @@ def query(searchstr, outformat=FORMAT_BIBTEX, allresults=False):
the list with citations
"""
logging.debug("Query: {sstring}".format(sstring=searchstr))
logger.debug("Query: {sstring}".format(sstring=searchstr))
searchstr = '/scholar?q='+quote(searchstr)
url = GOOGLE_SCHOLAR_URL + searchstr
header = HEADERS
Expand Down Expand Up @@ -224,58 +225,3 @@ def rename_file(pdf, bibitem):
os.rename(pdf, newfile)
else:
print("Aborting.")


if __name__ == "__main__":
usage = 'Usage: %prog [options] {pdf | "search terms"}'
parser = optparse.OptionParser(usage)
parser.add_option("-a", "--all", action="store_true", dest="all",
default=False, help="show all bibtex results")
parser.add_option("-d", "--debug", action="store_true", dest="debug",
default=False, help="show debugging output")
parser.add_option("-r", "--rename", action="store_true", dest="rename",
default=False, help="rename file (asks before doing it)")
parser.add_option("-f", "--outputformat", dest='output',
default="bibtex",
help="Output format. Available formats are: bibtex, endnote, refman, wenxianwang [default: %default]")
parser.add_option("-s", "--startpage", dest='startpage',
help="Page number to start parsing PDF file at.")
(options, args) = parser.parse_args()
if options.debug is True:
logging.basicConfig(level=logging.DEBUG)
if options.output == 'bibtex':
outformat = FORMAT_BIBTEX
elif options.output == 'endnote':
outformat = FORMAT_ENDNOTE
elif options.output == 'refman':
outformat = FORMAT_REFMAN
elif options.output == 'wenxianwang':
outformat = FORMAT_WENXIANWANG
if len(args) != 1:
parser.error("No argument given, nothing to do.")
sys.exit(1)
args = args[0]
pdfmode = False
if os.path.exists(args):
logging.debug("File exist, assuming you want me to lookup the pdf: {filename}.".format(filename=args))
pdfmode = True
biblist = pdflookup(args, all, outformat, options.startpage)
else:
logging.debug("Assuming you want me to lookup the query: {query}".format(query=args))
biblist = query(args, outformat, options.all)
if len(biblist) < 1:
print("No results found, try again with a different query!")
sys.exit(1)
if options.all is True:
logging.debug("All results:")
for i in biblist:
print(i)
else:
logging.debug("First result:")
print(biblist[0])
if options.rename is True:
if not pdfmode:
print("You asked me to rename the pdf but didn't tell me which file to rename, aborting.")
sys.exit(1)
else:
rename_file(args, biblist[0])
11 changes: 10 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
#!/usr/bin/env python


from distutils.core import setup
from setuptools import setup

import gscholar

setup(name='gscholar',
version=gscholar.__VERSION__,
description='Python library to query Google Scholar.',
long_description='This package provides a python package and CLI to query google scholar and get references in various formats (e.g. bibtex, endnote, etc.)',
classifiers=[
'Development Status :: 5 :: Production/Stable',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6',
],
keywords='google scholar cli',
author='Bastian Venthur',
author_email='[email protected]',
url='http://github.com/venthur/gscholar',
packages=['gscholar'],
scripts=['bin/gscholar'],
license='MIT',
)

0 comments on commit 8b7b354

Please sign in to comment.