Created
October 8, 2019 13:41
-
-
Save fxcoudert/67898042e370329bad8de2ca00811b14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import collections | |
import json | |
import orcid | |
import requests | |
import sys | |
import time | |
def retrieveFromORCID(auth_orcid): | |
def findDOI(paper): | |
for i in paper['external-ids']['external-id']: | |
if i['external-id-type'] == 'doi': | |
return i['external-id-value'] | |
return None | |
api = orcid.PublicAPI(orcid_api_id, orcid_api_secret) | |
token = api.get_search_token_from_orcid() | |
try: | |
record = api.read_record_public(auth_orcid, 'record', token) | |
name = record['person']['name'] | |
works = api.read_record_public(auth_orcid, 'works', token)['group'] | |
except: | |
return None, None, None | |
# Store DOIs as keys in an ordered dict, with uppercase, to avoid duplicates | |
doilist = collections.OrderedDict() | |
for paper in works: | |
doi = findDOI(paper) | |
if doi: | |
doilist[doi.upper()] = (doi, paper['work-summary'][0]['title']['title']['value']) | |
return name["given-names"]["value"], name["family-name"]["value"], doilist | |
def HALfromDOI(doi): | |
p = {'wt': 'json', | |
'q': 'doiId_s:"' + doi + '"', | |
'fl': 'openAccess_bool,docid'} | |
r = requests.get('http://api.archives-ouvertes.fr/search/', params=p) | |
r = r.json() | |
if 'response' in r and r['response']['numFound'] > 0: | |
return r | |
else: | |
# Search should be case insensitive, but it is not :( | |
# Work around this bug by performing a second search on lowercase | |
# if there were no results | |
p = {'wt': 'json', | |
'q': 'doiId_s:"' + doi.lower() + '"', | |
'fl': 'openAccess_bool,docid'} | |
r = requests.get('http://api.archives-ouvertes.fr/search/', params=p) | |
r = r.json() | |
if 'response' in r and r['response']['numFound'] > 0: | |
return r | |
else: | |
p = {'wt': 'json', | |
'q': 'doiId_s:"' + doi.upper() + '"', | |
'fl': 'openAccess_bool,docid'} | |
r = requests.get('http://api.archives-ouvertes.fr/search/', params=p) | |
r = r.json() | |
return r | |
def papersAvailable(doilist): | |
s = '<table>' | |
s += '<tr><td>Title</td><td>DOI</td><td>In HAL?</td><td>Full text?</td></tr>' | |
for doi, title in doilist.values(): | |
r = HALfromDOI(doi) | |
hal = False | |
oa = False | |
if 'response' in r and r['response']['numFound'] > 0: | |
hal = True | |
oa = r['response']['docs'][0]['openAccess_bool'] | |
if hal: | |
color = '#40d040' if oa else '#d0d040' | |
else: | |
color = '#d06060' | |
hal = '+' if hal else '–' | |
oa = '+' if oa else '–' | |
s += f'<tr style="background-color: {color}"><td>{title}</td><td>{doi}</td>' | |
s += f'<td style="text-align: center">{hal}</td><td style="text-align: center">{oa}</td></tr>' | |
s += '</table>' | |
return s | |
def auditORCID(auth_orcid): | |
first_name, last_name, doilist = retrieveFromORCID(auth_orcid) | |
if doilist: | |
s = f'<h1>Papers for {first_name} {last_name} (ORCID: {auth_orcid})</h1>' | |
s += papersAvailable(doilist) | |
return s | |
else: | |
return f'ERROR: Could not find ORCID record for: {auth_orcid}' | |
def landingPage(): | |
s = '<h1>Quickly check if your papers are on HAL</h1>' | |
s += 'Enter your ORCID here:' | |
s += '<form><input type="text" name="orcid" /><input type="submit" value="Submit"></form>' | |
s += 'This can take up to 2 minutes, please wait after you click...' | |
return s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment