-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyearly_stats_page.py
228 lines (176 loc) · 7.3 KB
/
yearly_stats_page.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
from flask import Blueprint, render_template
import csv
import datetime
import tools
USERNAME = tools.get_username()
def get_film_info(name, release_year, year):
film_info = {}
with open('export/data.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
if name == row[2] and release_year == int(row[1]):
uri = row[0]
watches = eval(row[10])
watches = list(filter(lambda x: x.year == year, watches))
film_info = {
'uri': uri,
'name': name,
'release_year': release_year,
'director': row[3],
'runtime': int(row[4]),
'cast': eval(row[5]),
'countries': eval(row[7]),
'languages': eval(row[8]),
'genres': eval(row[9]),
'watches': watches,
'rating': tools.get_rating(row[0]),
'poster': row[6],
}
return film_info
def get_yearly_data(year):
yearly_data = []
diary_entries = 0
rewatches = 0
time = 0
countries = {}
genres = {}
languages = {}
actors = {}
directors = {}
with open('export/diary.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
if str(year) != row[7][:4]:
continue
diary_entries += 1
name, release_year = row[1], int(row[2])
if row[5] == 'Yes':
rewatches += 1
if (name, release_year) in [(film['name'], film['release_year']) for film in yearly_data]:
continue
film_info = get_film_info(name, release_year, year)
time += film_info['runtime']*len(film_info['watches'])
rating = film_info['rating']
for c in film_info['countries']:
countries[c] = countries.get(c, 0) + 1
for c in film_info['genres']:
genres[c] = genres.get(c, 0) + 1
for c in film_info['languages']:
languages[c] = languages.get(c, 0) + 1
for c in film_info['cast']:
actors.setdefault(c, []).append(rating)
directors.setdefault(film_info['director'], []).append(rating)
yearly_data.append(film_info)
if datetime.datetime.now().year == year:
now = datetime.datetime.now()
time_data = {
'per_month': round(diary_entries/now.month, 1),
'per_week': round(diary_entries/week_from_date(now), 1)
}
else:
time_data = {
'per_month': round(diary_entries/12, 1),
'per_week': round(diary_entries/52, 1)
}
details = {
'hours':time//60,
'diary_entries': diary_entries,
'time_data': time_data,
'rewatches': rewatches,
'countries': countries,
'genres': genres,
'languages': languages,
'directors': directors,
'actors': actors,
}
return yearly_data, details
def highest_rated_films(data, year, max_current_year=8, max_older=16):
sorted_by_ratings = sorted(data, key = lambda film: film['rating'], reverse=True)
i,j = 0,0
current_year = []
older = []
for film in sorted_by_ratings:
if film['release_year'] == year:
i+=1
current_year.append(film)
if i == 8:
break
for film in sorted_by_ratings:
if film['release_year'] != year:
j+=1
older.append(film)
if j == 16:
break
return current_year, older
def get_milestones(data):
first, *_ = sorted(data, key = lambda film: sorted(film['watches'])[0])
*_, last = sorted(data, key = lambda film: sorted(film['watches'])[-1])
first_date = first['watches'][0].strftime("%b %d").replace('0', '')
last_date = last['watches'][-1].strftime("%b %d").replace('0', '')
return (first, first_date), (last, last_date)
def get_pie_info(data, year):
older_releases = 0
rewatches = 0
for film in data:
times_watched = len(film['watches'])
if times_watched > 1:
rewatches += times_watched - 1
# for times_watched:
if film['release_year'] != year:
older_releases += 1*times_watched
# print(older_releases)
return {'older_releases': older_releases, 'rewatches':rewatches}
def week_from_date(date_object):
yday = (date_object - datetime.datetime(date_object.year, 1, 1)).days + 1
return (yday // 7) + 1
def date_from_week(week, year):
start = datetime.datetime(year, 1, 1) + datetime.timedelta(days=(week-1)*7)
end = start + datetime.timedelta(days=6)
dates = start.strftime('%b %d') + '—' + end.strftime('%b %d')
return dates
def get_weekly_data(data):
weeks = {i:0 for i in range(1, 52)}
weekdays = {1:0, 2:0, 3:0, 4: 0, 5:0, 6:0, 7:0}
for film in data:
watches = film['watches']
for day in watches:
week = week_from_date(day)
weeks[week] = weeks.get(week, 0) + 1
# print(day, week, film['name'])
weekdays[day.weekday() + 1] += 1
# print(weeks)
return weeks, weekdays
yearly = Blueprint('yearly', __name__, template_folder='templates')
@yearly.route('/<year>')
def index(year):
try:
if int(year) in [2021, 2022]:
year = int(year)
yearly_data, details = get_yearly_data(year)
highest_rated_of_year, highest_rated_older = highest_rated_films(yearly_data, year)
milestones = get_milestones(yearly_data)
most_watched = sorted(yearly_data, key=lambda x: len(x['watches']), reverse=True)[:5]
actors = details['actors']
directors = details['directors']
actors_most_watched = tools.most_watched(actors, 10)
directors_most_watched = tools.most_watched(directors, 10)
actors_by_rating = tools.highest_rated(actors, 10, 3)
directors_by_rating = tools.highest_rated(directors, 10, 2)
breakdown_chart_info = tools.breakdown_charts(details['genres'],
details['countries'], details['languages'])
pie_chart_info = get_pie_info(yearly_data, year)
films_by_week, films_by_weekday = get_weekly_data(yearly_data)
map_data=tools.map_data(details['countries'])
return render_template('yearly.html', USERNAME=USERNAME, YEAR=year, details=details,
highest_rated_of_year=highest_rated_of_year,
highest_rated_older=highest_rated_older, milestones=milestones,
most_watched=most_watched, actors_most_watched=actors_most_watched,
actors_by_rating=actors_by_rating, directors_most_watched=directors_most_watched,
directors_by_rating=directors_by_rating,
breakdown_charts=breakdown_chart_info, pie_chart_info=pie_chart_info,
films_by_week=films_by_week, films_by_weekday=films_by_weekday,
map_data=map_data)
except ValueError:
return '<h1>404</h1>'