-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackdata.py
99 lines (73 loc) · 2.71 KB
/
trackdata.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
#!/usr/bin/env python
import spotipy
import json
import os
__trackdata = None
__datafile = 'data/tracks.json'
__include_analysis = False #analyses take up A LOT of space, don't get them by default
def __load_track_data():
global __trackdata
try:
with open(__datafile, 'r') as f:
__trackdata = json.load(f)
print('loaded track data')
except:
__trackdata = dict()
print('exception')
def __save_track_data():
global __trackdata
if __trackdata is not None:
with open(__datafile, 'w') as f:
f.write(json.dumps(__trackdata))
def __fetch_missing_data(ids_to_fetch, sp:spotipy.Spotify):
global __trackdata
if __trackdata is None:
__load_track_data()
if len(ids_to_fetch) > 0:
# page the ids into groups of < 100
id_pages = [ids_to_fetch[i:i+99] for i in range(0, len(ids_to_fetch), 99)]
for ids in id_pages:
fetched_tracks = sp.tracks(tracks=ids)['tracks']
fetched_features = sp.audio_features(tracks=ids)
if __include_analysis:
fetched_analyses = []
for id in ids:
track_analysis = sp.audio_analysis(id)
track_analysis['id'] = id
fetched_analyses.append(track_analysis)
# add new data to cache
for id in ids:
__trackdata[id] = {
}
for track in fetched_tracks:
__trackdata[track['id']]['info'] = track
if __include_analysis:
for track in fetched_analyses:
__trackdata[track['id']]['analysis'] = track
for track in fetched_features:
__trackdata[track['id']]['features'] = track
print(f'new trackdata length: {len(__trackdata)}')
__save_track_data()
def get_data_for_tracks(tracklist, sp:spotipy.Spotify):
global __trackdata
if __trackdata is None:
__load_track_data()
print(f'trackdata length: {len(__trackdata)}')
# get missing data
ids_to_fetch = [id for id in tracklist if id not in __trackdata]
__fetch_missing_data(ids_to_fetch, sp)
# return the useful parts
return_data = {}
for id in tracklist:
return_data[id] = __trackdata[id]
return return_data
""" track_data = {}
cached_data = __trackdata[id]
genre_set = set()
for artist in cached_data['info']['artists']:
if 'genres' in artist:
genre_set.union(set(artist['genres']))
track_data['artist_genres'] = list(genre_set)
return_data[id] = track_data
print(return_data)
"""