forked from Michael-K-Stein/SpotiFile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotify_client.py
128 lines (110 loc) · 5.04 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
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
data = {
"client_data": {
"client_version": "",
"client_id": client_id,
"js_sdk_data":
{
"device_brand": "",
"device_model": "",
"os": "",
"os_version": ""
}
}
}
response = session.post('https://clienttoken.spotify.com/v1/clienttoken', json=data, verify=self._verify_ssl)
return response.json()['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)
print('Access token is anon: ', response.json()['isAnonymous'])
self.is_anonymous = response.json()['isAnonymous']
return response.json()['accessToken'], response.json()['clientId']
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