forked from ptwikis/wikiloves
-
Notifications
You must be signed in to change notification settings - Fork 6
/
functions.py
231 lines (198 loc) · 6.42 KB
/
functions.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# -*- coding: utf-8 -*-
EVENTS = [
"earth",
"monuments",
"africa",
"public_art",
"science",
"food",
"folklore",
]
def get_country_data(db):
country_data = {}
for edition_slug in db:
scope_slug = edition_slug[:-4]
year = edition_slug[-4:]
for country in db[edition_slug]:
country_data.setdefault(country, {}).setdefault(scope_slug, {}).update(
{
year: {
"count": db[edition_slug][country]["count"],
"usercount": db[edition_slug][country]["usercount"],
"usage": db[edition_slug][country]["usage"],
"userreg": db[edition_slug][country]["userreg"],
}
}
)
return country_data
def get_events_data(db):
return {
name: {
e[-4:]: {
"count": sum(db[e][c]["count"] for c in db[e]),
"usercount": sum(db[e][c]["usercount"] for c in db[e]),
"userreg": sum(db[e][c]["userreg"] for c in db[e]),
"usage": sum(db[e][c]["usage"] for c in db[e]),
"country_count": len(db[e]),
}
for e in db
if e[:-4] == name
}
for name in set(e[:-4] for e in db)
}
def get_edition_data(db, edition_slug):
return {
country: {
field: db[edition_slug][country][field]
for field in db[edition_slug][country]
if field != "users"
}
for country in db[edition_slug]
}
def get_instance_users_data(db, edition_slug, country):
return sorted(
list(db[edition_slug][country]["users"].items()),
key=lambda i: (i[1]["count"], i[0]),
reverse=True,
)
def get_menu(db):
return {
name: sorted(e[-4:] for e in db if e[:-4] == name)
for name in set(e[:-4] for e in db)
}
def get_country_summary(country_data):
return {
c: [
(
sorted(country_data[c][event].keys())
if event in country_data[c]
else None
)
for event in EVENTS
]
for c in country_data
}
def normalize_country_name(country_name):
return country_name.replace("_", " ")
def get_event_name(event_slug):
"""
Generate a name from the label.
Returns title case with underscore replaced.
"""
default = "Wiki Loves %s" % event_slug.replace("_", " ").title()
return event_exceptions.get(event_slug.lower(), default)
def get_edition_name(scope_slug, year):
default = "%s %s" % (get_event_name(scope_slug), year)
return edition_exceptions.get((scope_slug.lower(), str(year)), default)
def get_instance_name(scope_slug, year, country):
return "%s %s in %s" % (get_event_name(scope_slug), year, country)
def get_wikiloves_category_name(event_slug, year, country):
if (event_slug, year, country) in special_exceptions:
return special_exceptions[(event_slug, year, country)]
edition = get_edition_name(event_slug, year)
template = get_event_category_template()
country_name = catExceptions.get(country, country)
return template.format(edition=edition, country=country_name).replace(" ", "_")
def get_event_category_template():
return "Images_from_{edition}_in_{country}"
event_exceptions = {
"science": "Wiki Science Competition",
}
catExceptions = {
"Netherlands": "the_Netherlands",
"Central African Republic": "the_Central_African_Republic",
"Comoros": "the_Comoros",
"Czech Republic": "the_Czech_Republic",
"Democratic Republic of the Congo": "the_Democratic_Republic_of_the_Congo",
"Republic of the Congo": "the_Republic_of_the_Congo",
"Dutch Caribbean": "the_Dutch_Caribbean",
"Philippines": "the_Philippines",
"Seychelles": "the_Seychelles",
"United Arab Emirates": "the_United_Arab_Emirates",
"United Kingdom": "the_United_Kingdom",
"United States": "the_United_States",
}
edition_exceptions = {
("science", "2015"): "European_Science_Photo_Competition_2015",
}
special_exceptions = {
(
"Monuments",
"2024",
"Austria",
): "Media_from_WikiDaheim_2024_in_Austria/Cultural_heritage_monuments",
(
"Monuments",
"2023",
"Austria",
): "Media_from_WikiDaheim_2023_in_Austria/Cultural_heritage_monuments",
(
"Monuments",
"2022",
"Austria",
): "Media_from_WikiDaheim_2022_in_Austria/Cultural_heritage_monuments",
(
"Monuments",
"2021",
"Austria",
): "Media_from_WikiDaheim_2021_in_Austria/Cultural_heritage_monuments",
(
"Monuments",
"2020",
"Austria",
): "Media_from_WikiDaheim_2020_in_Austria/Cultural_heritage_monuments",
(
"Monuments",
"2019",
"Austria",
): "Media_from_WikiDaheim_2019_in_Austria/Cultural_heritage_monuments",
(
"Monuments",
"2018",
"Austria",
): "Media_from_WikiDaheim_2018_in_Austria/Cultural_heritage_monuments",
(
"Monuments",
"2017",
"Austria",
): "Media_from_WikiDaheim_2017_in_Austria/Cultural_heritage_monuments",
(
"Monuments",
"2015",
"Armenia",
): "Images_from_Wiki_Loves_Monuments_2015_in_Armenia_&_Nagorno-Karabakh",
(
"Monuments",
"2017",
"Armenia",
): "Images_from_Wiki_Loves_Monuments_2017_in_Armenia_&_Nagorno-Karabakh",
("Earth", "2014", "Armenia"): "Images_from_Wiki_Loves_Earth_2014_in_Armenia_&_Nagorno-Karabakh",
(
"Earth",
"2021",
"United Arab Emirates",
): "Images_from_Wiki_Loves_Earth_2021_in_United_Arab_Emirates",
(
"Earth",
"2022",
"United Arab Emirates",
): "Images_from_Wiki_Loves_Earth_2022_in_United_Arab_Emirates",
(
"Earth",
"2022",
"Democratic Republic of the Congo",
): "Images_from_Wiki_Loves_Earth_2022_in_DR_Congo",
(
"Earth",
"2023",
"Democratic Republic of the Congo",
): "Images_from_Wiki_Loves_Earth_2023_in_DR_Congo",
(
"Science",
"2015",
"Armenia",
): "Images_from_European_Science_Photo_Competition_2015_in_Armenia",
("Science", "2013", "Estonia"): "Images_from_Teadusfoto_2013",
("Science", "2012", "Estonia"): "Images_from_Teadusfoto_2012",
("Science", "2011", "Estonia"): "Images_from_Teadusfoto_2011",
}