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