-
Notifications
You must be signed in to change notification settings - Fork 0
/
anki_connect.jl
121 lines (118 loc) · 4.99 KB
/
anki_connect.jl
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
using JSON
using HTTP
const anki_addr = "http://127.0.0.1:8765"
const add_deck = "English"
function check_duplicate(word::String)
findNotes = Dict("action" => "findNotes",
"action" => "findNotes",
"version" => 6,
"params" => Dict(
"query" => "deck:$(add_deck) Word:$(word)")
)
findNotes = JSON.json(findNotes)
result = HTTP.post(anki_addr, [], findNotes)
result = JSON.parse(String(result.body))
if result["error"] !== nothing
throw(result["error"])
return
end
if length(result["result"]) > 0
if length(result["result"]) > 1
throw("too many duplicates for word : $(word["word_key"])")
return
end
notesInfo = Dict(
"action" => "notesInfo",
"version" => 6,
"params" => Dict(
"notes" => result["result"]
)
)
notesInfo = JSON.json(notesInfo)
result = HTTP.post(anki_addr, [], notesInfo)
result = JSON.parse(String(result.body))
if result["error"] !== nothing
throw(result["error"])
return
end
return result["result"][1]
else
return nothing
end
end
function add_note(feilds::Vector{String}, word::DataFrameRow{DataFrame,DataFrames.Index})
addNote = Dict(
"action" => "addNote",
"version" => 6,
"params" => Dict(
"note" => Dict( "deckName" => "$(add_deck)",
"modelName" => "Bing Dictionary",
"fields" => Dict(
"Word" => feilds[1],
"Front" => feilds[2],
"Source" => feilds[3],
"Back" => feilds[4]
),
"options" => Dict(
"allowDuplicate" => false,
"duplicateScope" => "deck",
"duplicateScopeOptions" => Dict(
"deckName" => "$(add_deck)",
"checkChildren" => false,
"checkAllModels" => false
)
),
"tags" => ["kindle","$(word["book_key"])"]
)
)
)
addNote = JSON.json(addNote)
result = HTTP.post(anki_addr, [], addNote)
result = JSON.parse(String(result.body))
if result["error"] !== nothing
throw(result["error"])
return
end
return result["result"]
end
function update_note(feilds::Vector{String}, word::DataFrameRow{DataFrame,DataFrames.Index}, duplicate::Int64, tags::Vector{Any})
if word["book_key"] ∉ tags
addTags = Dict(
"action" => "addTags",
"version" => 6,
"params" => Dict(
"notes" => [duplicate],
"tags" => "$(word["book_key"])"
)
)
addTags = JSON.json(addTags)
result = HTTP.post(anki_addr, [], addTags)
result = JSON.parse(String(result.body))
if result["error"] !== nothing
throw(result["error"])
return
end
end
updateNoteFields = Dict(
"action" => "updateNoteFields",
"version" => 6,
"params" => Dict(
"note" => Dict(
"id" => duplicate,
"fields" => Dict(
"Word" => feilds[1],
"Front" => feilds[2],
"Source" => feilds[3],
"Back" => feilds[4])
)
)
)
updateNoteFields = JSON.json(updateNoteFields)
result = HTTP.post(anki_addr, [], updateNoteFields)
result = JSON.parse(String(result.body))
if result["error"] !== nothing
throw(result["error"])
return
end
return result["result"]
end