forked from SciCrunch/scibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrid.py
208 lines (180 loc) · 7.98 KB
/
rrid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from __future__ import print_function
import requests, re, traceback, pyramid
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
try:
import urlparse
except ImportError: # python3
from urllib import parse as urlparse
from os import environ
from io import StringIO
from datetime import datetime
import csv
import gzip
from lxml import etree
from hypothesis import HypothesisUtils
from export import export_impl
api_token = environ.get('RRIDBOT_API_TOKEN', 'TOKEN') # Hypothesis API dev token
username = environ.get('RRIDBOT_USERNAME', 'USERNAME') # Hypothesis username
group = environ.get('RRIDBOT_GROUP', '__world__')
print(username, group) # sanity check
prod_username = 'scibot' # nasty hardcode
if username == prod_username:
host = '0.0.0.0'
port = 80
else:
print('no login detected, running on localhost only')
host = 'localhost'
port = 8080
host_port = 'http://' + host + ':' + str(port)
def bookmarklet(request):
""" Return text of the RRID bookmarklet """
text = """javascript:(function(){var xhr=new XMLHttpRequest();var params='uri='+location.href+'&data='+encodeURIComponent(document.body.innerText);xhr.open('POST','%s/rrid',true);xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");xhr.setRequestHeader("Access-Control-Allow-Origin","*");xhr.onreadystatechange=function(){if(xhr.readyState==4)console.log('rrids: '+xhr.responseText)};xhr.send(params)}());""" % request.application_url
text = text.replace('"',"'")
html = """<html>
<head>
<style>
body { font-family: verdana; margin:.75in }
</style>
<title>rrid bookmarklet</title></head>
<body>
<p>To install the bookmarklet, drag this link -- <a href="%s">rrid</a> -- to your bookmarks bar.</p>
<p>If you need to copy/paste the bookmarklet's code into a bookmarklet, it's here:</p>
<p>%s</p>
</body>
</html>
""" % ( text, text )
r = Response(html)
r.content_type = 'text/html'
return r
def rrid(request):
""" Receive an article, parse RRIDs, resolve them, create annotations, log results """
if request.method == 'OPTIONS':
response = Response()
request_headers = request.headers['Access-Control-Request-Headers'].lower()
request_headers = re.findall('\w(?:[-\w]*\w)', request_headers)
response_headers = ['access-control-allow-origin']
for req_acoa_header in request_headers:
if req_acoa_header not in response_headers:
response_headers.append(req_acoa_header)
response_headers = ','.join(response_headers)
response.headers.update({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '%s' % response_headers
})
response.status_int = 204
return response
h = HypothesisUtils(username=username, token=api_token, group=group)
target_uri = urlparse.parse_qs(request.text)['uri'][0]
params = { 'limit':200, 'uri':target_uri }
query_url = h.query_url_template.format(query=urlencode(params, True))
obj = h.authenticated_api_query(query_url)
rows = obj['rows']
tags = set()
for row in rows:
if row['group'] != h.group: # api query returns unwanted groups
continue
elif row['user'] != 'acct:' + h.username + '@hypothes.is':
continue
for tag in row['tags']:
if tag.startswith('RRID'):
tags.add(tag)
html = urlparse.parse_qs(request.text)['data'][0]
print(target_uri)
found_rrids = {}
try:
matches = re.findall('(.{0,10})(RRID:\s*)([_\w\-:]+)([^\w].{0,10})', html.replace('–','-'))
existing = []
for match in matches:
print(match)
prefix = match[0]
exact = match[2]
if 'RRID:'+exact in tags:
print('skipping %s, already annotated' % exact)
continue
new_tags = []
if exact in existing:
new_tags.append('RRIDCUR:Duplicate')
else:
existing.append(exact)
found_rrids[exact] = None
suffix = match[3]
print('\t' + exact)
resolver_uri = 'https://scicrunch.org/resolver/%s.xml' % exact
r = requests.get(resolver_uri)
print(r.status_code)
xml = r.content
found_rrids[exact] = r.status_code
if r.status_code < 300:
root = etree.fromstring(xml)
if root.findall('error'):
s = 'Resolver lookup failed.'
s += '<hr><p><a href="%s">resolver lookup</a></p>' % resolver_uri
r = h.create_annotation_with_target_using_only_text_quote(url=target_uri, prefix=prefix, exact=exact, suffix=suffix, text=s, tags=new_tags + ['RRIDCUR:Unresolved'])
print('ERROR')
else:
data_elements = root.findall('data')[0]
s = ''
data_elements = [(e.find('name').text, e.find('value').text) for e in data_elements] # these shouldn't duplicate
citation = [(n, v) for n, v in data_elements if n == 'Proper Citation']
name = [(n, v) for n, v in data_elements if n == 'Name']
data_elements = citation + name + sorted([(n, v) for n, v in data_elements if (n != 'Proper Citation' or n != 'Name') and v is not None])
for name, value in data_elements:
if (name == 'Reference' or name == 'Mentioned In Literature') and value is not None and value.startswith('<a class'):
if len(value) > 500:
continue # nif-0000-30467 fix keep those pubmed links short!
s += '<p>%s: %s</p>' % (name, value)
s += '<hr><p><a href="%s">resolver lookup</a></p>' % resolver_uri
r = h.create_annotation_with_target_using_only_text_quote(url=target_uri, prefix=prefix, exact=exact, suffix=suffix, text=s, tags=new_tags + ['RRID:'+exact])
else:
s = 'Resolver lookup failed.'
r = h.create_annotation_with_target_using_only_text_quote(url=target_uri, prefix=prefix, exact=exact, suffix=suffix, text=s, tags=new_tags + ['RRIDCUR:Unresolved'])
except:
print(traceback.print_exc())
results = ', '.join(found_rrids.keys())
r = Response(results)
r.content_type = 'text/plain'
r.headers.update({
'Access-Control-Allow-Origin': '*'
})
try:
now = datetime.now().isoformat()[0:19].replace(':','').replace('-','')
fname = 'rrid-%s.log' % now
s = 'URL: %s\n\nResults: %s\n\nCount: %s\n\nText:\n\n%s' % ( target_uri, results, len(found_rrids), html )
with open(fname, 'wb') as f:
f.write(s.encode('utf-8'))
except:
print(traceback.print_exc())
return r
def export(request):
print('starting export')
output_rows, DATE = export_impl()
data = StringIO()
writer = csv.writer(data)
writer.writerows(sorted(output_rows))
r = Response(gzip.compress(data.getvalue().encode()))
r.content_type = 'text/csv'
r.headers.update({
'Content-Disposition':'attachment;filename = RRID-data-%s.csv' % DATE,
'Content-Encoding':'gzip'
})
return r
if __name__ == '__main__':
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
config = Configurator()
config.add_route('rrid', '/rrid')
config.add_view(rrid, route_name='rrid')
config.add_route('bookmarklet', '/bookmarklet')
config.add_view(bookmarklet, route_name='bookmarklet')
config.add_route('export', '/export')
config.add_view(export, route_name='export')
app = config.make_wsgi_app()
print('host: %s, port %s' % ( host, port ))
server = make_server(host, port, app)
server.serve_forever()