-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify_script.py
More file actions
102 lines (74 loc) · 3.64 KB
/
spotify_script.py
File metadata and controls
102 lines (74 loc) · 3.64 KB
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
import json
input_files = [ 'StreamingHistory_music_0.json', #these are all the streaming history files, change as needed
'StreamingHistory_music_1.json',
'StreamingHistory_music_2.json',
'StreamingHistory_music_3.json',
'StreamingHistory_music_4.json',
'StreamingHistory_music_5.json',
'StreamingHistory_music_6.json'
]
aggregate_data = {} #dictionary for all the data
def getting_info(input_files):
combined_data = [] # List to hold data from all files
for input_file in input_files:
with open(input_file, 'r') as file: # Open each file in read mode
data = json.load(file) # Load JSON data from the file
if isinstance(data, list): # Ensure the file contains a list of entries
combined_data.extend(data)
return combined_data
def reading_info(combined_data):
for entry in combined_data:
artist = entry.get('artistName', 'unknown')
track = entry.get('trackName', 'unknown')
ms_played = entry.get('msPlayed', 0)
key = (artist, track)
if key in aggregate_data:
aggregate_data[key] += ms_played
else:
aggregate_data[key] = ms_played
return aggregate_data
def write_song_info(aggregate_data):
with open('output.json', 'w') as output_file:
#opens output.json in writing mode
output_data = []
total_time = 0
total_songs = 0
top_artists = {}
for (artist, track), total_ms_played in aggregate_data.items():
min_played = round(total_ms_played / 60000, 2) # converts ms to minutes, rounded to 2 decimal places
# Add song info to the output_data
output_data.append({
"Name": artist,
"Song": track,
"Minutes Played": min_played
})
# Accumulate total time and total songs
total_time += min_played
total_songs += 1
# Accumulate artist's total play time in top_artists
if artist in top_artists:
top_artists[artist] += min_played
else:
top_artists[artist] = min_played
# Sort the songs by 'Minutes Played' in descending order
output_data = sorted(output_data, key=lambda x: x['Minutes Played'], reverse=True)
# Write the song info to output.json
json.dump(output_data, output_file, indent=4, ensure_ascii=False)
return total_time, total_songs, top_artists
def write_total_time(total_time, total_songs, top_artists):
#writes down the total time and other info for your "wrapped"
with open('wrapped.json', 'w') as total_time_file:
sorted_artists = sorted(top_artists.items(), key=lambda x: x[1], reverse=True)
top_5_artists = sorted_artists[:5]
json.dump({"Total Time": round(total_time, 2),
"Total Songs": total_songs,
"Top 5 Artist": top_5_artists
}, total_time_file, indent=4)
def main_func(input_files):
#main function, calls all the other ones
combined_data = getting_info(input_files)
aggregate_data = reading_info(combined_data)
total_time, total_songs, top_artists = write_song_info(aggregate_data)
write_total_time(total_time, total_songs, top_artists)
#runs the main function
main_func(input_files)