Created
November 16, 2019 16:49
-
-
Save fxcoudert/43d72e2ffba0fefcef8f59e18a130a56 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 | |
orcid_api_id = 'xxxxxx' | |
orcid_api_secret = 'xxxxxx' | |
base_api_ua = 'xxxxxx' | |
orcid_api_email = 'xxxxxx' | |
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_id:' + doi, | |
'fl': 'openAccess_bool,docid,halId_s'} | |
r = requests.get('http://api.archives-ouvertes.fr/search/', params=p) | |
return r.json() | |
def unpaywall(doi): | |
r = requests.get(f'https://api.unpaywall.org/v2/{doi}', | |
params={"email": "[email protected]"}) | |
try: | |
r = r.json() | |
except: | |
return None | |
if 'error' in r: | |
return None | |
if r['is_oa']: | |
return r['best_oa_location']['url_for_landing_page'] | |
return None | |
def base_search(doi): | |
r = requests.get('https://api.base-search.net/cgi-bin/BaseHttpSearchInterface.fcgi', | |
params={'func': 'PerformSearch', 'query': f'dcdoi:{doi}', 'boost': 'oa', 'format': 'json'}, | |
headers={'user-agent': '[email protected]'}) | |
docs = r.json()['response']['docs'] | |
for doc in docs: | |
if doc['dcoa'] == 1: | |
return doc['dcidentifier'][0] | |
return None | |
def isOA(doi): | |
x = unpaywall(doi) | |
if x: | |
return x | |
x = base_search(doi) | |
if x: | |
return x | |
return None | |
def papersAvailable(doilist, auth_orcid): | |
res = [] | |
for doi, title in doilist.values(): | |
res.append((doi, title, isOA(doi))) | |
nOA = sum(1 for x in res if x[2]) | |
score = 100 * nOA / len(res) | |
s = f'<p style="font-size: 140%">Your OA score is: <b>{score:.1f}%</b></p>' | |
s += '<p style="margin-top: 30px">Here is the detail of your papers,' | |
s += f' as recorded on <a href="https://orcid.org/{auth_orcid}">ORCID {auth_orcid}</a>:</p>' | |
s += '<table>' | |
s += '<tr><td>Title</td><td>DOI</td><td>Open access?</td></tr>' | |
for doi, title, oa in res: | |
color = '#80d080' if oa else '#d0d080' | |
href = 'https://doi.org/' + doi | |
s += f'<tr style="background-color: {color}"><td><a href="{href}">{title}</a></td><td><a href="{href}">{doi}</a></td>' | |
if oa: | |
s += f'<td style="text-align: center"><a href="{oa}">Yes</a></td></tr>' | |
else: | |
s += f'<td style="text-align: center">Not found</td></tr>' | |
s += '</table>' | |
s += '<p style="margin-top: 30px">Open access data is taken from <a href="https://unpaywall.org">Unpaywall</a>' | |
s += ' and <a href="https://www.base-search.net">BASE</a>.</p>' | |
return s | |
def auditORCID(auth_orcid): | |
first_name, last_name, doilist = retrieveFromORCID(auth_orcid) | |
if doilist: | |
s = header(f'Papers for {first_name} {last_name}') | |
s += papersAvailable(doilist, auth_orcid) | |
return s | |
else: | |
s = header('Error') | |
s += f'<p>Could not find ORCID record for: <a href="https://orcid.org/{auth_orcid}">{auth_orcid}</a>.</p>' | |
s += '<p>Either you do not have a DOI, or that DOI does not have any papers listed.</p>' | |
return s | |
def header(title): | |
return f''' | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>{title}</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<link rel="stylesheet" type="text/css" href="default.css" /> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<style> | |
a {{ | |
color: black; | |
}} | |
table a {{ | |
text-decoration: none; | |
}} | |
table, th, td {{ | |
padding: 2px; | |
font-size: 95%; | |
}} | |
</style> | |
</head> | |
<body> | |
<h1>{title}</h1> | |
''' | |
def landingPage(): | |
s = header("Check your Open Access score!") | |
s += 'Enter your ORCID here:' | |
s += '<form><input type="text" name="orcid" /><input type="submit" value="Submit"></form>' | |
s += 'This can take up to 3 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