-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotify_API.py
More file actions
114 lines (95 loc) · 4.02 KB
/
Copy pathspotify_API.py
File metadata and controls
114 lines (95 loc) · 4.02 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
105
106
107
108
109
110
111
112
113
114
import requests, json
from discord_webhook import DiscordWebhook, DiscordEmbed
from time import sleep
from datetime import datetime
TOKEN = "" #
HEADERS = {
'Accept':'application/json',
'Content-Type':'application/json',
'Authorization':f'Bearer {TOKEN}'
}
def refresh_token(): # get new token after the ancient expired (every hour)
url = 'https://accounts.spotify.com/api/token'
s = requests.Session()
REFRESH_TOKEN = "" # YOUR REFRESH TOKEN HERE
PARAMS = {
'client_id':'', # YOUR CLIENT-ID HERE
'client_secret':'', # YOUR SECRET CLIENT-ID HERE
'grant_type':'refresh_token',
'code':'', # your code. tutorial below
'refresh_token':REFRESH_TOKEN
}
"""
Tutorial how to get a 'code' for your parameters:
https://accounts.spotify.com/authorize?response_type=code&client_id=<YOUR_CLIENT_ID>&scope=user-read-currently-playing&user-modify-playback-state&redirect_uri=<YOUR_CALLBACK_URL_IN_URL-ENCODED_FORMAT>
Click to this link and check in the new url your 'code', i am sure that this is not the right way to do it, but it works lol
"""
HEADERS = {
'Content-Type':'application/x-www-form-urlencoded'
}
r = s.post(url, params=PARAMS, headers=HEADERS)
res = json.loads(r.text)
s.close()
return res['access_token']
def convert(ms):
if len(str(ms)) >= 4:
div = 1000
elif len(str(ms)) < 4:
div = 100
millis= ms
millis = int(millis)
seconds=(millis/div)%60
seconds = int(seconds)
minutes=(millis/(div*60))%60
minutes = int(minutes)
if len(str(minutes)) >= 1 and len(str(seconds)) == 1:
return f"{minutes}:0{seconds}"
elif len(str(minutes)) >= 1 and len(str(seconds)) == 2:
return f"{minutes}:{seconds}"
elif len(str(minutes)) == 0 and len(str(seconds)) == 1:
return f'00:0{seconds}'
elif len(str(minutes)) == 0 and len(str(seconds)) == 2:
return f'00:{seconds}'
def main():
current_song = ''
while True:
r = requests.get(url='https://api.spotify.com/v1/me/player/currently-playing', headers=HEADERS)
if r.status_code == 200:
try:
code = r.json()
artist = code['item']['artists'][0]['name']
#print(f"Artiste: {artist}")
titre = code['item']['name']
#print(f"Titre: {titre}")
#print(f"{titre} - {artist}")
link = code['item']['external_urls']['spotify']
#print(f"Link: {link}")
thumbnail = code['item']['album']['images'][2]['url']
#print(f"Thumbnail: {thumbnail}")
duration_ms = code['item']['duration_ms']
progress_ms = code['progress_ms']
#print(f"00:00 ━━⬤──── {convert(duration_ms)}")
global TOKEN
if current_song != titre:
current_song = titre
url = '' # webhook url
webhook = DiscordWebhook(url=url, rate_limit_retry=True)
embed = DiscordEmbed(description=f'[**{titre} - {artist}**]({link})\n\n{convert(progress_ms)} ━━⬤──── {convert(duration_ms)}\n\n:calendar_spiral: <t:{int(datetime.now().timestamp())}:f>', color=0x5EE922)
embed.set_thumbnail(url=thumbnail)
webhook.add_embed(embed)
webhook.execute()
sleep(4)
else: # notif already sent.
sleep(4)
except:
pass
elif r.status_code == 401: # no functionnal token provided
HEADERS.update({'Authorization':f'Bearer {refresh_token()}'})
sleep(4)
elif r.status_code == 204: # no song playing
sleep(30)
else:
# print(f'Error ({r.status_code})')
sleep(30)
if __name__ == '__main__':
main()