-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboltz.py
262 lines (187 loc) · 8.94 KB
/
boltz.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
# ! Main Boltz File
# TODO add search feature
from inc import *
class Boltz:
# * Constructor
def __init__(self, client_id:str, client_secret:str, save_path="./downloads") -> None:
self.sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))
self.save_path = save_path
# * Initializes and parses your url
def initialize_url(self, _link:str) -> BoltzUrl:
type, id = parse_validate_url(_link)
if type != 0 and id != 0:
return BoltzUrl(type, id, True)
else:
return BoltzUrl(type,id, False)
# * conducts operation accoring to your link types
def fetch_tracks(self, _boltzurl: BoltzUrl) -> list[BoltzTrack]:
if _boltzurl.is_valid:
match _boltzurl.type:
case 'playlist':
tracks = self.__fetch_playlist(_boltzurl.id)
return tracks
case 'album':
tracks = self.__fetch_album(_boltzurl.id)
return tracks
case 'track':
track = self.__fetch_track(_boltzurl.id)
return [track]
case _:
return []
# * Fetches and parses playlist
def __fetch_playlist(self, _id) -> list[BoltzTrack]:
offset = 0 # Count for no of tracks
_track_list = [] # List of BoltzTrack
_is_running = True # For while loop below
while _is_running:
try:
res_items = self.sp.playlist_items(
playlist_id= _id,
fields= SPOTIPY_FIELDS,
additional_types= ["track"],
offset= offset
)
except Exception as e:
break
res_items_parsed = parse_items(res_items) # Parsing dict to List[BoltzItem]
_total_songs = res_items["total"] # Total tracks in the playlist
for item in res_items_parsed:
album_images = item.album_images
artists = item.artists
main_artist_id = artists[0].uri if artists else None
genres = self.sp.artist(artist_id=main_artist_id).get("genres",[]) if main_artist_id else []
if item is None:
offset += 1
continue
_track_album = BoltzAlbum(
item.name,
item.release_date,
item.total_tracks,
album_images[0].url if album_images[0] else None,
artists
)
_track = BoltzTrack(item.id,
item.name,
item.track_number,
_track_album,
",".join([artist.name for artist in artists]),
genres[0] if genres else ""
)
_track_list.append(_track)
offset += 1
_is_running = False if _total_songs == offset else True # Checks if all the songs are processed
return _track_list # Returning list[BoltzItem]
# * Fetches and parses album
def __fetch_album(self, _id) -> list[BoltzTrack]:
offset = 0 # count for no of tracks
_track_list = []
_is_running = True
while _is_running:
try:
album_items = self.sp.album(album_id=_id)
res_items = self.sp.album_tracks(album_id=_id, offset=offset)
except Exception as e:
break
_total_songs = res_items.get("total")
album = parse_album(album_items) # Parsing dict to BoltzAlbum
main_artist_id = album.artists[0].uri
genres = self.sp.artist(artist_id=main_artist_id).get("genres",[]) if main_artist_id else [] # Getting artist genre
_items_parsed = parse_items_album(res_items, album) # Pasing dict to BoltzItem
for item in _items_parsed:
if item is None:
offset += 1
continue
_track = BoltzTrack(item.id,
item.name,
item.track_number,
album,
",".join([artist.name for artist in album.artists]),
genres[0] if genres else ""
)
_track_list.append(_track)
offset += 1
_is_running = False if _total_songs == offset else True # Checks if all the songs are processed
return _track_list # Returning List[BoltzTrack]
# * Fetches and parses track
def __fetch_track(self, _id) -> BoltzTrack:
try:
res_items = self.sp.track(track_id=_id)
except Exception as e:
return []
album = parse_album(res_items["album"])
main_artist_id = album.artists[0].uri
genres = self.sp.artist(artist_id=main_artist_id).get("genres",[]) if main_artist_id else [] # Getting artist genre
_item_parsed = parse_item_track(res_items,album)
_track = BoltzTrack(_item_parsed.id,
_item_parsed.name,
_item_parsed.track_number,
album,
",".join([artist.name for artist in album.artists]),
genres[0] if genres else ""
)
return _track # Returning BoltzTrack
# * Download and convert tracks
def download_track(self, track:BoltzTrack) -> BoltzMP3:
_query = generate_ytdl_query(track.artist, track.name)
_filename = [f"{track.artist} - {track.name}", track.position]
_filepath = path.join(self.save_path, _filename[0])
mp3_filename = f"{_filepath}.mp3"
_out_template = f"{_filepath}.%(ext)s"
_yt_dl_options= generate_ytdl_opts(_out_template,
SPONSOR_BLOCK_PP,
track.name,
track.artist,
track.album
)
with yt_dlp.YoutubeDL(_yt_dl_options) as _ydl:
try:
_ydl.download([_query])
except Exception as e:
return BoltzMP3(False, track, mp3_filename, track.position)
return BoltzMP3(True, track, mp3_filename, track.position)
# * Adding details to song
def set_tags(self, boltzmp3:BoltzMP3, _total_tracks:int) -> bool:
try:
mp3_file = MP3(boltzmp3.mp3_filename, ID3=EasyID3)
except Exception as e:
return False
mp3_file["date"] = boltzmp3.track.album.year
mp3_file["tracknumber"] = (str(boltzmp3.track.position) + "/" + str(_total_tracks))
mp3_file["genre"] = boltzmp3.track.genre
try:
mp3_file.save()
except Exception as e:
return False
mp3_file = MP3(boltzmp3.mp3_filename, ID3=ID3)
cover = boltzmp3.track.album.cover
if cover.lower().startswith("http"):
try:
_req = urllib.request.Request(cover)
with urllib.request.urlopen(_req) as _response:
mp3_file.tags["APIC"] = APIC(
encoding=3,
mime="image/jpeg",
type=3,
desc="Front Cover",
data=_response.read(),
)
except Exception as e:
pass
try:
mp3_file.save()
return True
except Exception as e:
return False
def search_song(self, keywords:str, type:str, total_results=SPOTIFY_SEARCH_LIMIT):
match type:
case BoltzSearchTypes.track:
res_search = self.sp.search(q=keywords, type=BoltzSearchTypes.track, limit=total_results)
return parse_search_res(res_search,BoltzSearchTypes.track)
case BoltzSearchTypes.playlist:
res_search = self.sp.search(q=keywords, type=BoltzSearchTypes.playlist, limit=total_results)
return parse_search_res(res_search, BoltzSearchTypes.playlist)
case BoltzSearchTypes.album:
res_search = self.sp.search(q=keywords, type=BoltzSearchTypes.album, limit=total_results)
return parse_search_res(res_search, BoltzSearchTypes.album)
case _:
return []