-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhypothes.is_to_anki.py
211 lines (181 loc) · 6.28 KB
/
hypothes.is_to_anki.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import requests
import os
import json
from datetime import datetime
import subprocess
import time
import psutil # For finding and closing Anki
from email_summary import send_learning_summary #send me a summary of what i learned today
# Replace with your Hypothesis API token
API_TOKEN = "" ############### Add your Hypothes.is API key here
url = "https://hypothes.is/api/search"
# Configuration files
LAST_RUN_FILE = "last_run.txt"
PROCESSED_IDS_FILE = "processed_ids.txt"
headers = {
"Authorization": f"Bearer {API_TOKEN}",
"Content-Type": "application/json",
}
def ensure_anki_is_running():
"""
Check if AnkiConnect is reachable. If not, open Anki using full path.
"""
try:
# Try to connect to AnkiConnect
requests.get("http://localhost:8765").json()
print("Anki is already running.")
except requests.exceptions.ConnectionError:
# If AnkiConnect is not reachable, open Anki
print("Anki is not running. Opening Anki...")
# Use the full path to Anki executable for cron compatibility
subprocess.Popen(["/usr/local/bin/anki"])
# Wait for Anki to start (adjust sleep time if needed)
time.sleep(15) # Increased wait time for slower systems
def sync_anki():
"""Explicitly sync with AnkiWeb before closing"""
try:
sync_request = {
"action": "sync",
"version": 6
}
response = requests.post("http://localhost:8765", json=sync_request)
if response.json().get("error"):
print(f"Sync error: {response.json()['error']}")
else:
print("Successfully synced with AnkiWeb")
time.sleep(3) # Allow sync to complete
except Exception as e:
print(f"Sync failed: {str(e)}")
def close_anki():
"""
Sync first, then close Anki.
"""
sync_anki() # Sync before closing
# Find and terminate the Anki process
for proc in psutil.process_iter(['pid', 'name']):
if proc.info['name'] == 'anki':
print(f"Closing Anki (PID: {proc.info['pid']})...")
proc.terminate() # Gracefully terminate Anki
time.sleep(5) # Give OS time to process termination
break
def read_last_run():
"""
Read the last run timestamp from file.
"""
try:
with open(LAST_RUN_FILE, "r") as f:
return f.read().strip()
except FileNotFoundError:
return "2000-01-01T00:00:00.000000+00:00" # Default old date
def write_last_run(timestamp):
"""
Write the last run timestamp to file.
"""
with open(LAST_RUN_FILE, "w") as f:
f.write(timestamp)
def read_processed_ids():
"""
Read the set of processed annotation IDs from file.
"""
try:
with open(PROCESSED_IDS_FILE, "r") as f:
return set(line.strip() for line in f)
except FileNotFoundError:
return set()
def write_processed_ids(ids):
"""
Write the set of processed annotation IDs to file.
"""
with open(PROCESSED_IDS_FILE, "w") as f:
for ann_id in ids:
f.write(f"{ann_id}\n")
def create_anki_note(front, back, tags):
"""
Create an Anki note payload.
"""
return {
"action": "addNote",
"version": 6,
"params": {
"note": {
"deckName": "Hypothes.is",
"modelName": "Basic",
"fields": {
"Front": front,
"Back": back
},
"tags": tags
}
}
}
# Ensure Anki is running before proceeding
ensure_anki_is_running()
# Read previous state
last_run = read_last_run()
processed_ids = read_processed_ids()
new_processed_ids = set()
flashcards_created = [] # Initialize flashcards list
# Hypothesis API parameters
params = {
"group": "", ############### Add your Hypothes.is group ID here
"limit": 200,
"sort": "created",
"order": "asc",
"query": f'created:>="{last_run}"'
}
# Fetch annotations from Hypothesis API
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
annotations = data.get("rows", [])
if annotations:
# Update last_run to the newest annotation's creation time
new_last_run = annotations[-1]["created"]
write_last_run(new_last_run)
for ann in annotations:
ann_id = ann.get("id")
if ann_id in processed_ids:
continue
# Extract text quote
exact_text = "N/A"
for selector in ann.get("target", [{}])[0].get("selector", []):
if selector.get("type") == "TextQuoteSelector":
exact_text = selector.get("exact", "N/A")
break
# Prepare Anki card
front = ann.get("text", "No text")
back = f"{exact_text}\n\nSource: <a href='{ann.get('uri', '#')}'>Link</a>"
tags = ann.get("tags", []) + ["Hypothesis"] # Add custom tag
note = create_anki_note(front, back, tags)
# Send to AnkiConnect
try:
anki_response = requests.post("http://localhost:8765", json=note)
if anki_response.json().get("error"):
print(f"Error adding note: {anki_response.json()['error']}")
else:
new_processed_ids.add(ann_id)
# Add to flashcards list for email
flashcards_created.append({
'front': front,
'back': back,
'tags': tags
})
except requests.exceptions.ConnectionError:
print("Could not connect to AnkiConnect. Is Anki running?")
break
# Update processed IDs
processed_ids.update(new_processed_ids)
write_processed_ids(processed_ids)
print(f"Successfully processed {len(new_processed_ids)} new annotations")
else:
print(f"Hypothesis API Error: {response.status_code} - {response.text}")
# Email summary
if flashcards_created:
if send_learning_summary("[email protected]", flashcards_created): ############### Add your gmail address here (the one you want to receive summaries in it)
print("Successfully sent learning summary email")
else:
print("Failed to send email summary")
else:
print("No new flashcards to email")
# Close Anki after processing
close_anki()