-
Notifications
You must be signed in to change notification settings - Fork 19
/
hypothesis.py
73 lines (56 loc) · 2.66 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
import requests
# add / to the uri to make exclude sub pages
from config import hypothesisToken, hypothesisUsername, defaultIndentLevel, isManageHypothesis, getHypothesisTagSpaceHandler
from utils import getWebPageTitle
from itertools import groupby
defaultIndentLevel = defaultIndentLevel + " "
def byURI(data):
return data['uri']
def getHypothesisAnnotations(targetURI):
headers = {"Authorization": "Bearer " + hypothesisToken}
# targetURI = "https://web.hypothes.is"
endpoint = "https://api.hypothes.is/api/search?url=" + targetURI + "&limit=200&order=asc&user=acct:" + hypothesisUsername
#print(endpoint)
r = requests.get(endpoint, headers=headers).json()
# headers = {"Authorization": "Bearer " + hypothesisToken}
# targetURI = "https://web.hypothes.is"
# endpoint = "https://api.hypothes.is/api/search?url=" + targetURI + "&limit=200&user=acct:" + hypothesisUsername
grouped_byURI = groupby(sorted(r['rows'], key=byURI), byURI)
groupedByURI = {}
for k, v in grouped_byURI:
groupedByURI[k] = list(v)
# Title r['https://web.hypothes.is/'][0]['document']['title']
# Highlighted r['https://web.hypothes.is/'][0]['target'][0]['selector'][2]['exact']
# annotation r['https://web.hypothes.is/'][0]['text']
# tags r['https://web.hypothes.is/'][0]['tags']
#print(groupedByURI)
outText = ""
for row in groupedByURI:
#print(row)
outText += defaultIndentLevel + "[" + getWebPageTitle(row) + "](" + row + ")" + "\n"
#print(title)
# print((groupedByURI[row][0]['target'][0]['selector'][2]['exact']).strip())
for i in range(len(groupedByURI[row])):
outText += "#" + defaultIndentLevel + (groupedByURI[row][i]['target'][0]['selector'][2]['exact']).strip() + "\n"
if groupedByURI[row][i]['text']:
annotation = groupedByURI[row][i]['text'] + "\n"
else:
annotation = ""
tags = ""
for tag in groupedByURI[row][i]['tags']:
if (' ' in tag):
if(getHypothesisTagSpaceHandler() == "[[]]"):
tag = "[[" + tag + "]]"
else:
tag = tag.replace(' ', '-')
tags += "#" + tag + " "
if annotation:
outText += "##" + defaultIndentLevel + annotation
if tags:
tags += "\n"
if annotation:
outText += "###" + defaultIndentLevel + tags
else:
outText += "##" + defaultIndentLevel + tags
#print(outText)
return outText