-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
280 lines (221 loc) · 8.08 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# ! File for all utilities for boltz
from models import *
# * Function to parse url to BoltzUrl
def parse_validate_url(url: str) -> [str,str]:
_url = url
if _url.startswith("https://open.spotify.com/"):
try:
_url = _url.replace("https://","")
_url = _url.split("/")
_type = _url[1]
# [id]?[garbage], so removing garbage
_id = _url[2].split("?")[0]
return [_type, _id]
except Exception as e:
return [0,0]
else:
return [0,0]
# * Function to parse dict to BoltzItem
def parse_items(items:dict) -> list[BoltzItem]:
_boltz_items = [] # list[BoltzItem]
for item in items["items"]:
_boltz_images = []
_items_images = item["track"]["album"]["images"]
if _items_images:
for image in _items_images:
_boltz_images.append(
BoltzImage(
image["width"],
image["height"],
image["url"]
)
)
_album_name = item["track"]["album"]["name"] if item["track"]["album"]["name"] else ""
_release_year = item["track"]["album"]["release_date"][:4] if item["track"]["album"]["name"] else ""
_total_tracks = item["track"]["album"]["total_tracks"] if item["track"]["album"]["total_tracks"] else 0
_boltz_artists = []
_items_artists = item["track"]["artists"]
if _items_artists:
for artist in _items_artists:
_boltz_artists.append(
BoltzArtist(
artist["name"],
artist["uri"]
)
)
_id = item["track"]["id"] if item["track"]["id"] else ""
_name = item["track"]["name"] if item["track"]["name"] else ""
_track_number = item["track"]["track_number"] if item["track"]["track_number"] else 0
_boltz_items.append(BoltzItem(
_boltz_images,
_album_name,
_release_year,
_total_tracks,
_boltz_artists,
_id,
_name,
_track_number
))
return _boltz_items # Returning list[BoltzItem]
# * Function to parse dict to BoltzAlbum
def parse_album(album_info:dict) -> BoltzAlbum:
_album_name = album_info["name"] if album_info["name"] else ""
_release_year = album_info["release_date"][:4] if album_info["release_date"] else ""
_total_tracks = album_info["total_tracks"] if album_info["total_tracks"] else 0
_album_images = album_info["images"]
_boltz_images = []
if _album_images:
for image in _album_images:
_boltz_images.append(
BoltzImage(
image["width"],
image["height"],
image["url"]
)
)
_boltz_artists = []
_album_artists = album_info["artists"]
if _album_artists:
for artist in _album_artists:
_boltz_artists.append(
BoltzArtist(
artist["name"],
artist["uri"]
)
)
return BoltzAlbum(
_album_name,
_release_year,
_total_tracks,
_boltz_images[0].url if _boltz_images[0] else None,
_boltz_artists
)
# * Function to parse dict to BoltzItems with album
def parse_items_album(items:dict, album:BoltzAlbum) -> list[BoltzItem]:
_boltz_items = [] # list[BoltzItem]
for item in items["items"]:
_items_image = album.cover
_album_name = album.name
_release_year = album.year
_total_tracks = album.total_tracks
_boltz_artists = []
_items_artists = item["artists"]
if _items_artists:
for artist in _items_artists:
_boltz_artists.append(
BoltzArtist(
artist["name"],
artist["uri"]
)
)
_id = item["id"] if item["id"] else ""
_name = item["name"] if item["name"] else ""
_track_number = item["track_number"] if item["track_number"] else 0
_boltz_items.append(BoltzItem(
[_items_image],
_album_name,
_release_year,
_total_tracks,
_boltz_artists,
_id,
_name,
_track_number
))
return _boltz_items # Returning list[BoltzItem]
# * Function to parse dict to BoltzItem
def parse_item_track(item:dict, album:BoltzAlbum) -> BoltzItem:
_boltz_item = None # list[BoltzItem]
_items_image = album.cover
_album_name = album.name
_release_year = album.year
_total_tracks = album.total_tracks
_boltz_artists = []
_items_artists = item["artists"]
if _items_artists:
for artist in _items_artists:
_boltz_artists.append(
BoltzArtist(
artist["name"],
artist["uri"]
)
)
_id = item["id"] if item["id"] else ""
_name = item["name"] if item["name"] else ""
_track_number = item["track_number"] if item["track_number"] else 0
_boltz_item = BoltzItem(
[_items_image],
_album_name,
_release_year,
_total_tracks,
_boltz_artists,
_id,
_name,
_track_number
)
return _boltz_item # Returning list[BoltzItem]
def generate_ytdl_query(_artist:str, _song_name:str) -> str:
_query = f"{_artist} - {_song_name} Lyrics"
_query = _query.replace(':','').replace(' " ', '') # removing unwanted characters in query
return _query
def generate_ytdl_opts(_out_template:str,
_post_processor:[dict],
_track_name:str,
_track_artist:str,
_track_album:str) -> dict:
_ytdl_opts = {
"quiet":True,
"proxy": "",
"default_search": "ytsearch",
"format": "bestaudio/best",
"outtmpl": _out_template,
"postprocessors": [],
"noplaylist": True,
"no_color": False,
"postprocessor_args": [
"-metadata",
"title=" + _track_name,
"-metadata",
"artist=" + _track_artist,
"-metadata",
"album=" + _track_album.name,
],
}
mp3_pp_opts = {
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": "192",
}
_ytdl_opts["postprocessors"].append(mp3_pp_opts.copy())
return _ytdl_opts
# * Parses and returns link of results
def parse_search_res(items:dict, type:str) -> [BoltzSearchResult]:
items = items[type + 's']
items = items["items"]
_search_res = []
for item in items:
_type = item["type"]
_id = item["id"]
_title = item["name"] if item["name"] else "Unknown"
if(type != BoltzSearchTypes.playlist):
_artist = item["artists"][0]["name"] if item["artists"][0]["name"] else "Unknown"
else:
_artist = item["owner"]["display_name"]
if(type == BoltzSearchTypes.track):
_image = item["album"]["images"][0]
_image = BoltzImage(_image["width"],_image["height"],_image["url"])
else:
_image = item["images"][0]
_image = BoltzImage(_image["width"],_image["height"],_image["url"])
_search_res.append(
BoltzSearchResult(
BoltzUrl(_type, _id, True),
_title,
_artist,
_image
)
)
return _search_res
def ASSERT(_condition:bool, _message:str):
if(not _condition):
print(_message)
exit()