-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzotero_proxy.py
More file actions
73 lines (65 loc) · 1.96 KB
/
zotero_proxy.py
File metadata and controls
73 lines (65 loc) · 1.96 KB
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 time, sys
from urllib.parse import urlparse, parse_qs, urlencode
import flask # pip install flask
import requests # pip install requests
from dharma import common
app = flask.Flask(__name__)
def copy_response(r):
ret = flask.Response(r.text)
v = r.headers.get("Content-Type")
if v:
ret.headers["Content-Type"] = v
return ret
def next_request_delay(r):
wait = -1
n = r.headers.get("Backoff", "")
if n.isdigit():
wait = max(wait, int(n))
n = r.headers.get("Retry-After", "")
if n.isdigit():
wait = max(wait, int(n))
return wait
MY_API_KEY = "ojTBU4SxEQ4L0OqUhFVyImjq"
def do_reply(url):
url = url._replace(scheme="https", netloc="api.zotero.org")
url = url.geturl()
s = requests.Session()
s.headers["Zotero-API-Version"] = "3"
s.headers["Zotero-API-Key"] = MY_API_KEY
for i in range(5):
r = s.get(url)
if r.ok:
return copy_response(r)
wait = next_request_delay(r)
# Must be noted that zotero servers return 500 and no request
# delay when the server is overloaded, thus we retry several
# times even if no request delay is given.
if wait <= 0:
wait = 5
time.sleep(wait)
raise Exception(repr((r.headers, r.text)))
@app.get("/groups/1633743/items")
def reply():
url = urlparse(flask.request.url)
return do_reply(url)
@app.get("/extra")
@common.transaction("texts")
def by_short_title():
short_title = flask.request.args["shortTitle"]
db = common.db("texts")
(key,) = db.execute("select key from biblio_data where short_title = ?",
(short_title,)).fetchone() or (None,)
url = urlparse(flask.request.url)
params = parse_qs(url.query)
del params["shortTitle"]
assert not params.get("itemKey")
if key:
params["itemKey"] = [key]
else:
params["tag"] = ["thiswontmatchxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"]
params = urlencode(params, doseq=True)
url = url._replace(path="/groups/1633743/items", query=params)
print(url.geturl(), file=sys.stderr)
return do_reply(url)
if __name__ == "__main__":
app.run(host="localhost", port=8024, debug=True)