Repository navigation
Expand file tree
/
Copy pathdownload.py
More file actions
413 lines (376 loc) · 14 KB
/
Copy pathdownload.py
File metadata and controls
413 lines (376 loc) · 14 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
import base64
import datetime
import os
import shutil
import smtplib
from email.message import EmailMessage
from urllib.parse import urlencode
from zipfile import ZipFile
import requests
import youtube_dl
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from pymongo import MongoClient
from youtube_search import YoutubeSearch
# import eyed3
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
client_id = ''
client_secret = ''
client = MongoClient(
"")
db = client.
collection1 = db['']
collection2 = db['']
collection3 = db['']
class SpotifyAPI(object):
access_token = None
access_token_expires = datetime.datetime.now()
access_token_did_expire = True
client_id = None
client_secret = None
token_url = 'https://accounts.spotify.com/api/token'
def __init__(self, client_id, client_secret, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client_id = client_id
self.client_secret = client_secret
def get_client_credentials(self):
"""
Returns a base64 encoded string
"""
client_id = self.client_id
client_secret = self.client_secret
if client_secret == None or client_secret == None:
raise Exception('You must set client_ID and client_secret')
client_creds = f'{client_id}:{client_secret}'
client_creds_base64 = base64.b64encode(client_creds.encode())
return client_creds_base64.decode()
def get_token_headers(self):
client_creds_base64 = self.get_client_credentials()
return {
'Authorization': f'Basic {client_creds_base64}'
}
def get_token_data(self):
return {
'grant_type': 'client_credentials'
}
def perfom_auth(self):
token_url = self.token_url
token_data = self.get_token_data()
token_headers = self.get_token_headers()
r = requests.post(token_url, data=token_data, headers=token_headers)
if r.status_code not in range(200, 299):
raise Exception("Could not authenticate client.")
now = datetime.datetime.now()
data = r.json()
access_token = data['access_token']
expires_in = data['expires_in']
expires = now + datetime.timedelta(seconds=expires_in)
self.access_token_expires = expires
self.access_token_did_expire = expires < now
self.access_token = access_token
return True
def get_access_token(self):
token = self.access_token
expires = self.access_token_expires
now = datetime.datetime.now()
if expires < now:
self.perfom_auth()
return self.get_access_token()
elif token == None:
self.perfom_auth()
return self.get_access_token()
return token
def search(self, query, search_type='artist'):
access_token = self.get_access_token()
headers = {
'Authorization': f'Bearer {access_token}'
}
endpoint = 'https://api.spotify.com/v1/search'
data = urlencode({
'q': query,
'type': search_type.lower()
})
print(data)
lookup_url = f"{endpoint}?{data}"
r = requests.get(lookup_url, headers=headers)
print(lookup_url)
if r.status_code not in range(200, 299):
return {}
return r.json()
def playlist(self, link, num, offset):
link_main = link[34:]
target_URI = ''
for char in link_main:
if char != '?':
target_URI += char
else:
break
access_token = self.get_access_token()
headers = {
'Authorization': f'Bearer {access_token}'
}
endpoint = 'https://api.spotify.com/v1/playlists/'
append = f"/tracks?market=ES&fields=items(track(name,artists,album(name,images)))&limit={num}&offset={offset}"
lookup_url = f"{endpoint}{target_URI}{append}"
r = requests.get(lookup_url, headers=headers)
if r.status_code not in range(200, 299):
return {}
return r.json()
spotify = SpotifyAPI(client_id, client_secret)
while True:
queue = collection2.find()
for user in queue:
user_check = collection1.find_one(
{
'email': user['email'].lower(),
'uses': 1
}
)
print(f"Download starting: {user['name']} {user['length_req']}")
num = int(user['length_req'])
if num > 100:
songs = []
images = []
album_names = []
artist_names = []
track_names = []
loops_req = int(user['length_req']) // 100 + 1
offset = 0
for loop in range(loops_req):
data = spotify.playlist(link=user['link'], num=100, offset=offset)
for item in range(100):
try:
none_object = data['items'][item]['track']
except IndexError:
pass
if none_object == None:
pass
else:
try:
track_name = data['items'][item]['track']['name']
artist_name = data['items'][item]['track']['artists'][0]['name']
image = data['items'][item]['track']['album']['images'][1]['url']
album_name = data['items'][item]['track']['album']['name']
songs.append(f'{track_name} - {artist_name}')
images.append(image)
album_names.append(album_name)
artist_names.append(artist_name)
track_names.append(track_name)
success = True
except IndexError:
pass
offset += 100
else:
songs = []
images = []
album_names = []
artist_names = []
track_names = []
data = spotify.playlist(link=user['link'], num=num, offset=0)
for item in range(num):
try:
track_name = data['items'][item]['track']['name']
artist_name = data['items'][item]['track']['artists'][0]['name']
image = data['items'][item]['track']['album']['images'][1]['url']
album_name = data['items'][item]['track']['album']['name']
songs.append(f'{track_name} - {artist_name}')
images.append(image)
album_names.append(album_name)
artist_names.append(artist_name)
track_names.append(track_name)
success = True
except IndexError:
pass
directory = f"{user['email'].lower()}'s Playlist"
user['directory'] = directory
try:
os.mkdir(directory)
except:
try:
shutil.rmtree(directory)
os.mkdir(directory)
except:
pass
base = 'https://www.youtube.com'
for song in songs:
index = songs.index(song)
success = False
try:
print(f"Downloading: {song}")
result = YoutubeSearch(song, max_results=1).to_dict()
suffix = result[0]['url_suffix']
link = base + suffix
song = "".join(x for x in song if (x.isalnum() or x in "._- "))
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': f'{directory}/{song}.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([link])
# success = True
except FileExistsError:
# success = True
pass
except:
try:
print(f"Downloading again: {song}")
result = YoutubeSearch(song, max_results=1).to_dict()
suffix = result[0]['url_suffix']
link = base + suffix
song = "".join(x for x in song if (x.isalnum() or x in "._- "))
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': f'{directory}/{song}.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([link])
except:
print(f"Download failed: {song}")
# if success:
# audiofile = eyed3.load(f"{directory}\{song}.mp3")
# audiofile.tag.artist = artist_names[index]
# audiofile.tag.album = album_names[index]
# audiofile.tag.title = track_names[index]
# audiofile.tag.track_num = int(index+1)
# audiofile.tag.images = images[index]
# audiofile.tag.save()
print(f"Playlist for {user['name']} downloaded successfully!")
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append(filepath)
with ZipFile(f"{user['email'].lower()}.zip", 'w') as zip:
for file in file_paths:
zip.write(file)
file = drive.CreateFile(
{
'title': f"{user['email'].lower()}.zip",
'parents': [{'kind': 'drive#fileLink',
'id': ""}]
}
)
file.SetContentFile(f"{user['email'].lower()}.zip")
file.Upload()
file_id = file['id']
file_url = f"https://drive.google.com/file/d/{file_id}/view?usp=sharing"
file.content.close()
name = user['name']
print("Upload complete")
collection2.delete_one(
{
'name': user['name'],
'email': user['email'].lower(),
'link': user['link'],
'length_req': user['length_req']
}
)
message = EmailMessage()
message['subject'] = 'Download Complete - SpotiPy Downloader'
message['from'] = '@gmail.com'
message['to'] = user['email'].lower()
html_message = """
<!DOCTYPE html>
<html lang='en' class=''>
<head>
<meta charset='UTF-8'>
<meta name="robots" content="noindex">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400,700">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Condensed">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<style class="INLINE_PEN_STYLESHEET_ID" >
*, *:before, *:after {
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
font-size: 12px;
}
body, button, input {
font-family: 'Montserrat', sans-serif;
font-weight: 700;
letter-spacing: 1.4px;
}
h1 {
font-family: 'Montserrat', sans-serif;
font-weight: 700;
letter-spacing: 1.4px;
text-align: center;
font-size: 30px;
color: #1db954;
}
p {
text-align: center;
line-height: 17px;
}
a {
color: rgb(66, 71, 77);
}
button {
color: #1db954;
background-color: rgb(66, 71, 77);
}
.form {
text-align: center;
align-items: center;
padding-top: 10px;
}
.note {
color: red;
}
</style>
"""
html_message += f"""
<script src="https://cpwebassets.codepen.io/assets/common/stopExecutionOnTimeout-157cd5b220a5c80d4ff8e0e70ac069bffd87a61252088146915e8726e5d9f147.js"></script>
<script src="https://cdpn.io/cp/internal/boomboom/pen.js?key=pen.js-00245fc6-a69f-7fef-45f8-9ca6a7d058a6" crossorigin></script>
<body>
<h1 cl#ass="heading">SpotiPy Downloader</h1>
<p>Download Link: <a href="{file_url}">{file_url}</a> </p>
<p>The file will be available on the above link for 24hours.</p>
<p>Hey {name.title()}! Your playlist is ready Thank you for using SpotiPy Downloader. I'd love to know how you found the <br>experience of using the service so would like to invite you to rate on <a href="https://forms.gle/33zWczLqooorKUiA8">Google Forms</a><br> - it'll only take a few clicks and will be invaluable to me!
</p>
<div class="form">
<form action="https://forms.gle/33zWczLqooorKUiA8">
<button type="submit">FEEDBACK FORM</button>
</form>
<p>ZipFile support link: <a href="https://docs.google.com/document/d/1hWC_WXkM7LTVKbOCcIcA76YJjdG3dom78GVXhPqRWf0/edit?usp=sharing"> https://docs.google.com/document/d/1hWC_WXkM7LTVKbOCcIcA76YJjdG3dom78GVXhPqRWf0/edit?usp=sharing</a> </p>
<p class="note"> Note: Some songs might be missing and some wrong audio files might be present in the playlist.
</div>
</body>
</html>
"""
message.add_alternative(html_message, subtype='html')
password = ""
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login('@gmail.com', password)
server.sendmail('@gmail.com', user['email'].lower(), message.as_string())
server.quit()
print("Link mailed")
try:
os.remove(f"{user['email'].lower()}.zip")
shutil.rmtree(directory)
except:
try:
os.remove(f"{user['email'].lower()}.zip")
shutil.rmtree(directory)
except:
try:
os.remove(f"{user['email'].lower()}.zip")
shutil.rmtree(directory)
except:
pass