-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatcher.py
More file actions
95 lines (84 loc) · 4.58 KB
/
dispatcher.py
File metadata and controls
95 lines (84 loc) · 4.58 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
import requests
import exceptions
from requests.adapters import HTTPAdapter
from urllib3 import Timeout
from urllib3.util.retry import Retry
async def get_current_playing_track(access_token):
session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
track = session.get("https://api.spotify.com/v1/me/player/currently-playing",
headers={
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"}, timeout=Timeout(connect=2, read=60)
)
if track.status_code == 200:
return track
elif track.status_code == 401:
raise exceptions.AccessTokenExpired(track)
else:
return None
async def get_access_token(sp_dc):
req = requests.get("https://open.spotify.com/get_access_token?reason=transport&productType=web-player",
headers={"accept": "application/json",
"accept-language": "ru",
"app-platform": "WebPlayer",
"cache-control": "no-cache",
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"spotify-app-version": "1.1.96.83.g8ad99f81",
"cookie": f"sp_dc={sp_dc}",
"Referer": "https://open.spotify.com/album/4c8PkfUv6QLHkFPEqH0yDw",
"Referrer-Policy": "strict-origin-when-cross-origin"}
)
return {'token': req.json()['accessToken'], "id": req.json()['clientId'],
"time": req.json()['accessTokenExpirationTimestampMs']}
async def get_client_token(client_id):
req = requests.post("https://clienttoken.spotify.com/v1/clienttoken",
headers={
"accept": "application/json",
"accept-language": "ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "no-cache",
"content-type": "application/json",
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"Referer": "https://open.spotify.com/",
"Referrer-Policy": "strict-origin-when-cross-origin"
},
data="{\"client_data\":{\"client_version\":\"1.1.96.83.g8ad99f81\","
"\"client_id\":\"" + client_id + "\",\"js_sdk_data\":{"
"\"device_brand\":\"unknown\","
"\"device_model\":\"desktop\","
"\"os\":\"Windows\", "
"\"os_version\":\"NT 10.0\"}}}",
)
return req.json()['granted_token']['token']
async def get_lyric_request(access_token, client_token, track_id):
req = requests.get(
f"https://spclient.wg.spotify.com/color-lyrics/v2/track/{track_id}?format=json&vocalRemoval=false&market=from_token",
headers={"accept": "application/json",
"accept-language": "ru",
"app-platform": "WebPlayer",
"authorization": f"Bearer {access_token}",
"cache-control": "no-cache",
"client-token": client_token,
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"spotify-app-version": "1.1.95.549.g44689ec7",
"Referer": "https://open.spotify.com/",
"Referrer-Policy": "strict-origin-when-cross-origin"}
)
return req
async def send_status(text, emoji, discord_token):
requests.patch("https://discord.com/api/v9/users/@me/settings",
headers={"authorization": discord_token, "content-type": "application/json"},
json={"custom_status": {"text": text, "emoji_name": emoji, "timeout": 2900}})