-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (101 loc) · 2.8 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"golang.design/x/clipboard"
"encoding/csv"
)
var sentenceField string
var sentenceReadingField string
var filePath string
var deckName string
const (
AnkiDojoWordColumn = 0
AnkiDojoSentenceColumn = 3
)
func init() {
flag.Usage = func() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, "Usage: %s [options] file_path deck\n", os.Args[0])
flag.PrintDefaults()
}
const (
defaultSentenceField = "Sentence"
defaultSentenceReadingField = "SentenceReading"
)
flag.StringVar(&sentenceField, "sentence", defaultSentenceField, "Name of your note type's sentence field")
flag.StringVar(&sentenceField, "s", defaultSentenceField, "shorthand for -sentence")
flag.StringVar(&sentenceReadingField, "sentence_reading", defaultSentenceReadingField, "Name of your note type's sentence reading field")
flag.StringVar(&sentenceReadingField, "r", defaultSentenceReadingField, "shorthand for -sentence_reading")
flag.Parse()
// Retrieve arguments
if flag.NArg() < 2 {
flag.Usage()
os.Exit(1)
}
filePath = flag.Arg(0)
deckName = flag.Arg(1)
}
func main() {
// Create map from words to sentences
wordSentenceMap, err := createWordSentenceMapFromAnkiDojoExport()
// Make array of just words
keys := make([]string, 0, len(wordSentenceMap))
for k := range wordSentenceMap {
keys = append(keys, k)
}
// Copy list of words separated by newlines to clipboard
err = clipboard.Init()
if err != nil {
panic(err)
}
clipboard.Write(clipboard.FmtText, []byte(strings.Join(keys, "\n")))
fmt.Println("Word list has been copied to the clipboard. Paste it into Yomitan's word generator and send the words to Anki.")
fmt.Println("Press enter to continue once you've done the above...")
// Wait for input
fmt.Scanln()
for word, sentence := range wordSentenceMap {
id, err := FindNoteID(word, deckName)
if err != nil {
log.Printf("Failed to find note:\n%v\n", err)
continue
}
if sentenceReadingField != "" {
err = UpdateNoteFields(id, map[string]interface{}{
sentenceField: sentence,
sentenceReadingField: "",
})
} else {
err = UpdateNoteFields(id, map[string]interface{}{
sentenceField: sentence,
})
}
if err != nil {
log.Printf("Failed to update note:\n%v\n", err)
}
}
}
// Returns map of words to sentences given the path of an AnkiDojo CSV file
func createWordSentenceMapFromAnkiDojoExport() (map[string]string, error) {
// Read file
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
r := csv.NewReader(f)
data, err := r.ReadAll()
if err != nil {
return nil, err
}
data = data[1:] // Discard first row
// Create map
m := make(map[string]string)
for _, row := range data {
m[row[AnkiDojoWordColumn]] = row[AnkiDojoSentenceColumn]
}
return m, nil
}