-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
139 lines (105 loc) · 4.02 KB
/
__init__.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import aqt
anki_version = tuple(int(segment) for segment in aqt.appVersion.split("."))
if anki_version < (2, 1, 54):
raise Exception("Minimum Anki version supported: 2.1.54")
import anki
import threading
import json
import socket
from http.server import BaseHTTPRequestHandler, HTTPServer
from aqt.qt import QAction
has_started = False
anki_add_note = None
anki_remove_notes = None
anki_update_note = None
collection = None
connections = set()
def add_note(self, note, deck_id):
changes = anki_add_note(self, note, deck_id)
broadcast_add_note(note)
return changes
def remove_notes(self, note_ids):
broadcast_remove_notes(note_ids)
return anki_remove_notes(self, note_ids)
def update_note(self, note):
# The note passed in is already updated with the new changes, so we should
# grab the note from the database for the current state
broadcast_remove_notes([note.id])
changes = anki_update_note(self, note)
broadcast_add_note(note)
return changes
def broadcast_add_note(note):
first_field_value = note.fields[0]
value = {"type": "add", "query": first_field_value, "noteId": note.id}
broadcast(json.dumps(value, separators=(",", ":"), ensure_ascii=False))
def broadcast_remove_notes(note_ids):
for note_id in note_ids:
note = collection.get_note(note_id)
first_field_value = note.fields[0]
value = {"type": "remove", "query": first_field_value, "noteId": note_id}
broadcast(json.dumps(value, separators=(",", ":"), ensure_ascii=False))
def broadcast(message):
global connections
for connection in connections.copy():
try:
connection.sendall(f"data: {message}\n\n".encode("utf-8"))
except socket.error:
connection.close()
connections.remove(connection)
class SSEHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args):
# Disable logging so that Anki does not throw an error
pass
def do_GET(self):
global connections
connections.add(self.connection)
self.send_response(200)
self.send_header("Cache-Control", "no-store")
self.send_header("Content-Type", "text/event-stream")
self.send_header("Connection", "keep-alive")
self.end_headers()
note_ids = collection.db.list("select id from notes")
for note_id in note_ids:
note = collection.get_note(note_id)
first_field_value = note.fields[0]
value = {"type": "add", "query": first_field_value, "noteId": note_id}
self.send_data(json.dumps(value, separators=(",", ":"), ensure_ascii=False))
def send_data(self, data):
message = f"data: {data}\n\n"
self.wfile.write(message.encode())
def start_sse_server(name):
server_address = ("", 12345)
httpd = HTTPServer(server_address, SSEHandler)
print("SSE server listening on port 12345...")
httpd.serve_forever()
def start_server():
global has_started
if has_started:
print("SSE server has already started")
return
# Patch anki add, update, and remove notes functions
global anki_add_note
global anki_remove_notes
global anki_update_note
global collection
anki_add_note = anki.Collection.add_note
anki_remove_notes = anki.Collection.remove_notes
anki_update_note = anki.Collection.update_note
anki.Collection.add_note = add_note
anki.Collection.remove_notes = remove_notes
anki.Collection.update_note = update_note
collection = aqt.mw.col
if collection is None:
raise Exception("collection is not available")
thread = threading.Thread(target=start_sse_server, daemon=True, args=(1,))
thread.start()
has_started = True
if __name__ != "plugin":
# Add a menu item to start the server because we need to wait for Anki to
# be fully loaded to have access to the collection
action = QAction(
"Start AnkiConnect Server-Sent Event server",
aqt.mw,
triggered=start_server,
)
aqt.mw.form.menuTools.addAction(action)