-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
158 lines (118 loc) · 4.38 KB
/
utils.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
147
148
149
150
151
152
153
154
155
156
157
158
import os
import sys
import base64
import requests
import toml
from librespot.core import Session
from mutagen.flac import Picture
from mutagen.oggvorbis import OggVorbis
from mutagen.oggvorbis import OggVorbisHeaderError
from mutagen.ogg import error
from config import ConfigError
class Utils:
def __init__(self, config):
self.config = config
def generate_new_token(self):
"""
tokens are not permament, they expire after an hour,
so when invalid, regenerates a new one
"""
client_id = self.config.get_config_value("account", "client_id")
client_secret = self.config.get_config_value("account", "client_secret")
resp = requests.post(
"https://accounts.spotify.com/api/token",
{
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret,
},
timeout=10,
).json()
config = self.config.read_config()
print("[generate_new_token] Trying to update token...")
if config:
config["account"]["token"] = resp["access_token"]
try:
with open("config.toml", "w", encoding="utf-8") as f:
toml.dump(config, f)
print("[generate_new_token] Token was updated")
except Exception as e:
sys.exit(
f"[generate_new_token] Error updating the configuration file: {e}\n\nManually update token from the config.toml file by yourself with: \"{resp['access_token']}\""
)
try:
os.execv(sys.argv[0], sys.argv)
except FileNotFoundError:
sys.exit("[generate_new_token] Token was updated, run pyspodl again")
def get_token(self):
"""
gets the temporary saved token from the config file
"""
try:
token = self.config.get_config_value("account", "token")
except ConfigError:
self.generate_new_token()
headers = {"Authorization": f"Bearer {token}"}
resp = requests.get(
"https://api.spotify.com/v1/search?q=home+resonance&type=track",
headers=headers,
timeout=10,
)
if resp.status_code == 401:
self.generate_new_token()
return token
def set_metadata(self, metadata, cover_image, filename):
"""
set metadata to a file (these are ogg tags, not id3!)
"""
file = OggVorbis(f"{filename}.ogg")
for key, value in metadata.items():
file[key] = str(value)
try:
resp = requests.get(cover_image["url"], timeout=10)
picture = Picture()
picture.data = resp.content
picture.type = 17
picture.mime = "image/jpeg"
picture.width = cover_image["width"]
picture.height = cover_image["height"]
picture_data = picture.write()
encoded_data = base64.b64encode(picture_data)
vcomment_value = encoded_data.decode("ascii")
file["metadata_block_picture"] = [vcomment_value]
except requests.exceptions.RequestException:
pass
try:
file.save()
except (OggVorbisHeaderError, error):
pass # fuck you
# seriously fuck it, idk why it happens
def get_session(self):
"""
create a user session and return it
"""
try:
print("[get_session] Trying to create a session...")
session = (
Session.Builder()
.user_pass(
self.config.get_config_value("account", "email"),
self.config.get_config_value("account", "password"),
)
.create()
)
return session
except Exception as exc:
sys.exit(
f"[get_session] An issue occured while trying to create session:\n{exc}"
)
def get_id_type_from_url(self, url):
"""
get the id of the track or whatever, and the type of whatever (lol.)
"""
try:
return (url.split("/")[4].split("?")[0], url.split("/")[3])
except IndexError:
sys.exit(
'[get_id_type_from_url] Invalid URL? Does it start with "https://open.spotify.com"?'
)