-
Notifications
You must be signed in to change notification settings - Fork 0
/
film_tools.py
executable file
·347 lines (309 loc) · 11.4 KB
/
film_tools.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/python3
import datetime
import itertools
import random
import click
import yaml
from mgmweb import film_logic
TIERS = ['I', 'II-A', 'II-B', 'II-C', 'III-A', 'III-B', 'III-C', 'IV']
FULL_TIERS = ['I', 'II-Aa', 'II-A', 'II-B', 'II-C', 'III-A', 'III-B', 'III-C', 'IV']
@click.group()
def main():
pass
@main.command()
def check():
"""Basic vetting for master yaml."""
master = get_master()
for month in master:
assert month['status'] in ('unranked', 'ranked', 'tier norank')
rankings = False
films = month['films']
for film in films:
if 'filter' in film:
continue
if 'title' in film:
assert 'year' in film
assert 'director' in film
assert type(film['director']) == list
elif 'rewatch' in film:
assert '(' in film['rewatch']
else:
raise Exception(film)
if 'ranking' in film:
rankings = True
master, error_log = film_logic.flesh_out_rewatches(master, log_errors=True)
# TODO: check stubz against being sequential, and in the correct month
# and possibly sanity checks of repeated days?
if False: # I don't use this anymore, should remove:
if rankings and not month['status'] == 'unranked':
assert all('ranking' in film for film in films if not 'filter' in film)
print('-------------RANKED-------------')
else:
print('************ UNRANKED***********')
print(len(films))
print()
def get_master():
return yaml.load(open('mgmweb/static/master.yaml'), Loader=yaml.FullLoader)
@main.command()
@click.option('--month', default='LAST', help="month to list, format='yyyy-mm'")
def current(month):
"""Print current, in-progress month, so to create new list."""
def print_month_contents(month):
print(f"#{month['month']}")
films = month['films']
for film in films:
if 'filter' in film:
continue
if 'rewatch' in film:
directors = ', '.join(film['director'])
print(f"*{film['rewatch']} ({directors}, {film['year']})")
else:
directors = ', '.join(film['director'])
print(f"{film['title']} ({directors}, {film['year']})")
print(len(films))
master = get_master()
master = film_logic.flesh_out_rewatches(master)
if month == 'LAST':
for month in master:
if month['status'] != ('in-progress'):
continue
print_month_contents(month)
else:
target = datetime.datetime.strptime(month, "%Y-%m").strftime("%B %Y")
for month in master:
if month['month'] != target:
continue
print_month_contents(month)
def get_tiers_for_year(year, master):
tiers_for_year = {tier: [] for tier in FULL_TIERS}
for month in master:
for film in month['films']:
if 'filter' in film or 'title' not in film:
continue
try:
film_year = film['effective_year']
except KeyError:
film_year = film['year']
if film_year == year:
try:
tier = film['tier']
film['month'] = month['month']
tiers_for_year[tier].append(film)
except KeyError:
print(f'No Tier for {film["title"]}')
continue
return tiers_for_year
@main.command()
@click.argument('year', default=datetime.datetime.now().year)
@click.option('--merge', is_flag=True, default=False, help="merge sort")
def year(year, merge):
"""Print all films for a year (corrected for effective year)"""
goal_year = int(year)
master = get_master()
tiers_for_year = get_tiers_for_year(goal_year, master)
total_count = sum(len(x) for x in tiers_for_year.values())
final_merged = {}
for tier, films in tiers_for_year.items():
# This is an absolute nightmare, it sorts and then uses groupby to
# bunch the same movies per tier from the same month:
def _sort_ranking_key(film):
try:
return film['ranking']
except KeyError:
return 999
months_per_tier = [
[film['title'] for film in sorted(group, key=_sort_ranking_key)]
for key, group in itertools.groupby(sorted(films,
key=lambda x: x['month']), lambda x: x['month'])]
if not months_per_tier:
final_merged[tier] = []
continue
if not merge:
print(tier)
print('\n*\n'.join('\n'.join(x) for x in months_per_tier))
else:
output = merge_sort(months_per_tier)
print(output)
final_merged[tier] = output
"""
output = []
while len(month_traunches) >= 2:
tops = {ii + 1: x[0] for ii, x in enumerate(month_traunches)}
prompt = '\n'.join(f'{ii}. {x}' for ii, x in tops.items())
print(prompt)
value = click.prompt('Pick top choice', type=int)
output.append(month_traunches[value - 1].pop(0))
month_traunches = [x for x in month_traunches if x]
output.append(month_traunches[0].pop(0))
final_merged[tier] = output
"""
if merge:
for tier, films in final_merged.items():
print(tier)
print('\n'.join(films))
print(f"Total count: {total_count}")
def merge_sort(months):
def merge_sort_two(months):
resp = []
while len(months) > 1:
tops = {ii + 1: x[0] for ii, x in enumerate(months)}
prompt = '\n'.join(f'{ii}. {x}' for ii, x in tops.items())
print(prompt)
value = click.prompt('Pick top choice', type=int)
resp.append(months[value - 1].pop(0))
months = [x for x in months if x]
resp.extend(months[0])
return resp
random.shuffle(months)
print(months)
if len(months) == 1:
return months[0]
elif len(months) == 2:
return merge_sort_two(*months)
else:
while len(months) > 2:
print(f"{len(months)} to merge")
merged = merge_sort_two(*months[:2])
months = [merged, *months[2:]]
random.shuffle(months)
return merge_sort_two(*months)
def pre_process(lines):
"""Mostly unsplit multiple tiers corresponding to (none)"""
for line in lines:
line = line.strip()
chunks = line.split(', ')
if len(chunks) == 1:
yield line
continue
if chunks[-1][-1] == ':':
chunks[-1] = chunks[-1][:-1]
if all(x in TIERS for x in chunks):
for chunk in chunks[:-1]:
yield chunk
yield ('(none)')
yield chunks[-1]
else:
yield line
def check_lists(lists):
for month, tiers in lists.items():
keys = tiers.keys()
extra = set(keys) - set(TIERS)
if extra:
raise Exception(f"extra tiers: {extra}")
missing = set(TIERS) - set(keys)
if missing:
raise Exception(f"missing in {month}: {missing}")
def get_lists(filename):
curr_month = None
resp = {}
lines = open(filename).readlines()
lines = pre_process(lines)
for line in lines:
if not line:
continue
if line.startswith('#'):
line = line[1:].strip()
month = line.capitalize()
if month[-1] == ':':
month = month[:-1]
curr_month = month
resp[curr_month] = {}
curr_tier = None
elif line[0] in ('0123456789'):
num, film = line.split('.', 1)
num = int(num)
film = film.rsplit(' (', 1)[0]
film = film.strip()
film = film.replace("’", "'")
if film[0] == '*':
film = film[1:].strip()
resp[curr_month][curr_tier].append((num, film))
elif line in TIERS:
if curr_month is None:
raise RuntimeError("Need to specify #MONTH YEAR at top of list")
curr_tier = line
if curr_tier in resp[curr_month]:
raise Exception(curr_tier, resp[curr_month])
resp[curr_month][curr_tier] = []
elif line == '(none)':
pass
else:
raise Exception(line)
return resp
def find_master_film(film_to_find, master_data):
def is_right_film(film, film_to_find):
if 'title' in film:
if film['title'] == film_to_find:
return True
elif 'rewatch' in film:
if film['rewatch'].split(' (')[0].lower() == film_to_find.lower():
return True
return False
title_find = [film for film in master_data
if is_right_film(film, film_to_find)]
if not title_find:
raise Exception(f'cannot find {film_to_find}')
assert len(title_find) == 1
return title_find[0]
@main.command()
@click.argument('filename', type=click.Path(exists=True))
def newlist(filename):
"""Feed new monthly list(s) and create new yaml master in /tmp"""
lists = get_lists(filename)
check_lists(lists)
out_master = []
master = get_master()
for master_data in master:
month_txt = master_data['month']
if month_txt not in lists:
out_master.append(master_data)
continue
print(month_txt)
ranking = lists[month_txt]
month_status = master_data['status']
master_films = master_data['films']
count = {tier: 0 for tier in TIERS}
for tier, num_films in ranking.items():
for num, film in num_films:
print(num, film)
master_film = find_master_film(film, master_films)
master_film['ranking'] = num
master_film['tier'] = tier
count[tier] += 1
master_data['tiers'] = count
if month_status == 'in-progress':
master_data['status'] = 'ranked'
elif month_status == 'tier norank':
master_data['status'] = 'ranked approx'
else:
raise NotImplementedError
out_master.append(master_data)
output_text = yaml.dump(out_master, sort_keys=False, allow_unicode=True)
print("Updated yaml document: /tmp/master_updated.yaml")
with open('/tmp/master_updated.yaml', 'w') as f:
f.write(output_text)
@main.command()
@click.argument('month', nargs=-1, required=True)
def ranking(month):
"""Print ranking for certain month."""
master = get_master()
# Important here to get info on director for rewatches:
master = film_logic.flesh_out_rewatches(master)
if month[0] == 'ALL':
for month_data in master:
if month_data['status'] != 'ranked':
continue
print(month_data['month'])
print_month(month_data)
else:
month = " ".join(month)
month_data = [x for x in master if x['month'] == month][0]
print_month(month_data)
def print_month(month_data):
tiers = film_logic.organize_month_data_into_tiers(month_data)
for tier, payload in tiers:
print(tier)
print("\n".join(payload))
print()
if __name__ == "__main__":
main()