-
Notifications
You must be signed in to change notification settings - Fork 0
/
lol.py
118 lines (96 loc) · 3.48 KB
/
lol.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
import os
import sys
import json
import spotipy
import spotipy.util as util
import webbrowser
client_id = 'e7b4933edab44bd08bfc5d82dd87a6eb'
client_secret = '08b8c7911de040188eb90f3ff287cb7c'
redirect_uri = 'https://google.com/'
# Get the username from terminal
username = sys.argv[1]
scope = 'user-read-private user-read-playback-state user-modify-playback-state'
# User ID: 11169195347
# Erase cache and prompt for user permission
try:
token = util.prompt_for_user_token(username, scope, client_id, client_secret,
redirect_uri)
except:
# raise ValueError("Invalid token")
os.remove(f'.cache-{username}')
token = util.prompt_for_user_token(username, scope, client_id, client_secret,
redirect_uri)
# Create our spotifyObject
spotifyObject = spotipy.Spotify(auth=token)
# Get current device
devices = spotifyObject.devices()
# print(json.dumps(devices, sort_keys=True, indent=4))
deviceID = devices['devices'][0]['id']
# Current track information
track = spotifyObject.current_user_playing_track()
# print(json.dumps(track, sort_keys=True, indent=4))
print()
artist = track['item']['artists'][0]['name']
track = track['item']['name']
if artist != "":
print("Currently playing {} - {}".format(artist, track))
# User information
user = spotifyObject.current_user()
displayName = user['display_name']
followers = user['followers']['total']
#print(json.dumps(user, sort_keys=True, indent=4))
while True:
print()
print(">>> Welcome to Spotipy {}".format(displayName))
print()
print("0 - Search for an artist")
print("1 - exit")
print()
choice = input("Your choice: ")
# Search for the artist
if choice == "0":
print()
searchQuery = input("Artist Name: ")
print()
# Get search results
searchResults = spotifyObject.search(searchQuery,1,0,"artist")
# print(json.dumps(user, sort_keys=True, indent=4))
artist = searchResults["artists"]["items"][0]
print(artist["name"])
print(f'{str(artist["followers"]["total"])} followers.')
print(artist['genres'][0])
print()
webbrowser.open(artist['images'][0]['url'])
artistID = artist['id']
# Album and track details
trackURIs = []
trackArt = []
z = 0
# Extract album data
albumResults = spotifyObject.artist_albums(artistID)
albumResults = albumResults['items']
for item in albumResults:
print('ALBUM {}'.format(item['name']))
albumID = item['id']
albumArt = item['images'][0]['url']
# Extract track data
trackResults = spotifyObject.album_tracks(albumID)
trackResults = trackResults['items']
for item in trackResults:
print('{} : {}'.format(str(z), item['name']))
trackURIs.append(item['uri'])
trackArt.append(albumArt)
z += 1
print()
# See album art
while True:
songSelection = input("Enter a song number to see the album art and play the song (x to exit): ")
if songSelection == 'x':
break
trackSelectionList = []
trackSelectionList.append(trackURIs[int(songSelection)])
spotifyObject.start_playback(deviceID, None, trackSelectionList)
webbrowser.open(trackArt[int(songSelection)])
# End the program
if choice == "1":
break