This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetData.py
executable file
·532 lines (426 loc) · 19.3 KB
/
getData.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
from cProfile import run
import os
import sys
import json
import time
import pickle
import requests
from tqdm import tqdm
from merry import Merry
from copy import deepcopy
from dotmap import DotMap
from rapidfuzz import fuzz
from operator import itemgetter
from tmdbv3api import TV
from tmdbv3api import TMDb
from tmdbv3api import Movie
from tmdbv3api import Season
from tmdbv3api import Episode
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5.QtWebEngineWidgets import QWebEngineView
from joblib import Parallel, delayed
from tmdbv3api.exceptions import TMDbException
# Uncomment Bottom line if you want to download the data again
# FirstRun = True
tmdb = TMDb()
tmdb.api_key = tmdb_API_KEY = os.environ['TMDB_API_KEY']
trakt_CLIENT_ID = os.environ['TRAKT_API_KEY']
merry = Merry()
@merry._except(Exception)
def update_files_on_error():
update_files()
sys.exit()
@merry._try
def get_genres(tmdb_id, type_of_item):
if tmdb_id not in info.keys():
info[tmdb_id] = {}
if 'genres' in info.get(tmdb_id, {}).keys() and not firstRun:
genres = info[tmdb_id]['genres']
else:
genres = []
try:
if type_of_item == 'movie':
item_info = TMDbMovie.details(tmdb_id)['genres']
else:
item_info = TMDbTV.details(tmdb_id)['genres']
for genre in item_info:
genre = genre['name']
if ' & ' in genre:
genres.extend(genre.split(' & '))
else:
genres.append(genre)
except TMDbException:
pass
info[tmdb_id]['genres'] = genres
return genres
@merry._try
def get_runtime(tmdb_id, type_of_item, tmdb_show_id=0, trakt_show_id=0, season=0, episode=0):
if tmdb_id not in info.keys():
info[tmdb_id] = {}
if 'runtime' in info.get(tmdb_id, {}).keys() and not firstRun:
runtime = info[tmdb_id]['runtime']
else:
runtime = 0
try:
if type_of_item == 'movie':
runtime = TMDbMovie.details(tmdb_id)['runtime']
else:
runtime = TMDbEpisode.details(tmdb_show_id, season, episode)['runtime']
if not runtime:
url = f"https://api.trakt.tv/shows/{trakt_show_id}/seasons/{season}/episodes/{episode}?extended=full"
runtime = requests.get(url, headers=headers).json()['runtime']
except TMDbException:
pass
info[tmdb_id]['runtime'] = runtime
return runtime
@merry._try
def get_network(tmdb_id, trakt_id):
if tmdb_id not in info.keys():
info[tmdb_id] = {}
if 'network' in info.get(tmdb_id, {}).keys() and not firstRun:
id = info[tmdb_id]['network']
else:
id = None
try:
baseurl = "https://api.trakt.tv/shows/{}?extended=full"
url = baseurl.format(trakt_id)
network = requests.get(url, headers=headers).json()['network']
networks_tmdb_data = TMDbTV.details(tmdb_id)['networks']
networks_tmdb_data = {i['name']: i for i in networks_tmdb_data}
if network not in networks_tmdb_data.keys():
for tmdb_network in networks_tmdb_data:
print('fuzzy matching network')
if fuzz.ratio(network, tmdb_network) > 90 or fuzz.partial_ratio(network, tmdb_network):
network = tmdb_network
break
else:
print('Randomly Selecting Network')
network = list(networks_tmdb_data.keys())[0]
url, id = itemgetter('logo_path', 'id')(networks_tmdb_data[network])
id = str(id)
download_tmdb_image(url=url, basepath='network_icons', filename=f"{id}", size='200')
except TMDbException:
pass
if id not in networks.keys():
networks[id] = network
info[tmdb_id]['network'] = id
return id
@merry._try
def get_studios(tmdb_id):
if tmdb_id not in info.keys():
info[tmdb_id] = {}
if 'studios' in info.get(tmdb_id, {}).keys() and not firstRun:
studios = info[tmdb_id]['studios']
else:
studios = []
try:
studios_data = TMDbMovie.details(tmdb_id).production_companies
for studio in studios_data:
id, name, url = itemgetter('id', 'name', 'logo_path')(studio)
id = str(id)
studio_image_path = download_tmdb_image(url=url, basepath='studios/', filename=f"{id}", size=200)
if not studio_image_path:
continue
studios.append(id)
studioslst[id] = name
except TMDbException:
pass
info[tmdb_id]['studios'] = studios
return studios
@merry._try
def get_poster(tmdb_id, type_of_item):
if tmdb_id not in info.keys():
info[tmdb_id] = {}
if 'poster' in info.get(tmdb_id, {}).keys() and not firstRun:
poster = info[tmdb_id]['poster']
else:
poster = None
try:
if type_of_item == 'movie':
poster = TMDbMovie.details(tmdb_id).poster_path
else:
poster = TMDbTV.details(tmdb_id).poster_path
except TMDbException:
pass
info[tmdb_id]['poster'] = poster
return poster
@merry._try
def get_cast(tmdb_id, type_of_item, tmdb_show_id=0, season=0, episode=0):
if tmdb_id not in info.keys():
info[tmdb_id] = {}
if 'cast' in info.get(tmdb_id, {}).keys() and not firstRun:
item_cast = info[tmdb_id]['cast']
else:
item_cast = []
try:
if type_of_item == 'movie':
item_cast = TMDbMovie.credits(tmdb_id)['cast']
if type_of_item == 'tv':
url = f"https://api.themoviedb.org/3/tv/{tmdb_show_id}/season/{season}/episode/{episode}/credits?api_key={tmdb_API_KEY}&language=en-US"
response = requests.get(url).json()
item_cast = response.get('cast', [])
guest_cast = response.get('guest_stars', [])
item_cast.extend(guest_cast)
except TMDbException:
pass
for actor in item_cast:
name, id, gender, image = itemgetter('name', 'id', 'gender', 'profile_path')(actor)
castlist[id] = {'name': name, 'gender': gender, 'image': image}
item_cast = [actor['id'] for actor in item_cast]
info[tmdb_id]['cast'] = item_cast
return item_cast
@merry._try
def get_crew(tmdb_id, type_of_item, tmdb_show_id=0, season=0, episode=0):
if tmdb_id not in info.keys():
info[tmdb_id] = {}
if 'crew' in info.get(tmdb_id, {}).keys() and not firstRun:
item_crew = info[tmdb_id]['crew']
else:
full_crew = {}
try:
if type_of_item == 'movie':
full_crew = TMDbMovie.credits(tmdb_id)['crew']
else:
url = f"https://api.themoviedb.org/3/tv/{tmdb_show_id}/season/{season}/episode/{episode}/credits?api_key={tmdb_API_KEY}&language=en-US"
full_crew = requests.get(url).json().get('crew', {})
except (TMDbException, KeyError):
pass
item_crew = {}
have_writer = False
for _crew in full_crew:
job = _crew['job']
if job == 'Director':
name, id, image = itemgetter('name', 'id', 'profile_path')(_crew)
crewlist[id] = {'name': name, 'image': image}
item_crew[job] = item_crew.get(job, []) + [id]
elif job == 'Writer':
have_writer = True
name, id, image = itemgetter('name', 'id', 'profile_path')(_crew)
crewlist[id] = {'name': name, 'image': image}
item_crew[job] = item_crew.get(job, []) + [id]
if not have_writer:
for _crew in full_crew:
job = _crew['job']
if job == 'Screenplay':
job = 'Writer'
name, id, image = itemgetter('name', 'id', 'profile_path')(_crew)
crewlist[id] = {'name': name, 'image': image}
item_crew[job] = item_crew.get(job, []) + [id]
info[tmdb_id]['crew'] = item_crew
return item_crew
@merry._try
def get_countries(tmdb_id, trakt_id, type_of_item):
if tmdb_id not in info.keys():
info[tmdb_id] = {}
if 'countries' in info.get(tmdb_id, {}).keys() and not firstRun:
countries = info[tmdb_id]['countries']
else:
if type_of_item == 'tv':
baseurl = 'https://api.trakt.tv/shows/{}?extended=full'
else:
baseurl = 'https://api.trakt.tv/movies/{}?extended=full'
url = baseurl.format(trakt_id)
response = requests.get(url, headers=headers).json()
countries = response["country"]
info[tmdb_id]['countries'] = countries
return countries
def run_parallely(fn, items):
return Parallel(n_jobs=-2, backend='threading', require='sharedmem')(delayed(fn)(item) for item in items)
@merry._try
def parse_movie_data():
print('Parsing Movies Data')
run_parallely(process_watched_movies, tqdm(data['history']))
def process_watched_movies(item):
type = item['type']
if type == 'movie':
tmdb_id, trakt_id = itemgetter('tmdb', 'trakt')(item['movie']['ids'])
if tmdb_id not in watched_movies.keys():
watched_movies[tmdb_id] = DotMap()
watched_movies[tmdb_id]['title'] = item['movie']['title']
watched_movies[tmdb_id]['time'] = [item['watched_at']]
watched_movies[tmdb_id]['plays'] = 1
watched_movies[tmdb_id]['released_year'] = item['movie']['year']
watched_movies[tmdb_id]['id'] = str(tmdb_id)
watched_movies[tmdb_id]['type'] = type
watched_movies[tmdb_id]['trakt_id'] = trakt_id
watched_movies[tmdb_id]['rating'] = ratings[tmdb_id] if tmdb_id in ratings.keys() else 0
watched_movies[tmdb_id]['runtime'] = get_runtime(tmdb_id, 'movie')
watched_movies[tmdb_id]['genres'] = get_genres(tmdb_id, 'movie')
watched_movies[tmdb_id]['poster'] = get_poster(tmdb_id, 'movie')
watched_movies[tmdb_id]['country'] = get_countries(tmdb_id, trakt_id, 'movie')
watched_movies[tmdb_id]['studios'] = get_studios(tmdb_id)
watched_movies[tmdb_id]['cast'] = get_cast(tmdb_id, 'movie')
watched_movies[tmdb_id]['crew'] = get_crew(tmdb_id, 'movie')
else:
watched_movies[tmdb_id]['plays'] += 1
watched_movies[tmdb_id]['time'].append(item['watched_at'])
@merry._try
def parse_shows_data():
print('Parsing TV Data')
run_parallely(process_watched_episodes, tqdm(data['history']))
run_parallely(process_watched_shows, data['watched'])
def process_watched_episodes(item):
type = item['type']
if type == 'episode':
tmdb_id = item['episode']['ids']['tmdb']
tmdb_show_id, trakt_show_id = itemgetter('tmdb', 'trakt')(item['show']['ids'])
if tmdb_id not in watched_episodes.keys():
watched_episodes[tmdb_id] = DotMap()
watched_episodes[tmdb_id]['title'] = item['episode']['title']
watched_episodes[tmdb_id]['season'] = season = item['episode']['season']
watched_episodes[tmdb_id]['episode'] = episode = item['episode']['number']
watched_episodes[tmdb_id]['time'] = [item['watched_at']]
watched_episodes[tmdb_id]['plays'] = 1
watched_episodes[tmdb_id]['show'] = item['show']['title']
watched_episodes[tmdb_id]['tmdb_show_id'] = tmdb_show_id
watched_episodes[tmdb_id]['type'] = type
watched_episodes[tmdb_id]['rating'] = ratings[tmdb_id] if tmdb_id in ratings.keys() else 0
watched_episodes[tmdb_id]['runtime'] = get_runtime(tmdb_id, 'tv', tmdb_show_id=tmdb_show_id, trakt_show_id=trakt_show_id, season=season, episode=episode)
watched_episodes[tmdb_id]['cast'] = get_cast(tmdb_id, 'tv', tmdb_show_id=tmdb_show_id, season=season, episode=episode)
watched_episodes[tmdb_id]['crew'] = get_crew(tmdb_id, 'tv', tmdb_show_id=tmdb_show_id, season=season, episode=episode)
else:
watched_episodes[tmdb_id]['plays'] += 1
watched_episodes[tmdb_id]['time'].append(item['watched_at'])
def process_watched_shows(item):
if 'show' in item.keys():
tmdb_id, trakt_id = itemgetter('tmdb', 'trakt')(item['show']['ids'])
watched_shows[tmdb_id] = DotMap()
watched_shows[tmdb_id]['id'] = str(tmdb_id)
watched_shows[tmdb_id]['title'] = item['show']['title']
watched_shows[tmdb_id]['plays'] = item['plays']
watched_shows[tmdb_id]['trakt_id'] = str(trakt_id)
watched_shows[tmdb_id]['released_year'] = item['show']['year']
watched_shows[tmdb_id]['rating'] = ratings[tmdb_id] if tmdb_id in ratings.keys() else 0
watched_shows[tmdb_id]['network'] = get_network(tmdb_id, trakt_id)
watched_shows[tmdb_id]['genres'] = get_genres(tmdb_id, 'tv')
watched_shows[tmdb_id]['poster'] = get_poster(tmdb_id, 'tv')
watched_shows[tmdb_id]['country'] = get_countries(tmdb_id, trakt_id, 'tv')
@merry._try
def download_tmdb_image(url, basepath, filename, size):
url = f"https://image.tmdb.org/t/p/w{size}/{url}"
extension = url[-4:]
path = os.path.join(".cache", basepath, filename+extension)
if not os.path.exists(path):
request = requests.get(url)
if request.status_code != 200:
return False
with open(path, 'wb') as icon_file:
icon_file.write(request.content)
time.sleep(0.5)
return path
@merry._try
def get_top_shows_and_movies_lists():
if list_of_lists:
return
imdb_top_250_shows = requests.get("https://api.trakt.tv/lists/imdb-top-rated-tv-shows/items",headers=headers).json()
trakt_top_250_shows = requests.get("https://api.trakt.tv/lists/trakt-popular-tv-shows/items",headers=headers).json()
rollingstone_top_100_shows = requests.get("https://api.trakt.tv/lists/rolling-stone-s-100-greatest-tv-shows-of-all-time/items", headers=headers).json()
imdb_top_250_movies = requests.get("https://api.trakt.tv/lists/imdb-top-rated-movies/items", headers=headers).json()
trakt_top_250_movies = requests.get("https://api.trakt.tv/lists/trakt-popular-movies/items", headers=headers).json()
reddit_top_250_movies = requests.get("https://api.trakt.tv/lists/reddit-top-250-2019-edition/items",headers=headers).json()
most_played_show_trakt = requests.get("https://api.trakt.tv/shows/played/all", headers=headers).json()
most_played_movies_trakt = requests.get("https://api.trakt.tv/movies/played/all", headers=headers).json()
imdb_top_250_shows = [i['show']['ids']['tmdb'] for i in imdb_top_250_shows]
trakt_top_250_shows = [i['show']['ids']['tmdb'] for i in trakt_top_250_shows]
rollingstone_top_100_shows = [i['show']['ids']['tmdb'] for i in rollingstone_top_100_shows]
imdb_top_250_movies = [i['movie']['ids']['tmdb'] for i in imdb_top_250_movies]
trakt_top_250_movies = [i['movie']['ids']['tmdb'] for i in trakt_top_250_movies]
reddit_top_250_movies = [i['movie']['ids']['tmdb'] for i in reddit_top_250_movies]
most_played_show_trakt = {show['show']['ids']['tmdb']: show for show in most_played_show_trakt}
most_played_movies_trakt = {show['movie']['ids']['tmdb']: show for show in most_played_movies_trakt}
list_of_lists['shows'] = {
'imdb': imdb_top_250_shows,
'trakt': trakt_top_250_shows,
'rollingstone': rollingstone_top_100_shows
}
list_of_lists['movies'] = {
'imdb': imdb_top_250_movies,
'trakt': trakt_top_250_movies,
'reddit': reddit_top_250_movies
}
list_of_lists['trakt'] = {
'most_played_shows': most_played_show_trakt,
'most_played_movies': most_played_movies_trakt
}
def update_files():
if info != info_copy:
print("saving info")
file = open('info.dict', 'wb')
pickle.dump(info, file)
file.close()
if json_data != json_data_copy:
print("saving json_data")
file = open('json_data.json', 'w')
json.dump(json_data, file)
file.close()
@merry._try
def getData():
parse_movie_data()
parse_shows_data()
get_top_shows_and_movies_lists()
update_files()
return watched_movies, watched_episodes, watched_shows, len(ratings), len(data['lists']), json_data, name, joined_date
headers = {
'Content-Type': 'application/json',
'trakt-api-version': '2',
'trakt-api-key': trakt_CLIENT_ID,
}
tmdb.language = 'en'
TMDbMovie = Movie()
TMDbTV = TV()
TMDbSeason = Season()
TMDbEpisode = Episode()
watched_movies = DotMap()
watched_shows = DotMap()
watched_episodes = DotMap()
if os.path.exists('data.json'):
filename = "data.json"
else:
app = QtWidgets.QApplication(sys.argv)
messagebox = QtWidgets.QMessageBox()
messagebox.setIcon(QtWidgets.QMessageBox.Information)
messagebox.setTextFormat(QtCore.Qt.RichText)
messagebox.setText("Select full exported data file from <a href='https://github.com/seanbreckenridge/traktexport'>traktexport</a>")
messagebox.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)
if messagebox.exec_() == QtWidgets.QMessageBox.Cancel:
sys.exit()
file_dialog = QtWidgets.QFileDialog()
file_dialog.setFileMode(QtWidgets.QFileDialog.AnyFile)
filename = QtWidgets.QFileDialog.getOpenFileName(file_dialog, "Select traktexport export file", "", "json(*json)")
filename = filename[0]
app.quit()
file = open(filename, 'r')
data = json.load(file)
ratings = {}
for item in data['ratings']:
type = item['type']
item_tmdb_id = item[type]['ids']['tmdb']
ratings[item_tmdb_id] = item['rating']
if not os.path.exists('info.dict'):
with open('info.dict', 'wb') as file2:
pickle.dump(DotMap(), file2)
if not os.path.exists('json_data.json'):
with open('json_data.json', 'w') as file2:
dct = {"networks": {}, "studios": {}, "castlist": {}, "crewlist": {}, "lists": {}}
json.dump(dct, file2)
info = pickle.load(open('info.dict', 'rb'))
info_copy = info.copy()
json_data = json.load(open('json_data.json', 'r'))
json_data_copy = deepcopy(json_data)
castlist = json_data['castlist']
crewlist = json_data['crewlist']
networks = json_data['networks']
studioslst = json_data['studios']
list_of_lists = json_data['lists']
name = data['profile']['name'].split(' ')[0]
joined_date = data['history'][-1]['watched_at'].split('T')[0]
# Creating Directories
os.makedirs('.cache', exist_ok=True)
os.makedirs('.cache/cast', exist_ok=True)
os.makedirs('.cache/crew', exist_ok=True)
os.makedirs('.cache/posters', exist_ok=True)
os.makedirs('.cache/studios', exist_ok=True)
os.makedirs('.cache/network_icons', exist_ok=True)
os.makedirs('.cache/posters/processed', exist_ok=True)
firstRun = False