From adb0158c3d2171b9619dbf565685f22dce39e22e Mon Sep 17 00:00:00 2001 From: Fabien Benureau Date: Thu, 8 Jan 2015 14:58:41 +0100 Subject: [PATCH] experimental support for adding from DOIs --- pubs/apis.py | 12 ++++++++++++ pubs/commands/add_cmd.py | 17 +++++++++++++---- setup.py | 3 ++- 3 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 pubs/apis.py diff --git a/pubs/apis.py b/pubs/apis.py new file mode 100644 index 0000000..5fe06ce --- /dev/null +++ b/pubs/apis.py @@ -0,0 +1,12 @@ +"""Interface for Remote Bibliographic APIs""" + +import requests + +def doi2bibtex(doi): + """Return a bibtex string of metadata from a DOI""" + + url = 'http://dx.doi.org/{}'.format(doi) + headers = {'accept': 'application/x-bibtex'} + r = requests.get(url, headers=headers) + + return r.text diff --git a/pubs/commands/add_cmd.py b/pubs/commands/add_cmd.py index 1dc5907..a8a5e06 100644 --- a/pubs/commands/add_cmd.py +++ b/pubs/commands/add_cmd.py @@ -5,12 +5,14 @@ from .. import content from .. import repo from .. import paper from .. import templates +from .. import apis def parser(subparsers): parser = subparsers.add_parser('add', help='add a paper to the repository') parser.add_argument('bibfile', nargs='?', default = None, - help='bibtex, bibtexml or bibyaml file') + help='bibtex file') + parser.add_argument('-D', '--doi', help='doi number to retrieve the bibtex entry, if it is not provided', default=None) parser.add_argument('-d', '--docfile', help='pdf or ps file', default=None) parser.add_argument('-t', '--tags', help='tags associated to the paper, separated by commas', default=None) @@ -65,10 +67,17 @@ def command(args): rp = repo.Repository(config()) - # get bibfile - + # get bibtex entry if bibfile is None: - bibdata = bibdata_from_editor(ui, rp) + if args.doi is None: + bibdata = bibdata_from_editor(ui, rp) + else: + bibdata_raw = apis.doi2bibtex(args.doi) + bibdata = rp.databroker.verify(bibdata_raw) + if bibdata is None: + ui.error('invalid doi {} or unable to retreive bibfile.'.format(doi)) + # TODO distinguish between cases, offer to open the error page in a webbrowser. + # TODO offer to confirm/change citekey else: bibdata_raw = content.get_content(bibfile, ui=ui) bibdata = rp.databroker.verify(bibdata_raw) diff --git a/setup.py b/setup.py index 8811800..4425f32 100644 --- a/setup.py +++ b/setup.py @@ -12,10 +12,11 @@ setup( url = '', description = 'command-line scientific bibliography manager', - requires = ['pyyaml', 'bibtexparser', 'dateutil'], packages = find_packages(), scripts = ['pubs/pubs'], + install_requires = ['pyyaml', 'bibtexparser', 'python-dateutil', 'requests'], + classifiers=[ 'Development Status :: 4 - Beta', 'License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)',