-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.py
54 lines (38 loc) · 1.15 KB
/
main.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
import requests
import time
from pprint import pprint
SPOTIFY_GET_CURRENT_TRACK_URL = 'https://api.spotify.com/v1/me/player/currently-playing'
ACCESS_TOKEN = ''
def get_current_track(access_token):
response = requests.get(
SPOTIFY_GET_CURRENT_TRACK_URL,
headers={
"Authorization": f"Bearer {access_token}"
}
)
json_resp = response.json()
track_id = json_resp['item']['id']
track_name = json_resp['item']['name']
artists = [artist for artist in json_resp['item']['artists']]
link = json_resp['item']['external_urls']['spotify']
artist_names = ', '.join([artist['name'] for artist in artists])
current_track_info = {
"id": track_id,
"track_name": track_name,
"artists": artist_names,
"link": link
}
return current_track_info
def main():
current_track_id = None
while True:
current_track_info = get_current_track(ACCESS_TOKEN)
if current_track_info['id'] != current_track_id:
pprint(
current_track_info,
indent=4,
)
current_track_id = current_track_info['id']
time.sleep(1)
if __name__ == '__main__':
main()