-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEDASpotify.py
executable file
·195 lines (95 loc) · 3.67 KB
/
EDASpotify.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
# -*- coding: utf-8 -*-
"""
Spyder Editor
"""
import os
import fnmatch
import datetime
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# -----------------------------
# CORE Functions
# -----------------------------
def files_to_df(path, pattern = 'StreamingHistory*'):
listofdfs = []
for path,dirs,files in os.walk(path):
for f in fnmatch.filter(files, pattern):
fullname = os.path.abspath(os.path.join(path,f))
listofdfs.append(pd.read_json(fullname))
return pd.concat(listofdfs)
def group_artist_rank(data, limit = 20, verbose = False):
artistData = data.groupby('artistName').sum()
artistData = artistData.sort_values('msPlayed', ascending=False)
sample = artistData.head(limit)
sample.reset_index(level=0, inplace=True)
if verbose:
print(sample)
return sample
def group_by_time(data, limit = 31, verbose = False):
timeData = data.groupby('date').sum()
sample = timeData.head(limit)
sample.reset_index(level=0, inplace=True)
if verbose:
print(sample)
return sample
def plot_bar_time(sample):
dates = sample['date']
y_pos = np.arange(len(dates))
plt.bar(y_pos, sample['playTime_min'], align='center', color='coral')
plt.xticks(y_pos, dates, rotation=90)
plt.ylabel('Time Played (mins)')
plt.title('Dates')
hours = (int(np.max(sample['playTime_min']))/1000)%60
while hours > 1:
plt.axhline(hours*1000)
hours -=1
plt.show()
def plot_bar_frequency(sample):
artists = sample['artistName']
y_pos = np.arange(len(artists))
plt.bar(y_pos, sample['playTime_min'], align='center', color='coral')
plt.xticks(y_pos, artists, rotation=90)
plt.ylabel('Time Played (mins)')
plt.title('Artist')
hours = (int(np.max(sample['playTime_min']))/1000)%60
while hours > 1:
plt.axhline(hours*1000)
hours -=1
plt.show()
def preprocess(data):
# Transforms ms to hours:minutes:seconds
data['playTime'] = data.msPlayed.apply(to_time_format)
data['playTime_min'] = data.msPlayed.apply(to_mins)
# Split to ease grouping by time
data[['date', 'time']] = data['endTime'].str.split(" ", expand=True)
data = data.drop(columns =['endTime'])
return data
# -----------------------------
# Support Functions
# -----------------------------
def to_time_format(millis, formattime ='%H:%M:%S'):
return datetime.datetime.fromtimestamp(millis).strftime(formattime)
def to_mins(millis):
return (int(millis)/(1000*60))%60
if __name__ == '__main__':
test_path = '/Users/raulcoroban/Documents/Projects/Spotify Data'
full_data = files_to_df(test_path)
pro_data = preprocess(full_data)
# Artist overall ranking
data_artist = group_artist_rank(pro_data)
plot_bar_frequency(data_artist)
# Artists over the time (barplot)
data_time = group_by_time(pro_data)
plot_bar_time(data_time)
# CUSTOM artist over time
# Artists number of songs, albums
# Days of the week streaming time
# - Check if all days are consecutive
# - Add times
# Days of the week streaming artists
# Days of the week streaming CUSTOM artist
# Months streaming time
# Months streaming artists
# Months streaming CUSTOM artist
# Time played during the year (highlight lockdown)