-
Notifications
You must be signed in to change notification settings - Fork 3
/
create-deck.py
129 lines (110 loc) · 4.03 KB
/
create-deck.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
import genanki
import csv
from staticmap import StaticMap, CircleMarker
import os
import glob
from shutil import copyfile
def get_note_model():
guess_country_answer_html = """
<p><b>{{Country}}</b>, {{Region}}</p>
<p style="opacity:0.75;filter:grayscale(100%)">{{Map}}</p>"""
guess_location_answer_html = """
<p>{{Map}}</p>"""
return genanki.Model(
6677382317,
'City of the world',
fields=[
{'name': 'Rank'},
{'name': 'City'},
{'name': 'Province'},
{'name': 'Country'},
{'name': 'Region'},
{'name': 'Continent'},
{'name': 'Latlong'},
{'name': 'Country code'},
{'name': 'Country code alt'},
{'name': 'Population'},
{'name': 'Map'}
],
templates=[
{
'name': 'Guess the country',
'qfmt': '<p class="light">Country of...?</p> <p><b>{{City}}</b></p>',
'afmt': '{{FrontSide}} <hr id="answer"> '+guess_country_answer_html,
},
{
'name': 'Guess the location',
'qfmt': '<p class="light" style="color:blue;">Location of...?</p> <p><b>{{City}}</b>, {{Province}}, {{Country}}</p>',
'afmt': '{{FrontSide}} <hr id="answer"> '+guess_location_answer_html,
},
],
css="""
.card {
font-family: arial;
font-size: 18px;
text-align: left;
color: black;
background-color: white;
line-height:1.45;
text-align:center;
background-color:#f9f9f9;
}
.small {
font-size:13px;
font-weight:bold;
}
.light {
color:#b0b0b5;
}
img {
border:2px solid white;
}
""")
def make_map_image(geoname_id, lat, long):
img_file_name = geoname_id+'.png'
img_archive_file = 'maps/'+img_file_name
if os.path.isfile(img_file_name):
return img_file_name
if not os.path.isfile(img_archive_file):
m = StaticMap(550, 300, url_template='http://a.tile.osm.org/{z}/{x}/{y}.png')
marker_outline = CircleMarker((long, lat), 'white', 16)
marker = CircleMarker((long, lat), '#0036FF', 10)
m.add_marker(marker_outline)
m.add_marker(marker)
image = m.render(zoom=4)
image.save(img_archive_file)
if os.path.isfile(img_archive_file):
copyfile(img_archive_file, img_file_name)
return img_file_name
raise Exception('Image not found!')
def set_notes(my_model, my_deck, files):
with open('world_cities_geoname.csv', newline='', encoding='utf-8') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count > 0:
img_file_name = make_map_image(row[1], float(row[5]), float(row[6]))
files.append(img_file_name)
latlong = '{0}, {1}'.format(row[5], row[6])
if row[12] == 'NULL':
row[12] = ''
img_field_val = '<img src="'+img_file_name+'">'
my_note = genanki.Note(
model=my_model,
fields=[str(line_count), row[2], row[12], row[7], row[8], row[9], latlong, row[10], row[11], row[4], img_field_val])
my_deck.add_note(my_note)
line_count = line_count+1
def clean_up_temp_images():
for f in glob.glob("./*.png"):
os.remove(f)
def create_deck():
field_media_files = list()
model = get_note_model()
deck = genanki.Deck(5079708189, 'Cities of the world');
set_notes(model, deck, field_media_files)
package = genanki.Package(deck)
package.media_files = field_media_files
package.write_to_file('cities-of-the-world.apkg')
clean_up_temp_images()
if __name__ == "__main__":
create_deck();