updated add cmds with isbn support, improved dialog, replaced -c and -C by single --link option

main
Fabien Benureau 10 years ago
parent 3989a13359
commit 526dc6418f

@ -1,6 +1,7 @@
"""Interface for Remote Bibliographic APIs""" """Interface for Remote Bibliographic APIs"""
import requests import requests
from bs4 import BeautifulSoup
def doi2bibtex(doi): def doi2bibtex(doi):
"""Return a bibtex string of metadata from a DOI""" """Return a bibtex string of metadata from a DOI"""
@ -10,3 +11,14 @@ def doi2bibtex(doi):
r = requests.get(url, headers=headers) r = requests.get(url, headers=headers)
return r.text return r.text
def isbn2bibtex(isbn):
"""Return a bibtex string of metadata from a DOI"""
url = 'http://www.ottobib.com/isbn/{}/bibtex'.format(isbn)
r = requests.get(url)
soup = BeautifulSoup(r.text)
citation = soup.find("textarea").text
return citation

@ -15,15 +15,14 @@ def parser(subparsers):
parser.add_argument('bibfile', nargs='?', default = None, parser.add_argument('bibfile', nargs='?', default = None,
help='bibtex 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', '--doi', help='doi number to retrieve the bibtex entry, if it is not provided', default=None)
parser.add_argument('-I', '--isbn', help='isbn 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('-d', '--docfile', help='pdf or ps file', default=None)
parser.add_argument('-t', '--tags', help='tags associated to the paper, separated by commas', parser.add_argument('-t', '--tags', help='tags associated to the paper, separated by commas',
default=None) default=None)
parser.add_argument('-k', '--citekey', help='citekey associated with the paper;\nif not provided, one will be generated automatically.', parser.add_argument('-k', '--citekey', help='citekey associated with the paper;\nif not provided, one will be generated automatically.',
default=None) default=None)
parser.add_argument('-c', '--copy', action='store_true', default=True, parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True,
help="copy document files into library directory (default)") help="don't copy document files, just create a link.")
parser.add_argument('-C', '--nocopy', action='store_false', dest='copy',
help="don't copy document files (opposite of -c)")
return parser return parser
@ -71,13 +70,21 @@ def command(args):
# get bibtex entry # get bibtex entry
if bibfile is None: if bibfile is None:
if args.doi is None: if args.doi is None and args.isbn is None:
bibdata = bibdata_from_editor(ui, rp) bibdata = bibdata_from_editor(ui, rp)
else: else:
if args.doi is not None:
bibdata_raw = apis.doi2bibtex(args.doi) bibdata_raw = apis.doi2bibtex(args.doi)
bibdata = rp.databroker.verify(bibdata_raw) bibdata = rp.databroker.verify(bibdata_raw)
if bibdata is None: if bibdata is None:
ui.error('invalid doi {} or unable to retrieve bibfile.'.format(args.doi)) ui.error('invalid doi {} or unable to retrieve bibfile from it.'.format(args.doi))
if args.isbn is None:
ui.exit(1)
if args.isbn is not None:
bibdata_raw = apis.isbn2bibtex(args.isbn)
bibdata = rp.databroker.verify(bibdata_raw)
if bibdata is None:
ui.error('invalid isbn {} or unable to retrieve bibfile from it.'.format(args.isbn))
ui.exit(1) ui.exit(1)
# TODO distinguish between cases, offer to open the error page in a webbrowser. # TODO distinguish between cases, offer to open the error page in a webbrowser.
# TODO offer to confirm/change citekey # TODO offer to confirm/change citekey

@ -16,10 +16,8 @@ def parser(subparsers):
help='import paper(s) to the repository') help='import paper(s) to the repository')
parser.add_argument('bibpath', parser.add_argument('bibpath',
help='path to bibtex, bibtexml or bibyaml file (or directory)') help='path to bibtex, bibtexml or bibyaml file (or directory)')
parser.add_argument('-c', '--copy', action='store_true', default=None, parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True,
help="copy document files into library directory (default)") help="don't copy document files, just create a link.")
parser.add_argument('-C', '--nocopy', action='store_false', dest='copy',
help="don't copy document files (opposite of -c)")
parser.add_argument('keys', nargs='*', parser.add_argument('keys', nargs='*',
help="one or several keys to import from the file") help="one or several keys to import from the file")
return parser return parser

@ -38,6 +38,9 @@ class Paper(object):
if self.citekey is None: if self.citekey is None:
self.citekey = bibstruct.extract_citekey(self.bibdata) self.citekey = bibstruct.extract_citekey(self.bibdata)
bibstruct.check_citekey(self.citekey) bibstruct.check_citekey(self.citekey)
else:
def_citekey = bibstruct.extract_citekey(self.bibdata)
self.bibdata = {citekey: self.bibdata[def_citekey]}
def __eq__(self, other): def __eq__(self, other):
return (isinstance(self, Paper) and type(other) is type(self) return (isinstance(self, Paper) and type(other) is type(self)

@ -1,6 +1,7 @@
# Function here may belong somewhere else. In the mean time... # Function here may belong somewhere else. In the mean time...
from . import color from . import color
from . import pretty
def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True): def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True):
"""Check that a citekey exists, or autocompletes it if not ambiguous.""" """Check that a citekey exists, or autocompletes it if not ambiguous."""
@ -18,8 +19,12 @@ def resolve_citekey(repo, citekey, ui=None, exit_on_fail=True):
citekey = citekeys[0] citekey = citekeys[0]
elif citekey not in citekeys: elif citekey not in citekeys:
if ui is not None: if ui is not None:
ui.error("be more specific; provided citekey '{}' matches multiples citekeys: {}".format( citekeys = sorted(citekeys)
citekey, ', '.join(color.dye(citekey, color.citekey) for citekey in citekeys))) ui.error("be more specific; provided citekey '{}' matches multiples citekeys:".format(
citekey))
for c in citekeys:
p = repo.pull_paper(c)
ui.print_(u' {}'.format(pretty.paper_oneliner(p)))
if exit_on_fail: if exit_on_fail:
ui.exit() ui.exit()
return citekey return citekey

Loading…
Cancel
Save