-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpotifyTopData.py
69 lines (51 loc) · 2.03 KB
/
SpotifyTopData.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
from collections import Counter
import spotipy
import os
import sys
import json
import webbrowser
import spotipy.util as util
from json.decoder import JSONDecodeError
# Get the username from terminal
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print("Usage: %s username" % (sys.argv[0],))
sys.exit()
scope = 'user-top-read user-read-private user-read-playback-state user-modify-playback-state'
# Erase the cache and prompt for user permission
try:
token = util.prompt_for_user_token(username, scope)
except:
os.remove(f".cache-{username}")
token = util.prompt_for_user_token(username, scope)
if token:
# Create the spotifyObject
sp = spotipy.Spotify(auth=token)
sp.trace = False
# Set list for checked time ranges
ranges = ['short_term', 'medium_term', 'long_term']
# Now we define a dictionary pairing the ranges to more concrete time intervals.
timeInterval = {'short_term':'this month ','medium_term':'this year ','long_term':'all time '}
for range in ranges:
# Initialize an empty list for the genres.
genres = []
print("range:", timeInterval[range])
# Get the top 50 artists listened to during this time range.
artistsResults = sp.current_user_top_artists(time_range=range, limit = 50)
# Get the top 50 tracks listened to during this time range.
trackResults = sp.current_user_top_tracks(time_range=range, limit = 50)
print('Artists')
for i, item in enumerate(artistsResults['items']):
# Add in all the different genres covered by these artists
genres += item['genres']
print(str(i + 1)+":",item['name'])
print()
print('Tracks')
for i, item in enumerate(trackResults['items']):
print(str(i + 1)+":", item['artists'][0]['name']+" - "+item['name'])
print()
print("Your most listented to genre of "+timeInterval[range] +"is "+ Counter(genres).most_common(1)[0][0] + "!")
print()
else:
print("Can't get token for", username)