-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoda_to_anki.py
167 lines (127 loc) · 4.45 KB
/
noda_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
import csv
import random
fields = ['Uuid','Title','ImageURL','FromUuid','ToUuid']
fields_mapped = {}
class NodaItem:
def __init__(self):
self._uuid = None
self._title = None
self._image_url = None
self._from_uuid = None
self._to_uuid = None
self._child_nodes = []
def setUUID(self, uuid):
self._uuid = uuid
def setTitle(self, title):
self._title = title
def setImageUrl(self, image_url):
self._image_url = image_url
def setFromUUID(self, from_uuid):
self._from_uuid = from_uuid
def setToUUID(self, to_uuid):
self._to_uuid = to_uuid
def getUUID(self):
return self._uuid
def getTitle(self):
return self._title
def getImageUrl(self):
return self._image_url
def getFromUUID(self):
return self._from_uuid
def getToUUID(self):
return self._to_uuid
def getChildNodes(self):
return self._child_nodes
def addChildNode(self, NodaItem):
if NodaItem.getFromUUID() == self.getUUID():
self._child_nodes.append(NodaItem.getToUUID())
class AnkiItem:
def __init__(self):
self._title = None
self._image_urls = []
self._front_html = ''
self._back_html = ''
def getTitle(self):
return self._title
def setTitle(self, title):
self._title = title
self.buildFrontHTML()
def getImageURLS(self):
return self._image_urls
def addImageURL(self, image_url):
self._image_urls.append(image_url)
self.buildBackHTML()
def getFrontHTML(self):
return self.front_html
def buildFrontHTML(self):
self.front_html = '<html>'+self.getTitle()+"</html>"
def getBackHTML(self):
return self.back_html
def buildBackHTML(self):
self.back_html = '<html>'
for img_url in self.getImageURLS():
self.back_html += '<img src='+img_url+'></img><br>'
self.back_html += '</html>'
def printCard(self):
return self.getFrontHTML() + ',' + self.getBackHTML()
def printReverseCard(self):
return self.getBackHTML() + ',' + self.getFrontHTML()
def getNodeByUUID(uuid, nodaItems):
for node in nodaItems:
if node.getUUID() == uuid:
return node
else:
pass
# Parse the Noda Export File and Create the NodaItems
nodaItems = []
with open('noda_export.csv', 'r') as noda_file:
reader = csv.reader(noda_file)
for idx, row in enumerate(reader):
if idx == 0:
for field in fields:
fields_mapped[field] = row.index(field)
else:
nodaItem = NodaItem()
for field in fields_mapped:
#print(row[fields_mapped.get(field)])
if field == 'Uuid':
try: nodaItem.setUUID(row[fields_mapped.get(field)])
except: pass
if field == 'Title':
try: nodaItem.setTitle(row[fields_mapped.get(field)])
except: pass
if field == 'ImageURL':
try: nodaItem.setImageUrl(row[fields_mapped.get(field)])
except: pass
if field == 'FromUuid':
try: nodaItem.setFromUUID(row[fields_mapped.get(field)])
except: pass
if field == 'ToUuid':
try: nodaItem.setToUUID(row[fields_mapped.get(field)])
except: pass
nodaItems.append(nodaItem)
# Add the NodaItem Child Nodes
for node_to_add_child in nodaItems:
for node_to_check_child in nodaItems:
node_to_add_child.addChildNode(node_to_check_child)
# Create the AnkiItems
ankiItems = []
for node in nodaItems:
if node.getTitle() != '' and len(node.getChildNodes()) != 0:
ankiItem = AnkiItem()
ankiItem.setTitle(node.getTitle())
for child_node_uuid in node.getChildNodes():
child_node = getNodeByUUID(child_node_uuid, nodaItems)
ankiItem.addImageURL(child_node.getImageUrl())
ankiItems.append(ankiItem)
# Print the Anki Items to a File
outputfilename = "noda_anki_deck.csv"
out = open(outputfilename, "w")
for ankiItem in ankiItems:
out.write(ankiItem.printCard()+"\n")
out.write(ankiItem.printReverseCard()+"\n")
out.close()
# Shuffle the Cards in the File
lines = open(outputfilename).readlines()
random.shuffle(lines)
open(outputfilename, 'w').writelines(lines)