Skip to content

Instantly share code, notes, and snippets.

@fxcoudert
Created November 13, 2019 08:31
Show Gist options
  • Save fxcoudert/fbfb284ee31559444b40ef707780d826 to your computer and use it in GitHub Desktop.
Save fxcoudert/fbfb284ee31559444b40ef707780d826 to your computer and use it in GitHub Desktop.
# -*- 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,halId_s'}
r = requests.get('http://api.archives-ouvertes.fr/search/', params=p)
return r.json()
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 = '#80d080' if oa else '#d0d080'
halid = r['response']['docs'][0]['halId_s']
hal = f'<a href="https://hal.archives-ouvertes.fr/{halid}/">+</a>'
oa = '+' if oa else '–'
oa = f'<a href="https://hal.archives-ouvertes.fr/{halid}/">{oa}</a>'
else:
color = '#d08080'
hal = '–'
oa = '+' if oa else '–'
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>'
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 = header(f'Papers for {first_name} {last_name} (ORCID: {auth_orcid})')
s += papersAvailable(doilist)
return s
else:
s = header('Error')
s += f'ERROR: Could not find ORCID record for: {auth_orcid}'
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 {{
text-decoration: none;
color: black;
}}
table, th, td {{
padding: 2px;
font-size: 95%;
}}
</style>
</head>
<body>
<h1>{title}</h1>
'''
def landingPage():
s = header('Quickly check if your papers are on HAL')
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