-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
55 lines (47 loc) · 1.95 KB
/
util.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
import json
from base64 import b64encode
from http.client import HTTPSConnection
from urllib.parse import urlencode
from config import config
class SpotifyActor:
client_secret_pair = b64encode(
"{id}:{sec}".format(id=config.get("client_id"), sec=config.get("client_secret")).encode("utf-8")).decode(
"utf-8")
# auth stuff
auth_url = "accounts.spotify.com"
api_url = "api.spotify.com"
auth_headers = {
"Content-type": "application/x-www-form-urlencoded",
"Authorization": "Basic " + client_secret_pair
}
auth_params = urlencode({
"grant_type": "refresh_token",
"refresh_token": config.get("refresh_token")
})
def __init__(self):
self.access_token = self.get_access_token()
self.playlist = self.get_playlist()
self.mirror = self.get_mirror_playlist()
def get_playlist(self) -> json:
conn = HTTPSConnection(self.api_url)
conn.request("GET",
"/v1/playlists/{id}".format(id=config.get("playlist_id")),
headers=self.generate_access_token_header())
response = conn.getresponse()
return json.loads(response.read().decode("utf-8"))
def get_mirror_playlist(self):
conn = HTTPSConnection(self.api_url)
conn.request("GET",
"/v1/playlists/{id}".format(id=config.get("mirror")),
headers=self.generate_access_token_header())
response = conn.getresponse()
return json.loads(response.read().decode("utf-8"))
def get_access_token(self) -> str:
conn = HTTPSConnection(self.auth_url)
conn.request("POST", "/api/token", self.auth_params, self.auth_headers)
response = conn.getresponse()
return json.loads(response.read().decode("utf-8"))["access_token"]
def generate_access_token_header(self) -> dict:
return {
"Authorization": "Bearer {token}".format(token=self.access_token)
}