diff --git a/scholia/app/views.py b/scholia/app/views.py index 7d1d61094..4af247a48 100644 --- a/scholia/app/views.py +++ b/scholia/app/views.py @@ -14,7 +14,7 @@ from ..arxiv import metadata_to_quickstatements, string_to_arxiv from ..arxiv import get_metadata as get_arxiv_metadata from ..query import (arxiv_to_qs, cas_to_qs, atomic_symbol_to_qs, doi_to_qs, - github_to_qs, biorxiv_to_qs, chemrxiv_to_qs, + doi_prefix_to_qs, github_to_qs, biorxiv_to_qs, chemrxiv_to_qs, identifier_to_qs, inchikey_to_qs, issn_to_qs, orcid_to_qs, viaf_to_qs, q_to_class, q_to_dois, random_author, twitter_to_qs, cordis_to_qs, mesh_to_qs, pubmed_to_qs, @@ -721,6 +721,24 @@ def redirect_doi(doi): return render_template('404-doi.html', doi=doi) +@main.route('/doi-prefix/') +def redirect_doi_prefix(doi): + """Detect and redirect for DOI. + + Parameters + ---------- + doi : str + DOI identifier. + + """ + normalize_doi = remove_special_characters_url(doi) + qs = doi_prefix_to_qs(normalize_doi) + if len(qs) > 0: + q = qs[0] + return redirect(url_for('app.show_publisher', q=q), code=302) + return render_template('404.html', doi=doi) + + @main.route('/event/' + q_pattern) def show_event(q): """Return HTML rendering for specific event. diff --git a/scholia/query.py b/scholia/query.py index 0e48bf6d5..2059e4a5c 100644 --- a/scholia/query.py +++ b/scholia/query.py @@ -383,6 +383,44 @@ def doi_to_qs(doi): for item in data['results']['bindings']] +def doi_prefix_to_qs(doi): + """Convert DOI prefix to Wikidata ID. + + Wikidata Query Service is used to resolve the DOI. + + The DOI string is converted to uppercase before any + query is made. Uppercase DOIs are default in Wikidata. + + Parameters + ---------- + doi : str + DOI prefix identifier + + Returns + ------- + qs : list of str + Strings of Wikidata ID. + + Examples + -------- + >>> doi_prefix_to_qs('10.1186') == ['Q463494'] + True + + >>> doi_prefix_to_qs('10.1016') == ['Q746413'] + True + """ + query = 'select ?work where {{ ?work wdt:P1662 "{doi}" }}'.format( + doi=escape_string(doi.upper())) + + url = 'https://query.wikidata.org/sparql' + params = {'query': query, 'format': 'json'} + response = requests.get(url, params=params, headers=HEADERS) + data = response.json() + + return [item['work']['value'][31:] + for item in data['results']['bindings']] + + def iso639_to_q(language): """Convert ISO639 to Q item.