forked from surenjanath/Spotify-Song-Downloader-PyQt5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpotify_Downloader.py
More file actions
583 lines (481 loc) · 22.7 KB
/
Spotify_Downloader.py
File metadata and controls
583 lines (481 loc) · 22.7 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# Main
# if __name__ == '__main__':from PyQt5.uic import loadUi
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsDropShadowEffect
from PyQt5.QtCore import Qt, QThread, pyqtSignal, pyqtSlot, QSize, QPropertyAnimation, QEasingCurve
from PyQt5.QtGui import QCursor, QImage, QPixmap
from Template import Ui_MainWindow
import sys
import os
import string
import requests
import re
import webbrowser
from mutagen.easyid3 import EasyID3
from mutagen.id3 import APIC, ID3
class MusicScraper(QThread):
PlaylistCompleted = pyqtSignal(str)
PlaylistID = pyqtSignal(str)
song_Album = pyqtSignal(str)
song_meta = pyqtSignal(dict)
add_song_meta = pyqtSignal(dict)
count_updated = pyqtSignal(int)
dlprogress_signal = pyqtSignal(int)
Resetprogress_signal = pyqtSignal(int)
def __init__(self):
super(MusicScraper, self).__init__()
self.counter = 0 # Initialize the counter to zero
self.session = requests.Session()
def get_ID(self, yt_id):
# The 'get_ID' function from your scraper code
LINK = f'https://api.spotifydown.com/getId/{yt_id}'
headers = {
'authority': 'api.spotifydown.com',
'method': 'GET',
'path': f'/getId/{id}',
'origin': 'https://spotifydown.com',
'referer': 'https://spotifydown.com/',
'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
'sec-fetch-mode': 'cors',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
response = self.session.get(url=LINK, headers=headers)
if response.status_code == 200:
data = response.json()
return data
return None
def generate_Analyze_id(self, yt_id):
# The 'generate_Analyze_id' function from your scraper code
DL = 'https://corsproxy.io/?https://www.y2mate.com/mates/analyzeV2/ajax'
data = {
'k_query': f'https://www.youtube.com/watch?v={yt_id}',
'k_page': 'home',
'hl': 'en',
'q_auto': 0,
}
headers = {
'authority': 'corsproxy.io',
'method': 'POST',
'path': '/?https://www.y2mate.com/mates/analyzeV2/ajax',
'origin': 'https://spotifydown.com',
'referer': 'https://spotifydown.com/',
'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
'sec-fetch-mode': 'cors',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
RES = self.session.post(url=DL, data=data, headers=headers)
if RES.status_code == 200:
return RES.json()
return None
def generate_Conversion_id(self, analyze_yt_id, analyze_id):
# The 'generate_Conversion_id' function from your scraper code
DL = 'https://corsproxy.io/?https://www.y2mate.com/mates/convertV2/index'
data = {
'vid' : analyze_yt_id,
'k' : analyze_id,
}
headers = {
'authority': 'corsproxy.io',
'method': 'POST',
'path': '/?https://www.y2mate.com/mates/analyzeV2/ajax',
'origin': 'https://spotifydown.com',
'referer': 'https://spotifydown.com/',
'sec-ch-ua': '"Not_A Brand";v="99", "Google Chrome";v="109", "Chromium";v="109"',
'sec-fetch-mode': 'cors',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}
RES = self.session.post(url=DL, data=data, headers=headers)
if RES.status_code == 200:
return RES.json()
return None
def get_PlaylistMetadata(self, Playlist_ID):
# The 'get_PlaylistMetadata' function from your scraper code
URL = f'https://api.spotifydown.com/metadata/playlist/{Playlist_ID}'
headers = {
'authority': 'api.spotifydown.com',
'method': 'GET',
'path': f'/metadata/playlist/{Playlist_ID}',
'scheme': 'https',
'origin': 'https://spotifydown.com',
'referer': 'https://spotifydown.com/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
}
meta_data = self.session.get(headers=headers, url=URL)
if meta_data.status_code == 200:
return meta_data.json()['title'] + ' - ' + meta_data.json()['artists']
return None
def errorcatch(self, SONG_ID):
# The 'errorcatch' function from your scraper code
print('[*] Trying to download...')
headers = {
'authority': 'api.spotifydown.com',
'method': 'GET',
'path': f'/download/{SONG_ID}',
'scheme': 'https',
'origin': 'https://spotifydown.com',
'referer': 'https://spotifydown.com/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
}
x = self.session.get(headers=headers, url='https://api.spotifydown.com/download/' + SONG_ID)
if x.status_code == 200:
return x.json()['link']
return None
def V2catch(self, SONG_ID):
## Updated .. .19TH OCTOBER 2023
# yt_id = self.get_ID(SONG_ID)
# domain = ["co.wuk.sh", "cobalt2.snapredd.app"]
# target_domain = domain[random.randint(0,len(domain) - 1)]
headers = {
"authority": "api.spotifydown.com",
"method": "POST",
"path": '/download/68GdZAAowWDac3SkdNWOwo',
"scheme": "https",
"Accept": "*/*",
'Sec-Ch-Ua':'"Chromium";v="118", "Google Chrome";v="118", "Not=A?Brand";v="99"',
"Dnt": '1',
"Origin": "https://spotifydown.com",
"Referer": "https://spotifydown.com/",
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
}
## Updated .. .29TH OCTOBER 2023
x = self.session.get(url = f'https://api.spotifydown.com/download/{SONG_ID}', headers=headers)
# if x.status_code == 200:
# # par = {
# # 'aFormat':'"mp3"',
# # 'dubLang':'false',
# # 'filenamePattern':'"classic"',
# # 'isAudioOnly':'true',
# # 'isNoTTWatermark':'true',
# # 'url':f'"https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D{yt_id}"'
# # }
# file_status = self.session.post(url=f"https://{target_domain}/api/json", json=par, headers=headers)
# print('[*] Data Gathered : ', str(x.content))
if x.status_code == 200:
try:
return {
'link' : x.json()['link'],
'metadata' : None
}
except:
return {
'link' : None,
'metadata' : None
}
return None
def scrape_playlist(self, spotify_playlist_link, music_folder):
ID = self.returnSPOT_ID(spotify_playlist_link)
self.PlaylistID.emit(ID)
PlaylistName = self.get_PlaylistMetadata(ID)
self.song_Album.emit(PlaylistName)
# Create Folder for Playlist
if not os.path.exists(music_folder):
os.makedirs(music_folder)
try:
FolderPath = ''.join(e for e in PlaylistName if e.isalnum() or e in [' ', '_'])
playlist_folder_path = os.path.join(music_folder, FolderPath)
except:
playlist_folder_path = music_folder
if not os.path.exists(playlist_folder_path):
os.makedirs(playlist_folder_path)
headers = {
'authority': 'api.spotifydown.com',
'method': 'GET',
'path': f'/trackList/playlist/{ID}',
'scheme': 'https',
'accept': '*/*',
'dnt': '1',
'origin': 'https://spotifydown.com',
'referer': 'https://spotifydown.com/',
'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Google Chrome";v="110"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36',
}
Playlist_Link = f'https://api.spotifydown.com/trackList/playlist/{ID}'
offset_data = {}
offset = 0
offset_data['offset'] = offset
while offset is not None:
response = self.session.get(url=Playlist_Link, params=offset_data, headers=headers)
if response.status_code == 200:
Tdata = response.json()['trackList']
page = response.json()['nextOffset']
for count, song in enumerate(Tdata):
self.Resetprogress_signal.emit(0)
filename = song['title'].translate(str.maketrans('', '', string.punctuation)) + ' - ' + song['artists'].translate(str.maketrans('', '', string.punctuation)) + '.mp3'
filepath = os.path.join(playlist_folder_path, filename)
try:
try:
V2METHOD = self.V2catch(song['id'])
DL_LINK = V2METHOD['link']
SONG_META = song
SONG_META['file'] = filepath
self.song_meta.emit(SONG_META)
except IndentationError:
yt_id = self.get_ID(song['id'])
if yt_id is not None:
data = self.generate_Analyze_id(yt_id['id'])
try:
DL_ID = data['links']['mp3']['mp3128']['k']
DL_DATA = self.generate_Conversion_id(data['vid'], DL_ID)
DL_LINK = DL_DATA['dlink']
except Exception as NoLinkError:
CatchMe = self.errorcatch(song['id'])
if CatchMe is not None:
DL_LINK = CatchMe
else:
print('[*] No data found for : ', song)
download_complete = False
if DL_LINK is not None:
## DOWNLOAD
link = self.session.get(DL_LINK, stream=True)
total_size = int(link.headers.get('content-length', 0))
block_size = 1024 # 1 Kilobyte
downloaded = 0
## Save
with open(filepath, "wb") as f:
for data in link.iter_content(block_size):
f.write(data)
downloaded += len(data)
# self.dlprogress_signal.emit(int(100 * downloaded / total_size))
#Increment the counter
self.increment_counter()
# # Emit the signal with the downloaded song name
self.add_song_meta.emit(SONG_META)
else:
print('[*] No Download Link Found.')
except Exception as error_status:
print('[*] Error Status Code : ', error_status)
if page is not None:
offset_data['offset'] = page
response = self.session.get(url=Playlist_Link, params=offset_data, headers=headers)
else:
self.PlaylistCompleted.emit('Download Complete!')
break
def returnSPOT_ID(self, link):
# # The 'returnSPOT_ID' function from your scraper code
# return link.split('/')[-1].split('?si')[0]
# Define the regular expression pattern for the Spotify playlist URL
pattern = r"https://open\.spotify\.com/playlist/([a-zA-Z0-9]+)\?si=.*"
# Try to match the pattern in the input text
match = re.match(pattern, link)
if not match:
raise ValueError("Invalid Spotify playlist URL.")
# Extract the playlist ID from the matched pattern
extracted_id = match.group(1)
return extracted_id
def increment_counter(self):
self.counter += 1
self.count_updated.emit(self.counter) # Emit the signal with the updated count
# Scraper Thread
class ScraperThread(QThread):
progress_update = pyqtSignal(str)
def __init__(self, playlist_link):
super().__init__()
self.playlist_link = playlist_link
self.scraper = MusicScraper() # Create an instance of MusicScraper
def run(self):
music_folder = os.path.join(os.getcwd(), "music") # Change this path to your desired music folder
self.progress_update.emit("Scraping started...")
try:
self.scraper.returnSPOT_ID(self.playlist_link)
self.scraper.scrape_playlist(self.playlist_link, music_folder)
self.progress_update.emit("Scraping completed.")
except Exception as e:
self.progress_update.emit(f"{e}")
# Download Song Cover Thread
class DownloadCover(QThread):
albumCover = pyqtSignal(object)
def __init__(self, url):
super().__init__()
self.url = url
def run(self):
response = requests.get(self.url, stream=True)
if response.status_code == 200 :
self.albumCover.emit(response.content)
# Scraper Thread
class WritingMetaTagsThread(QThread):
tags_success = pyqtSignal(str)
def __init__(self, tags, filename):
super().__init__()
self.tags = tags
self.filename = filename
self.PICTUREDATA = None
def run(self):
# self.tags_success.emit("Writing Metatags")
try:
print('[*] FileName : ', self.filename)
audio = EasyID3(self.filename)
audio['title'] = self.tags['title']
audio['artist'] = self.tags['artists']
audio['album'] = self.tags['album']
audio['date'] = self.tags['releaseDate']
audio.save()
# self.tags_success.emit("Meta added!")
# self.tags_success.emit("Adding Cover")
self.CoverPic = DownloadCover(self.tags['cover']+"?size=1")
self.CoverPic.albumCover.connect(self.setPIC)
self.CoverPic.start()
except Exception as e:
pass
# print(f'Error {e}')
# self.tags_success.emit(f"Error in FIle : {e}")
def setPIC(self, data):
if data is None:
self.tags_success.emit("Cover Not Added..!")
else:
try:
audio = ID3(self.filename)
audio['APIC'] = APIC(
encoding=3,
mime='image/jpeg',
type=3,
desc=u'Cover',
data=data
)
audio.save()
# self.tags_success.emit("Cover Added..!")
except Exception as e:
self.tags_success.emit(f"Error adding cover: {e}")
class DownloadThumbnail(QThread):
def __init__(self, url, main_UI):
super().__init__()
self.url = url
self.main_UI = main_UI
def run(self):
response = requests.get(self.url, stream=True)
if response.status_code == 200 :
pic = QImage()
pic.loadFromData(response.content)
self.main_UI.CoverImg.setPixmap(QPixmap(pic))
self.main_UI.CoverImg.show()
# Main Window
class MainWindow(QMainWindow, Ui_MainWindow):
# class MainWindow(QMainWindow):
def __init__(self):
"""MainWindow constructor"""
super(MainWindow, self).__init__()
# Main UI code goes here
# loadUi("Template.ui", self)
self.setupUi(self)
self.SONGINFORMATION.setGraphicsEffect(QGraphicsDropShadowEffect(blurRadius=25,xOffset=2,yOffset=2))
self.PlaylistLink.returnPressed.connect(self.on_returnButton)
self.showPreviewCheck.stateChanged.connect(self.show_preview)
self.Closed.clicked.connect(self.exitprogram)
self.Select_Home.clicked.connect(self.Medium)
# End main UI code
# https://open.spotify.com/playlist/37i9dQZF1E36hkEAnydKTA?si=20caa5adfed648d3
@pyqtSlot()
def on_returnButton(self):
playlist_id = self.PlaylistLink.text()
try:
# self.PlaylistMsg.setText('Playlist Code : %s' % playlist_id)
# Start the scraper in a separate thread
self.scraper_thread = ScraperThread(playlist_id)
# self.scraper_thread.scraper.PlaylistID.connect(lambda x:self.PlaylistMsg.setText("Playlist Code : {}".format(x))) # Connect the signal
self.scraper_thread.progress_update.connect(self.update_progress)
self.scraper_thread.finished.connect(self.thread_finished) # Connect the finished signal
self.scraper_thread.scraper.song_Album.connect(self.update_AlbumName) # Connect the signal
self.scraper_thread.scraper.song_meta.connect(self.update_song_META) # Connect the signal
self.scraper_thread.scraper.add_song_meta.connect(self.add_song_META) # Connect the signal
self.scraper_thread.scraper.dlprogress_signal.connect(self.update_song_progress) # Download Progress
self.scraper_thread.scraper.Resetprogress_signal.connect(self.Reset_song_progress) # Download Progress
self.scraper_thread.scraper.PlaylistCompleted.connect(lambda x: self.statusMsg.setText(x))
# Connect the count_updated signal to the update_counter slot
self.scraper_thread.scraper.count_updated.connect(self.update_counter)
self.scraper_thread.start()
except ValueError as e:
self.statusMsg.setText(str(e))
def thread_finished(self):
self.scraper_thread.deleteLater() # Clean up the thread properly
def update_progress(self, message):
self.statusMsg.setText(message)
@pyqtSlot(dict)
def update_song_META(self, song_meta):
if self.showPreviewCheck.isChecked():
self.thumbnail_thread = DownloadThumbnail(song_meta['cover']+"?size=1", self)
self.thumbnail_thread.start()
self.ArtistNameText.setText(song_meta['artists'])
self.AlbumText.setText(song_meta['album'])
self.SongName.setText(song_meta['title'])
self.YearText.setText(song_meta['releaseDate'])
else:
pass
self.MainSongName.setText(song_meta['title'] + ' - ' + song_meta['artists'])
if self.AddMetaDataCheck.isChecked():
self.meta_thread = WritingMetaTagsThread(song_meta, song_meta['file'])
self.meta_thread.tags_success.connect(lambda x:self.statusMsg.setText("{}".format(x)))
self.meta_thread.start()
@pyqtSlot(dict)
def add_song_META(self, song_meta):
if self.AddMetaDataCheck.isChecked():
self.meta_thread = WritingMetaTagsThread(song_meta, song_meta['file'])
self.meta_thread.tags_success.connect(lambda x:self.statusMsg.setText("{}".format(x)))
self.meta_thread.start()
@pyqtSlot(str)
def update_AlbumName(self, AlbumName):
self.AlbumName.setText("Playlist Name : " + AlbumName)
@pyqtSlot(int)
def update_counter(self, count):
self.CounterLabel.setText("Songs downloaded " + str(count))
@pyqtSlot(int)
def update_song_progress(self, progress):
self.SongDownloadprogressBar.setValue(progress)
self.SongDownloadprogress.setValue(progress)
@pyqtSlot(int)
def Reset_song_progress(self, progress):
self.SongDownloadprogressBar.setValue(0)
self.SongDownloadprogress.setValue(0)
# DRAGGLESS INTERFACE
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.m_drag = True
self.m_DragPosition = event.globalPos() - self.pos()
event.accept()
self.setCursor(QCursor(Qt.ClosedHandCursor))
def mouseMoveEvent(self, QMouseEvent):
try:
if Qt.LeftButton and self.m_drag:
self.move(QMouseEvent.globalPos() - self.m_DragPosition)
QMouseEvent.accept()
except:
pass
def mouseReleaseEvent(self, QMouseEvent):
self.m_drag = False
self.setCursor(QCursor(Qt.ArrowCursor))
def CloseSongInformation(self):
self.animation = QPropertyAnimation(self.SONGINFORMATION, b"size")
self.animation.setDuration(250)
self.animation.setEndValue(QSize(0,330))
self.animation.setEasingCurve(QEasingCurve.InOutQuad)
self.animation.start()
def OpenSongInformation(self):
self.animation = QPropertyAnimation(self.SONGINFORMATION, b"size")
self.animation.setDuration(1000)
self.animation.setEndValue(QSize(270,330))
self.animation.setEasingCurve(QEasingCurve.InOutQuad)
self.animation.start()
def show_preview(self, state):
if state == 2: # 2 corresponds to checked state
self.preview_window = self.OpenSongInformation()
else:
self.CloseSongInformation()
def exitprogram(self):
sys.exit()
def Medium(self):
webbrowser.open('https://surenjanath.medium.com/subscribe')
# Main
if __name__ == '__main__':
app = QApplication(sys.argv)
Screen = MainWindow()
Screen.setFixedHeight(390)
Screen.setFixedWidth(600)
Screen.setWindowFlags(Qt.FramelessWindowHint)
Screen.setAttribute(Qt.WA_TranslucentBackground)
Screen.show()
sys.exit(app.exec())