Created
August 11, 2017 01:19
-
-
Save judell/02f9d923e81810b08ac36fafc2f68277 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def genius_test(): | |
h = Hypothesis(username="judell", | |
token='6879- ... a3df5', | |
group='Pk ... gL') | |
anno_id = '12192534' | |
genius_token = 'yOalM ... R4MuU' | |
url = 'https://api.genius.com/annotations/' + anno_id | |
headers = {'Authorization': 'Bearer ' + genius_token, 'Content-Type': 'application/json;charset=utf-8' } | |
r = requests.get(url, headers=headers) | |
annotation = r.json() | |
web_page_id = '560764' // | |
page = 1 | |
done = False | |
all_referents = [] | |
while done is False: | |
url = 'https://api.genius.com/referents?web_page_id=%s&per_page=20&page=%s' % (web_page_id, page) | |
headers = {'Authorization': 'Bearer ' + genius_token, 'Content-Type': 'application/json;charset=utf-8' } | |
response = requests.get(url, headers=headers) | |
r = response.json() | |
referents = r['response']['referents'] | |
if len(referents) == 0: | |
done = True | |
else: | |
all_referents = all_referents + referents | |
page = page + 1 | |
callums = [r for r in all_referents if r['annotator_login'] == 'CallumBorchers'] | |
def complex_node(node): | |
tag = None | |
attributes = None | |
children = None | |
for item in node: | |
if item == 'tag': tag = node[item] | |
if item == 'attributes': attributes = node[item] | |
if item == 'children': children = node[item] | |
return {'tag':tag, 'attributes':attributes, 'children':children } | |
url = 'https://www.washingtonpost.com/news/the-fix/wp/2017/06/20/sean-spicers-off-camera-no-audio-press-briefing-annotated/' | |
title = "Sean Spicer's off-camera, no-audio press briefing, annotated" | |
for callum in callums: | |
range = callum['range'] | |
prefix = range['before'][-30:] | |
exact = range['content'] | |
suffix = range['after'][0:30] | |
dom = callum['annotations'][0]['body']['dom'] | |
text = '' | |
for child in dom['children']: | |
if not 'children' in child: | |
continue | |
print child['tag'] | |
if child['tag'] == 'p': | |
text = text + '<p>' | |
nodes = child['children'] | |
for node in nodes: | |
if type(node) is unicode: | |
text = text + node | |
else: | |
complex = complex_node(node) | |
if complex['tag'] == 'a': | |
text = text + ' <a href="%s">%s</a> ' % ( | |
complex['attributes']['href'], | |
complex['children'][0] | |
) | |
payload = { | |
"uri": url, | |
"target": | |
[{ | |
"source": [url], | |
"selector": | |
[{ | |
"type": "TextQuoteSelector", | |
"prefix": prefix, | |
"exact": exact, | |
"suffix": suffix | |
} | |
] | |
}], | |
"text": text, | |
"document": { | |
"title": [title] | |
}, | |
"permissions": h.permissions, | |
"group": h.group | |
} | |
r = h.post_annotation(payload) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment