mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Recognize http URLs in identifiers with arbitrary keys
This commit is contained in:
parent
496b0ce0f9
commit
edfbb3c7bc
@ -533,6 +533,12 @@ def identify(log, abort, # {{{
|
|||||||
def urls_from_identifiers(identifiers): # {{{
|
def urls_from_identifiers(identifiers): # {{{
|
||||||
identifiers = {k.lower():v for k, v in identifiers.iteritems()}
|
identifiers = {k.lower():v for k, v in identifiers.iteritems()}
|
||||||
ans = []
|
ans = []
|
||||||
|
keys_left = set(identifiers)
|
||||||
|
|
||||||
|
def add(name, k, val, url):
|
||||||
|
ans.append((name, k, val, url))
|
||||||
|
keys_left.discard(k)
|
||||||
|
|
||||||
rules = msprefs['id_link_rules']
|
rules = msprefs['id_link_rules']
|
||||||
if rules:
|
if rules:
|
||||||
formatter = EvalFormatter()
|
formatter = EvalFormatter()
|
||||||
@ -546,40 +552,49 @@ def urls_from_identifiers(identifiers): # {{{
|
|||||||
import traceback
|
import traceback
|
||||||
traceback.format_exc()
|
traceback.format_exc()
|
||||||
continue
|
continue
|
||||||
ans.append((name, k, val, url))
|
add(name, k, val, url)
|
||||||
for plugin in all_metadata_plugins():
|
for plugin in all_metadata_plugins():
|
||||||
try:
|
try:
|
||||||
for id_type, id_val, url in plugin.get_book_urls(identifiers):
|
for id_type, id_val, url in plugin.get_book_urls(identifiers):
|
||||||
ans.append((plugin.get_book_url_name(id_type, id_val, url), id_type, id_val, url))
|
add(plugin.get_book_url_name(id_type, id_val, url), id_type, id_val, url)
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
isbn = identifiers.get('isbn', None)
|
isbn = identifiers.get('isbn', None)
|
||||||
if isbn:
|
if isbn:
|
||||||
ans.append((isbn, 'isbn', isbn,
|
add(isbn, 'isbn', isbn,
|
||||||
'https://www.worldcat.org/isbn/'+isbn))
|
'https://www.worldcat.org/isbn/'+isbn)
|
||||||
doi = identifiers.get('doi', None)
|
doi = identifiers.get('doi', None)
|
||||||
if doi:
|
if doi:
|
||||||
ans.append(('DOI', 'doi', doi,
|
add('DOI', 'doi', doi,
|
||||||
'https://dx.doi.org/'+doi))
|
'https://dx.doi.org/'+doi)
|
||||||
arxiv = identifiers.get('arxiv', None)
|
arxiv = identifiers.get('arxiv', None)
|
||||||
if arxiv:
|
if arxiv:
|
||||||
ans.append(('arXiv', 'arxiv', arxiv,
|
add('arXiv', 'arxiv', arxiv,
|
||||||
'https://arxiv.org/abs/'+arxiv))
|
'https://arxiv.org/abs/'+arxiv)
|
||||||
oclc = identifiers.get('oclc', None)
|
oclc = identifiers.get('oclc', None)
|
||||||
if oclc:
|
if oclc:
|
||||||
ans.append(('OCLC', 'oclc', oclc,
|
add('OCLC', 'oclc', oclc,
|
||||||
'https://www.worldcat.org/oclc/'+oclc))
|
'https://www.worldcat.org/oclc/'+oclc)
|
||||||
issn = check_issn(identifiers.get('issn', None))
|
issn = check_issn(identifiers.get('issn', None))
|
||||||
if issn:
|
if issn:
|
||||||
ans.append((issn, 'issn', issn,
|
add(issn, 'issn', issn,
|
||||||
'https://www.worldcat.org/issn/'+issn))
|
'https://www.worldcat.org/issn/'+issn)
|
||||||
|
q = {'http', 'https', 'file'}
|
||||||
for k, url in identifiers.iteritems():
|
for k, url in identifiers.iteritems():
|
||||||
if url and re.match(r'ur[il]\d*$', k) is not None:
|
if url and re.match(r'ur[il]\d*$', k) is not None:
|
||||||
url = url[:8].replace('|', ':') + url[8:].replace('|', ',')
|
url = url[:8].replace('|', ':') + url[8:].replace('|', ',')
|
||||||
if url.partition(':')[0].lower() in {'http', 'file', 'https'}:
|
if url.partition(':')[0].lower() in q:
|
||||||
parts = urlparse(url)
|
parts = urlparse(url)
|
||||||
name = parts.netloc or parts.path
|
name = parts.netloc or parts.path
|
||||||
ans.append((name, k, url, url))
|
add(name, k, url, url)
|
||||||
|
for k in tuple(keys_left):
|
||||||
|
val = identifiers.get(k)
|
||||||
|
if val:
|
||||||
|
url = val[:8].replace('|', ':') + val[8:].replace('|', ',')
|
||||||
|
if url.partition(':')[0].lower() in q:
|
||||||
|
parts = urlparse(url)
|
||||||
|
name = parts.netloc or parts.path
|
||||||
|
add(name, k, url, url)
|
||||||
return ans
|
return ans
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user