示例#1
0
def urls_from_identifiers(identifiers):  # {{{
    identifiers = {k.lower(): v for k, v in identifiers.iteritems()}
    ans = []
    rules = msprefs['id_link_rules']
    if rules:
        formatter = EvalFormatter()
        for k, val in identifiers.iteritems():
            vals = {
                'id':
                quote(val if isinstance(val, bytes) else val.encode('utf-8')).
                decode('ascii')
            }
            items = rules.get(k) or ()
            for name, template in items:
                try:
                    url = formatter.safe_format(template, vals, '', vals)
                except Exception:
                    import traceback
                    traceback.format_exc()
                    continue
                ans.append((name, k, val, url))
    for plugin in all_metadata_plugins():
        try:
            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))
        except:
            pass
    isbn = identifiers.get('isbn', None)
    if isbn:
        ans.append(
            (isbn, 'isbn', isbn, 'http://www.worldcat.org/isbn/' + isbn))
    doi = identifiers.get('doi', None)
    if doi:
        ans.append(('DOI', 'doi', doi, 'http://dx.doi.org/' + doi))
    arxiv = identifiers.get('arxiv', None)
    if arxiv:
        ans.append(('arXiv', 'arxiv', arxiv, 'http://arxiv.org/abs/' + arxiv))
    oclc = identifiers.get('oclc', None)
    if oclc:
        ans.append(
            ('OCLC', 'oclc', oclc, 'http://www.worldcat.org/oclc/' + oclc))
    issn = check_issn(identifiers.get('issn', None))
    if issn:
        ans.append(
            (issn, 'issn', issn, 'http://www.worldcat.org/issn/' + issn))
    for k, url in identifiers.iteritems():
        if url and re.match(r'ur[il]\d*$', k) is not None:
            url = url[:8].replace('|', ':') + url[8:].replace('|', ',')
            if url.partition(':')[0].lower() in {'http', 'file', 'https'}:
                parts = urlparse(url)
                name = parts.netloc or parts.path
                ans.append((name, k, url, url))
    return ans
示例#2
0
文件: identify.py 项目: Coi-l/calibre
def urls_from_identifiers(identifiers):  # {{{
    identifiers = {k.lower():v for k, v in identifiers.iteritems()}
    ans = []
    rules = msprefs['id_link_rules']
    if rules:
        formatter = EvalFormatter()
        for k, val in identifiers.iteritems():
            vals = {'id':quote(val if isinstance(val, bytes) else val.encode('utf-8')).decode('ascii')}
            items = rules.get(k) or ()
            for name, template in items:
                try:
                    url = formatter.safe_format(template, vals, '', vals)
                except Exception:
                    import traceback
                    traceback.format_exc()
                    continue
                ans.append((name, k, val, url))
    for plugin in all_metadata_plugins():
        try:
            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))
        except:
            pass
    isbn = identifiers.get('isbn', None)
    if isbn:
        ans.append((isbn, 'isbn', isbn,
            'http://www.worldcat.org/isbn/'+isbn))
    doi = identifiers.get('doi', None)
    if doi:
        ans.append(('DOI', 'doi', doi,
            'http://dx.doi.org/'+doi))
    arxiv = identifiers.get('arxiv', None)
    if arxiv:
        ans.append(('arXiv', 'arxiv', arxiv,
            'http://arxiv.org/abs/'+arxiv))
    oclc = identifiers.get('oclc', None)
    if oclc:
        ans.append(('OCLC', 'oclc', oclc,
            'http://www.worldcat.org/oclc/'+oclc))
    issn = check_issn(identifiers.get('issn', None))
    if issn:
        ans.append((issn, 'issn', issn,
            'http://www.worldcat.org/issn/'+issn))
    for k, url in identifiers.iteritems():
        if url and re.match(r'ur[il]\d*$', k) is not None:
            url = url[:8].replace('|', ':') + url[8:].replace('|', ',')
            if url.partition(':')[0].lower() in {'http', 'file', 'https'}:
                parts = urlparse(url)
                name = parts.netloc or parts.path
                ans.append((name, k, url, url))
    return ans
示例#3
0
def urls_from_identifiers(identifiers):  # {{{
    identifiers = dict([(k.lower(), v) for k, v in identifiers.iteritems()])
    ans = []
    for plugin in all_metadata_plugins():
        try:
            id_type, id_val, url = plugin.get_book_url(identifiers)
            ans.append((plugin.get_book_url_name(id_type, id_val, url), id_type, id_val, url))
        except:
            pass
    isbn = identifiers.get('isbn', None)
    if isbn:
        ans.append((isbn, 'isbn', isbn,
            'http://www.worldcat.org/isbn/'+isbn))
    doi = identifiers.get('doi', None)
    if doi:
        ans.append(('DOI', 'doi', doi,
            'http://dx.doi.org/'+doi))
    arxiv = identifiers.get('arxiv', None)
    if arxiv:
        ans.append(('arXiv', 'arxiv', arxiv,
            'http://arxiv.org/abs/'+arxiv))
    oclc = identifiers.get('oclc', None)
    if oclc:
        ans.append(('OCLC', 'oclc', oclc,
            'http://www.worldcat.org/oclc/'+oclc))
    issn = check_issn(identifiers.get('issn', None))
    if issn:
        ans.append((issn, 'issn', issn,
            'http://www.worldcat.org/issn/'+issn))
    for x in ('uri', 'url'):
        url = identifiers.get(x, None)
        if url and url.startswith('http'):
            url = url[:8].replace('|', ':') + url[8:].replace('|', ',')
            parts = urlparse(url)
            name = parts.netloc
            ans.append((name, x, url, url))
    return ans
示例#4
0
def urls_from_identifiers(identifiers):  # {{{
    identifiers = dict([(k.lower(), v) for k, v in identifiers.iteritems()])
    ans = []
    for plugin in all_metadata_plugins():
        try:
            id_type, id_val, url = plugin.get_book_url(identifiers)
            ans.append((plugin.get_book_url_name(id_type, id_val, url), id_type, id_val, url))
        except:
            pass
    isbn = identifiers.get('isbn', None)
    if isbn:
        ans.append((isbn, 'isbn', isbn,
            'http://www.worldcat.org/isbn/'+isbn))
    doi = identifiers.get('doi', None)
    if doi:
        ans.append(('DOI', 'doi', doi,
            'http://dx.doi.org/'+doi))
    arxiv = identifiers.get('arxiv', None)
    if arxiv:
        ans.append(('arXiv', 'arxiv', arxiv,
            'http://arxiv.org/abs/'+arxiv))
    oclc = identifiers.get('oclc', None)
    if oclc:
        ans.append(('OCLC', 'oclc', oclc,
            'http://www.worldcat.org/oclc/'+oclc))
    issn = check_issn(identifiers.get('issn', None))
    if issn:
        ans.append((issn, 'issn', issn,
            'http://www.worldcat.org/issn/'+issn))
    for x in ('uri', 'url'):
        url = identifiers.get(x, None)
        if url and url.startswith('http'):
            url = url[:8].replace('|', ':') + url[8:].replace('|', ',')
            parts = urlparse(url)
            name = parts.netloc
            ans.append((name, x, url, url))
    return ans
示例#5
0
def urls_from_identifiers(identifiers):  # {{{
    identifiers = {k.lower(): v for k, v in identifiers.iteritems()}
    ans = []
    for plugin in all_metadata_plugins():
        try:
            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))
        except:
            pass
    isbn = identifiers.get('isbn', None)
    if isbn:
        ans.append(
            (isbn, 'isbn', isbn, 'http://www.worldcat.org/isbn/' + isbn))
    doi = identifiers.get('doi', None)
    if doi:
        ans.append(('DOI', 'doi', doi, 'http://dx.doi.org/' + doi))
    arxiv = identifiers.get('arxiv', None)
    if arxiv:
        ans.append(('arXiv', 'arxiv', arxiv, 'http://arxiv.org/abs/' + arxiv))
    oclc = identifiers.get('oclc', None)
    if oclc:
        ans.append(
            ('OCLC', 'oclc', oclc, 'http://www.worldcat.org/oclc/' + oclc))
    issn = check_issn(identifiers.get('issn', None))
    if issn:
        ans.append(
            (issn, 'issn', issn, 'http://www.worldcat.org/issn/' + issn))
    for k, url in identifiers.iteritems():
        if url and re.match(r'ur[il]\d*$', k) is not None and url.partition(
                ':')[0].lower() in {'http', 'file', 'https'}:
            url = url[:8].replace('|', ':') + url[8:].replace('|', ',')
            parts = urlparse(url)
            name = parts.netloc or parts.path
            ans.append((name, k, url, url))
    return ans
示例#6
0
def urls_from_identifiers(identifiers):  # {{{
    identifiers = {k.lower():v for k, v in identifiers.iteritems()}
    ans = []
    for plugin in all_metadata_plugins():
        try:
            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))
        except:
            pass
    isbn = identifiers.get('isbn', None)
    if isbn:
        ans.append((isbn, 'isbn', isbn,
            'http://www.worldcat.org/isbn/'+isbn))
    doi = identifiers.get('doi', None)
    if doi:
        ans.append(('DOI', 'doi', doi,
            'http://dx.doi.org/'+doi))
    arxiv = identifiers.get('arxiv', None)
    if arxiv:
        ans.append(('arXiv', 'arxiv', arxiv,
            'http://arxiv.org/abs/'+arxiv))
    oclc = identifiers.get('oclc', None)
    if oclc:
        ans.append(('OCLC', 'oclc', oclc,
            'http://www.worldcat.org/oclc/'+oclc))
    issn = check_issn(identifiers.get('issn', None))
    if issn:
        ans.append((issn, 'issn', issn,
            'http://www.worldcat.org/issn/'+issn))
    for k, url in identifiers.iteritems():
        if url and re.match(r'ur[il]\d*$', k) is not None and url.partition(':')[0].lower() in {'http', 'file', 'https'}:
            url = url[:8].replace('|', ':') + url[8:].replace('|', ',')
            parts = urlparse(url)
            name = parts.netloc or parts.path
            ans.append((name, k, url, url))
    return ans
示例#7
0
def urls_from_identifiers(identifiers, sort_results=False):  # {{{
    identifiers = {k.lower(): v for k, v in iteritems(identifiers)}
    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']
    if rules:
        formatter = EvalFormatter()
        for k, val in iteritems(identifiers):
            val = val.replace('|', ',')
            vals = {
                'id':
                unicode_type(
                    quote(val if isinstance(val, bytes) else val.encode('utf-8'
                                                                        ))),
                'id_unquoted':
                str(val),
            }
            items = rules.get(k) or ()
            for name, template in items:
                try:
                    url = formatter.safe_format(template, vals, '', vals)
                except Exception:
                    import traceback
                    traceback.format_exc()
                    continue
                add(name, k, val, url)
    for plugin in all_metadata_plugins():
        try:
            for id_type, id_val, url in plugin.get_book_urls(identifiers):
                add(plugin.get_book_url_name(id_type, id_val, url), id_type,
                    id_val, url)
        except Exception:
            pass
    isbn = identifiers.get('isbn', None)
    if isbn:
        add(isbn, 'isbn', isbn, 'https://www.worldcat.org/isbn/' + isbn)
    doi = identifiers.get('doi', None)
    if doi:
        add('DOI', 'doi', doi, 'https://dx.doi.org/' + doi)
    arxiv = identifiers.get('arxiv', None)
    if arxiv:
        add('arXiv', 'arxiv', arxiv, 'https://arxiv.org/abs/' + arxiv)
    oclc = identifiers.get('oclc', None)
    if oclc:
        add('OCLC', 'oclc', oclc, 'https://www.worldcat.org/oclc/' + oclc)
    issn = check_issn(identifiers.get('issn', None))
    if issn:
        add(issn, 'issn', issn, 'https://www.worldcat.org/issn/' + issn)
    q = {'http', 'https', 'file'}
    for k, url in iteritems(identifiers):
        if url and re.match(r'ur[il]\d*$', k) is not None:
            url = url[:8].replace('|', ':') + url[8:].replace('|', ',')
            if url.partition(':')[0].lower() in q:
                parts = urlparse(url)
                name = parts.netloc or parts.path
                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)
    if sort_results:

        def url_key(x):
            return primary_sort_key(str(x[0]))

        ans = sorted(ans, key=url_key)
    return ans