Varia library working group XPPL.
https://gitea.xpub.nl/XPUB/XPPL
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.8 KiB
86 lines
2.8 KiB
import json
|
|
|
|
import requests
|
|
from flask import request
|
|
|
|
|
|
def get_annotations():
|
|
# TODO(decentral1se): remove this from the source
|
|
KEY = "6879-H5GbA2KyFIeEUM1BubjVTLP8yM3GMyHgOAjjNe_GBxY"
|
|
|
|
# a dictionary containing necessary http headers
|
|
headers = {
|
|
"Host": "hypothes.is",
|
|
"Accept": "application/json",
|
|
"Authorization": "Bearer %s" % KEY
|
|
}
|
|
|
|
base_url = "https://hypothes.is/api/search?user=varia_annotator@hypothes.is"
|
|
|
|
search_url = "".join([base_url])
|
|
|
|
r = requests.get(search_url, headers=headers)
|
|
|
|
# data is a python dictionary
|
|
data = json.loads(r.text)
|
|
server = request.host
|
|
|
|
for row in data['rows']:
|
|
row['uri'] = row['uri'].replace(
|
|
'http://' + server + '/uploads/',
|
|
''
|
|
)
|
|
|
|
return data
|
|
|
|
|
|
def get_annot_results(annot, name):
|
|
res = []
|
|
annot = get_annotations()
|
|
for item in annot['rows']:
|
|
if 'selector' in item['target'][0]:
|
|
if len(item['target'][0]['selector']) > 2:
|
|
exact_second = item['target'][0]['selector'][2]['exact']
|
|
if name in item['text'] or name in exact_second:
|
|
data = {
|
|
'text': item['text'],
|
|
'extract': item['target'][0]['selector'][2]['exact'],
|
|
'title': 'try me', 'url': item['uri']
|
|
}
|
|
res.append(data)
|
|
else:
|
|
exact_first = item['target'][0]['selector'][1]['exact']
|
|
if name in item['text'] or name in exact_first:
|
|
data = {
|
|
'text': item['text'],
|
|
'extract': item['target'][0]['selector'][1]['exact'],
|
|
'title': 'try me', 'url': item['uri']
|
|
}
|
|
res.append(data)
|
|
return res
|
|
|
|
|
|
def get_annot_book(annot, name):
|
|
res = []
|
|
server = request.host
|
|
for item in annot['rows']:
|
|
if 'selector' in item['target'][0]:
|
|
if len(item['target'][0]['selector']) > 2:
|
|
string = item['uri']
|
|
if name == string.replace('http://' + server+'/uploads/', ''):
|
|
data = {
|
|
'text': item['text'],
|
|
'extract': item['target'][0]['selector'][2]['exact'],
|
|
'title': item['uri'], 'url': item['uri']
|
|
}
|
|
res.append(data)
|
|
else:
|
|
string = item['uri']
|
|
if name == string.replace('http://' + server+'/uploads/', ''):
|
|
data = {
|
|
'text': item['text'],
|
|
'extract': item['target'][0]['selector'][1]['exact'],
|
|
'title': item['uri'], 'url': item['uri']
|
|
}
|
|
res.append(data)
|
|
return res
|
|
|