-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzotero.py
59 lines (52 loc) · 1.79 KB
/
zotero.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
# Syncing with zotero servers. This must be run in a separate process.
# The relevant documentation is at:
# https://www.zotero.org/support/dev/web_api/v3/streaming_api
import logging
# Documentation at https://websockets.readthedocs.io
from websockets.sync.client import connect # pip install websockets
from dharma import common, change, biblio
create_subscriptions = common.to_json({
"action": "createSubscriptions",
"subscriptions": [{
"apiKey": biblio.MY_API_KEY,
# The second group is a dummy one for testing.
"topics": [f"/groups/{biblio.LIBRARY_ID}", "/groups/5336269"]
}]
})
# Response:
# {"event":"","subscriptions":[{"apiKey":"ojTBU4SxEQ4L0OqUhFVyImjq","topics":["/groups/1633743","/groups/5336269"]}],"errors":[]}
# After an update we receive:
# {"event":"topicUpdated","topic":"/groups/5336269","version":6}
delete_subscriptions = common.to_json({
"action": "deleteSubscriptions",
"subscriptions": [{"apiKey": biblio.MY_API_KEY}]
})
# Response:
# {"event":"subscriptionsDeleted","subscriptions":[{"apiKey":"ojTBU4SxEQ4L0OqUhFVyImjq","topics":["/groups/1633743","/groups/5336269"]}]}
def read_message(sock):
ret = sock.recv()
logging.info(ret)
return common.from_json(ret)
def main(sock):
sock.send(create_subscriptions)
ret = read_message(sock)
assert ret["event"] == "subscriptionsCreated"
while True:
ret = read_message(sock)
assert ret["event"] in ("topicUpdated", "topicAdded", "topicRemoved")
change.notify("bib")
if __name__ == "__main__":
exit = False
while not exit:
sock = connect("wss://stream.zotero.org")
try:
ret = read_message(sock)
# Should receive {"event": "connected", "retry": 10000}
assert ret["event"] == "connected"
logging.info("connected")
main(sock)
except KeyboardInterrupt:
exit = True
finally:
sock.close()
logging.info("exiting")