forked from SciCrunch/scibot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hypothesis.py
196 lines (179 loc) · 7.93 KB
/
hypothesis.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
#!/usr/bin/env python3
from __future__ import print_function
import json
import requests
import traceback
try:
from urllib.parse import urlencode
except ImportError:
from urllib import urlencode
class HypothesisUtils:
""" services for authenticating, searching, creating annotations """
def __init__(self, username=None, token=None, limit=None, max_results=None, domain=None, group=None):
if domain is None:
self.domain = 'hypothes.is'
else:
self.domain = domain
if username is not None:
self.username = username
if token is not None:
self.token = token
self.app_url = 'https://%s/app' % self.domain
self.api_url = 'https://%s/api' % self.domain
self.query_url_template = 'https://%s/api/search?{query}' % self.domain
self.group = group if group is not None else '__world__'
self.single_page_limit = 200 if limit is None else limit # per-page, the api honors limit= up to (currently) 200
self.multi_page_limit = 200 if max_results is None else max_results # limit for paginated results
self.permissions = {
"read": ['group:' + self.group],
"update": ['acct:' + self.username + '@hypothes.is'],
"delete": ['acct:' + self.username + '@hypothes.is'],
"admin": ['acct:' + self.username + '@hypothes.is']
}
def authenticated_api_query(self, query_url=None):
try:
headers = {'Authorization': 'Bearer ' + self.token, 'Content-Type': 'application/json;charset=utf-8' }
r = requests.get(query_url, headers=headers)
obj = json.loads(r.text)
return obj
except:
print(traceback.print_exc())
def make_annotation_payload_with_target_using_only_text_quote(self, url, prefix, exact, suffix, text, tags):
"""Create JSON payload for API call."""
if tags == None:
tags = []
url = url.rstrip('//')
payload = {
"uri": url,
"user": 'acct:' + self.username + '@hypothes.is',
"permissions": self.permissions,
"group": self.group,
"target":
[{
"scope": [url],
"selector":
[{
"type": "TextQuoteSelector",
"prefix": prefix,
"exact": exact,
"suffix": suffix
},]
}],
"tags": tags,
"text": text
}
return payload
def create_annotation_with_target_using_only_text_quote(self, url=None, prefix=None,
exact=None, suffix=None, text=None, tags=None, tag_prefix=None):
"""Call API with token and payload, create annotation (using only text quote)"""
payload = self.make_annotation_payload_with_target_using_only_text_quote(url, prefix, exact, suffix, text, tags)
try:
r = self.post_annotation(payload)
except:
print(traceback.print_exc())
r = None # if we get here someone probably ran the bookmarklet from firefox or the like
return r
def post_annotation(self, payload):
headers = {'Authorization': 'Bearer ' + self.token, 'Content-Type': 'application/json;charset=utf-8' }
data = json.dumps(payload, ensure_ascii=False)
r = requests.post(self.api_url + '/annotations', headers=headers, data=data.encode('utf-8'))
return r
def search_all(self, params={}):
"""Call search API with pagination, return rows """
params['offset'] = 0
params['limit'] = self.single_page_limit
while True:
query_url = self.query_url_template.format(query=urlencode(params, True))
obj = self.authenticated_api_query(query_url)
rows = obj['rows']
row_count = len(rows)
if 'replies' in obj:
rows += obj['replies']
params['offset'] += row_count
if params['offset'] > self.multi_page_limit:
break
if len(rows) is 0:
break
for row in rows:
yield row
class HypothesisAnnotation:
"""Encapsulate one row of a Hypothesis API search."""
def __init__(self, row):
self.type = None
self.id = row['id']
self.updated = row['updated'][0:19]
self.user = row['user'].replace('acct:','').replace('@hypothes.is','')
if 'uri' in row: # should it ever not?
self.uri = row['uri']
else:
self.uri = "no uri field for %s" % self.id
self.uri = self.uri.replace('https://via.hypothes.is/h/','').replace('https://via.hypothes.is/','')
if self.uri.startswith('urn:x-pdf') and 'document' in row:
if 'link' in row['document']:
self.links = row['document']['link']
for link in self.links:
self.uri = link['href']
if self.uri.encode('utf-8').startswith('urn:') == False:
break
if self.uri.encode('utf-8').startswith('urn:') and 'filename' in row['document']:
self.uri = row['document']['filename']
if 'document' in row and 'title' in row['document']:
t = row['document']['title']
if isinstance(t, list) and len(t):
self.doc_title = t[0]
else:
self.doc_title = t
else:
self.doc_title = self.uri
if self.doc_title is None:
self.doc_title = ''
self.doc_title = self.doc_title.replace('"',"'")
if self.doc_title == '': self.doc_title = 'untitled'
self.tags = []
if 'tags' in row and row['tags'] is not None:
self.tags = row['tags']
if isinstance(self.tags, list):
self.tags = [t.strip() for t in self.tags]
self.text = ''
if 'text' in row:
self.text = row['text']
self.references = []
if 'references' in row:
self.type = 'reply'
self.references = row['references']
self.target = []
if 'target' in row:
self.target = row['target']
self.is_page_note = False
try:
if self.references == [] and self.target is not None and len(self.target) and isinstance(self.target,list) and 'selector' not in self.target[0]:
self.is_page_note = True
self.type = 'pagenote'
except:
traceback.print_exc()
if 'document' in row and 'link' in row['document']:
self.links = row['document']['link']
if not isinstance(self.links, list):
self.links = [{'href':self.links}]
else:
self.links = []
self.start = self.end = self.prefix = self.exact = self.suffix = None
try:
if isinstance(self.target,list) and len(self.target) and 'selector' in self.target[0]:
self.type = 'annotation'
selectors = self.target[0]['selector']
for selector in selectors:
if 'type' in selector and selector['type'] == 'TextQuoteSelector':
try:
self.prefix = selector['prefix']
self.exact = selector['exact']
self.suffix = selector['suffix']
except:
traceback.print_exc()
if 'type' in selector and selector['type'] == 'TextPositionSelector' and 'start' in selector:
self.start = selector['start']
self.end = selector['end']
if 'type' in selector and selector['type'] == 'FragmentSelector' and 'value' in selector:
self.fragment_selector = selector['value']
except:
print(traceback.format_exc())