This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
spotify_client.py
147 lines (127 loc) · 5.79 KB
/
spotify_client.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from config import *
from exceptions import SpotifyClientException
class SpotifyClient:
_proxy = PROXY
_client_token = ''
_access_token = ''
_client_id = ''
__USER_AGENT = USER_AGENT
_verify_ssl = VERIFY_SSL
user_data = None
def __init__(self, sp_dc=None, sp_key=None):
self.dc = sp_dc
self.key = sp_key
self.__HEADERS = {
'User-Agent': self.__USER_AGENT,
'Accept': 'application/json',
'Origin': 'https://open.spotify.com',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Referer': 'https://open.spotify.com/',
'Te': 'trailers',
'App-Platform': 'WebPlayer'
}
self.get_tokens(sp_dc, sp_key)
def get_tokens(self, sp_dc=None, sp_key=None):
self._access_token, self._client_id = self.get_access_token(sp_dc=sp_dc, sp_key=sp_key)
self._client_token = self.get_client_token(self._client_id)
print('Client token: ', self._client_token)
print('Access token: ', self._access_token)
def refresh_tokens(self):
self.get_tokens(self.dc, self.key)
def get_client_token(self, client_id: str):
with requests.session() as session:
session.proxies = self._proxy
session.headers = self.__HEADERS
# Clear old tokens, otherwise we will get 400 Bad Request
if 'client_token' in session.headers:
session.headers.pop('client_token')
if 'Authorization' in session.headers:
session.headers.pop('Authorization')
data = {
"client_data": {
"client_version": "1.2.13.477.ga4363038",
"client_id": client_id,
"js_sdk_data":
{
"device_brand": "",
"device_id": "",
"device_model": "",
"device_type": "",
"os": "",
"os_version": ""
}
}
}
response = session.post('https://clienttoken.spotify.com/v1/clienttoken', json=data, verify=self._verify_ssl)
try:
rj = response.json()
except Exception as ex:
print('Failed to parse client token response as json!', ex)
exit(0)
return rj['granted_token']['token']
def get_access_token(self, keys=None, sp_dc=None, sp_key=None):
with requests.session() as session:
session.proxies = self._proxy
session.headers = self.__HEADERS
cookie = {}
if keys is not None:
cookie = keys
if sp_dc is not None:
cookie['sp_dc'] = sp_dc
if sp_key is not None:
cookie['sp_key'] = sp_key
response = session.get('https://open.spotify.com/get_access_token', verify=self._verify_ssl, cookies=cookie)
try:
rj = response.json()
except Exception as ex:
print('An error occured when generating an access token!', ex)
exit(0)
print('Access token is anon: ', rj['isAnonymous'])
self.is_anonymous = rj['isAnonymous']
return rj['accessToken'], rj['clientId'] if rj['clientId'].lower() != 'unknown' else self._client_id
def get_me(self):
with requests.session() as session:
session.proxies = self._proxy
session.headers = self.__HEADERS
session.headers.update({
'Client-Token': self._client_token,
'Authorization': f'Bearer {self._access_token}'
})
response_json = session.get('https://api.spotify.com/v1/me', verify=self._verify_ssl).json()
self.user_data = response_json
if not 'product' in self.user_data:
raise SpotifyClientException('Spotify client keys are invalid.\nVerify that you have entered valid SP_KEY & SP_DC values.')
if self.user_data['product'] == 'premium':
raise SpotifyClientException('THIS USER IS PREMIUM!')
return response_json
def get_premium_keys(self):
page = requests.get('https://www.rkstore.tn/2022/03/spotify-premium-cookies.html', verify=self._verify_ssl)
root = html.document_fromstring(page.content)
cookies_element = root.get_element_by_id('download_link')
cookies = json.loads(cookies_element.text_content())
prem_keys = {}
for cookie in cookies:
prem_keys[cookie['name']] = cookie['value']
return prem_keys
def get(self, url: str) -> Response:
with requests.session() as session:
session.proxies = self._proxy
session.headers = self.__HEADERS
session.headers.update({
'Client-Token': self._client_token,
'Authorization': f'Bearer {self._access_token}'
})
response = session.get(url, verify=self._verify_ssl)
return response
def post(self, url: str, payload=None) -> Response:
with requests.session() as session:
session.proxies = self._proxy
session.headers = self.__HEADERS
session.headers.update({
'Client-Token': self._client_token,
'Authorization': f'Bearer {self._access_token}'
})
response = session.post(url, verify=self._verify_ssl, data=payload)
return response