-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify_music_player.py
More file actions
104 lines (85 loc) · 3.07 KB
/
spotify_music_player.py
File metadata and controls
104 lines (85 loc) · 3.07 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
103
104
import requests
import webbrowser
import spotipy
client_id = 'your_spotify_client_id'
client_secret = 'your_spotify_client_secret'
# auth_header = {
# 'Authorization': 'Basic ' + (client_id + ':' + client_secret)
# }
#
# auth_response = requests.post('https://accounts.spotify.com/api/token', headers=auth_header, data={
# 'grant_type': 'client_credentials'
# })
# print(auth_response.text)
# access_token = auth_response.json()['access_token']
SPOTIFY_TOKEN = "https://accounts.spotify.com/api/token"
request_body = {
"grant_type": "client_credentials",
"code": "code",
"redirect_uri": 'http://localhost:8888/callback',
"client_id": client_id,
"client_secret": client_secret,
}
r = requests.post(url=SPOTIFY_TOKEN, data=request_body)
resp = r.json()['access_token']
spotify = spotipy.SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
# redirect_uri='http://localhost:8888/callback',
# token=access_token
# Create a Spotify client
client = spotipy.Spotify(auth=resp)
def play():
# Extract the first result from the search results
track = results['tracks']['items'][0]
# print(track)
# Get the URI of the track
track_uri = track['uri']
# print(track_uri)
# Start playing the track
webbrowser.open(track_uri)
def art():
# Extract the first result from the search results
track = results['artists']['items'][0]
# print(track)
# Get the URI of the track
track_uri = track['uri']
# Start playing the track
webbrowser.open(track_uri)
def alb():
# Get the first album in the list of results
album = results["albums"]["items"][0]
# print(album)
album_uri = album['uri']
# print(album_uri)
webbrowser.open(album_uri)
# Search for a song
print("Welcome to Spotify! Here you can play about 82 million songs from over 11 million various artists. You just need"
" to enter the \nsong's name and the artist's name to play the song.\n")
while True:
print("Type: \n1. To enter a song you want to play\n2. To enter a song you want to play of a particular artist\n"
"3. To play top tracks of an artist\n4. To exit Spotify player")
choice = int(input())
if choice == 1:
print("Enter the name of the song you want to play")
track_name = input()
results = client.search(q='track:%s' % track_name, type='track')
# print(results)
play()
elif choice == 2:
print("Enter the name of the song you want to play")
track_name = input()
print("Enter the artist's name")
artist_name = input()
results = client.search(q='track:%s artist:%s' % (track_name, artist_name), type='track')
# print(results)
play()
elif choice == 3:
print("Enter the name of the artist whose top tracks you want to play")
artist_name = input()
results = client.search(q='artist:%s' % artist_name, type='artist')
# print(results)
art()
elif choice == 4:
print("Thank you for playing Spotify!")
break
else:
print("Invalid Input! Try again.")