Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirect for doi-prefix as specified #1999

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion scholia/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -721,6 +721,24 @@ def redirect_doi(doi):
return render_template('404-doi.html', doi=doi)


@main.route('/doi-prefix/<path:doi>')
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.
Expand Down
38 changes: 38 additions & 0 deletions scholia/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down