author | ymh <ymh.work@gmail.com> |
Mon, 17 Sep 2018 17:38:24 +0200 | |
changeset 7 | 5847345004db |
parent 0 | 5f4fcbc80b37 |
permissions | -rw-r--r-- |
0
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
1 |
import requests |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
2 |
import json |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
3 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
4 |
QUERY_GROUP = "gVjA5P3n" |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
5 |
GROUP = "Ri4iq687" |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
6 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
7 |
USER_TOKENS = { |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
8 |
'acct:taniki@hypothes.is': ('acct:taniki@h.projet-episteme.org', '6879-zc5yIYGFH7bovEATVDl0HL_CeNhHZYWk07wpoLQs5Ng'), |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
9 |
'acct:Iness21@hypothes.is': ('acct:Iness21@h.projet-episteme.org', '6879-y_H0QeS2laEZ09VK6KesdKzE2e0IIxyuq5kwIuckeOw'), |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
10 |
'acct:vincentpuig@hypothes.is': ('acct:vincentpuig@h.projet-episteme.org', '6879-8G7dNqQCdcRxrb4ejjwJuNKVIE5hKvOFhNpntwvvcO4') |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
11 |
} |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
12 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
13 |
DEFAULT_TOKEN = "6879-8G7dNqQCdcRxrb4ejjwJuNKVIE5hKvOFhNpntwvvcO4" |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
14 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
15 |
TAGS_MAP = { |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
16 |
'==': 'cat:commentaire', |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
17 |
'!!': 'cat:important', |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
18 |
'++': 'cat:important', |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
19 |
'+glossary': 'cat:mot-clef', |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
20 |
'??': 'cat:trouble' |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
21 |
} |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
22 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
23 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
24 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
25 |
def query_hypothesis(token=DEFAULT_TOKEN): |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
26 |
''' |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
27 |
query hypothesis |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
28 |
''' |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
29 |
url = 'https://hypothes.is/api/search' |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
30 |
headers = { |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
31 |
"Authorization" : "Bearer 6879-03KAAUnasiHITuFDIbGD9QsEu405TF4e8EREEdYrUek" |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
32 |
# "Authorization" : "Bearer %s" % token |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
33 |
} |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
34 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
35 |
params = { |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
36 |
# "group": "gVjA5P3n", |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
37 |
"group": QUERY_GROUP, |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
38 |
"limit": 200 |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
39 |
} |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
40 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
41 |
res = requests.get(url, params=params, headers=headers) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
42 |
return res.json() |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
43 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
44 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
45 |
def json_annotation(annot, user): |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
46 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
47 |
tags = [TAGS_MAP.get(t, t) for t in annot.get('tags', [])] |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
48 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
49 |
return { |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
50 |
"group": GROUP, |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
51 |
"tags": tags, |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
52 |
"target": annot['target'], |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
53 |
"text": annot['text'], |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
54 |
"uri": annot['uri'], |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
55 |
"document": annot['document'], |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
56 |
"permissions": { |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
57 |
"read": [ |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
58 |
"group:" + GROUP |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
59 |
], |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
60 |
"admin": [ |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
61 |
user |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
62 |
], |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
63 |
"update": [ |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
64 |
user |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
65 |
], |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
66 |
"delete": [ |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
67 |
user |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
68 |
] |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
69 |
} |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
70 |
} |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
71 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
72 |
def make_annotation(json_annot, token): |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
73 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
74 |
url = 'https://h.projet-episteme.org/api/annotations' |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
75 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
76 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
77 |
headers = { |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
78 |
"Authorization" : "Bearer %s" % token, |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
79 |
'Content-type': 'application/json' |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
80 |
} |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
81 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
82 |
data_str = json.dumps(json_annot) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
83 |
print("Create annotation %s" % data_str) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
84 |
res = requests.post(url, data=data_str, headers=headers) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
85 |
print("Annotation created %r" % res) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
86 |
# print("%r"%json_annot) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
87 |
print("\n") |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
88 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
89 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
90 |
def delete_annotation(annotation_id, token): |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
91 |
url = 'https://h.projet-episteme.org/api/annotations/%s' % annotation_id |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
92 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
93 |
headers = { |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
94 |
"Authorization" : "Bearer %s" % token, |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
95 |
} |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
96 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
97 |
res = requests.delete(url, headers=headers) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
98 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
99 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
100 |
if __name__ == "__main__": |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
101 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
102 |
res_annotations = query_hypothesis() |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
103 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
104 |
# for k, (v, t) in USER_TOKENS.items(): |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
105 |
# # print("USER %r, %r, %r" % (k,v,t)) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
106 |
# for annot in query_hypothesis(t)['rows']: |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
107 |
# print("ID %r - %r" % (repr(annot['id']), repr(annot['uri']))) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
108 |
# delete_annotation(annot['id'], t) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
109 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
110 |
# import json |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
111 |
# with open('res_annotations2.txt', 'w') as outfile: |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
112 |
# json.dump(res_annotations, outfile, ensure_ascii=False) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
113 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
114 |
# import json |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
115 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
116 |
# with open('filename.txt', 'r') as f: |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
117 |
# array = json.load(f) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
118 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
119 |
for annot in res_annotations['rows']: |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
120 |
user_create, token = USER_TOKENS.get(annot.get('user', ''), DEFAULT_TOKEN) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
121 |
annot_json = json_annotation(annot, user_create) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
122 |
make_annotation(annot_json, token) |
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
123 |
|
5f4fcbc80b37
Create new repository to host all dashboard developments
ymh <ymh.work@gmail.com>
parents:
diff
changeset
|
124 |