-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuryNdays.py
115 lines (99 loc) · 4.12 KB
/
BuryNdays.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
from aqt import mw
import time
from aqt.utils import tooltip
from aqt import gui_hooks
from pathlib import Path
import json
from pprint import pprint
now = time.time()
def autobury(self):
# pprint(dir(mw.col.decks))
# print(mw.col.decks.current()['id'])
# print(c)
toBury = set()
checked = set()
count = 0
undostep = -1
for i in range(10000):
# mw.col.reset()
cards = mw.col.sched.get_queued_cards(fetch_limit=10000).cards
for item in cards:
if (item.card.id in checked):
continue
checked.add(item.card.id)
card = mw.col.get_card(item.card.id)
# print(dir(mw.col.get_note(card.nid)))
# print(card.id, card.nid)
note = card.note()
sfld=note.fields[note.note_type()["sortf"]].replace('"', '""').replace("'", "''")
# print(note.id,note.fields,note.data,note.items)
# print(sfld)
# pprint(dir(note))
# return
did = card.did
if (card.odid):
did = card.odid
newbury = mw.col.decks.config_dict_for_deck_id(did)["new"].get("bury", True)
revbury = mw.col.decks.config_dict_for_deck_id(did)["rev"].get("bury", True)
buryInterval = mw.col.decks.config_dict_for_deck_id(did).get("buryInterval", 3)
acrossDecks = mw.col.decks.config_dict_for_deck_id(did).get("acrossDecks", False)
# print(acrossDecks)
dbstr = f"""SELECT id FROM cards WHERE id != {card.id} AND
(nid = {card.nid} OR nid IN (SELECT id FROM notes WHERE sfld = "{sfld}"))"""
if (not acrossDecks):
dbstr += f""" AND did = {card.did}"""
if ((card.queue == 0 and newbury) or (card.queue == 2 and revbury)):
for cid in mw.col.db.list(dbstr):
delta = (now - mw.col.card_stats_data(cid).latest_review) / 60 / 60 / 24
if (delta < buryInterval):
toBury.add(card.id)
break
# print(len(toBury))
if (len(toBury) == 0):
break
if (undostep < 0):
mw.col.add_custom_undo_entry("Undo auto bury")
undostep = mw.col.undo_status().last_step
mw.col.sched.bury_cards(toBury, manual=False)
mw.col.merge_undo_entries(undostep)
count += len(toBury)
toBury.clear()
# print(checked)
if (count > 0):
mw.update_undo_actions()
self.refresh()
tooltip("bury " + str(count) + " card(s)")
def AnsweredQus(reviewer, card, ease):
note = card.note()
sfld = note.fields[note.note_type()["sortf"]]
did = card.did
cards = mw.col.sched.get_queued_cards(fetch_limit=10000).cards
cids = [item.card.id for item in cards]
# if (card.queue == 0):
dbstr = f"""SELECT id FROM cards WHERE did = {card.did} AND id != {card.id} AND
nid IN (SELECT id FROM notes WHERE sfld = "{sfld}")"""
newbury = mw.col.decks.config_dict_for_deck_id(did)["new"].get("bury", True)
revbury = mw.col.decks.config_dict_for_deck_id(did)["rev"].get("bury", True)
list = mw.col.db.list(dbstr)
toBury = set()
for cid in list:
if (cid in cids):
cardToLrn = mw.col.get_card(cid)
if ((cardToLrn.queue == 0 and newbury) or (cardToLrn.queue == 2 and revbury)):
toBury.add(cid)
# print(cardToLrn.note().fields)
if (len(toBury) > 0):
mw.col.sched.bury_cards(toBury, manual=False)
mw.col.merge_undo_entries(mw.col.undo_status().last_step - 1)
tooltip("bury " + str(len(toBury)) + " card(s)", y_offset=200)
gui_hooks.reviewer_did_answer_card.append(AnsweredQus)
gui_hooks.overview_did_refresh.append(autobury)
file = Path(__file__)
with open(file.with_name("raw.html"), encoding="utf8") as f:
html = f.read()
with open(file.with_name("raw.js"), encoding="utf8") as f:
script = f.read()
def optionInject(deck_options):
# print(dir(deck_options))
deck_options.web.eval(script.replace("HTML_CONTENT", json.dumps(html)))
gui_hooks.deck_options_did_load.append(optionInject)