-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeezer_it.py
360 lines (324 loc) · 11.1 KB
/
deezer_it.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
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
import deezloader
from deezloader.utils import generate_token
import requests
import os
import glob
def global_search(query):
global final_res, ind_search, res_search, link_search, links, raw_response
q = '\"' + query + '\"'
offset = 0
again = False
new = None
while True:
if again:
q = '\"' + input('= ') + '\"'
new = None
token = generate_token()
# print(token)
# My Search
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer {token}',
}
params = {
'q': q,
'type': 'track',
'limit': 5,
'offset': offset
}
try:
raw_response = requests.get('https://api.spotify.com/v1/search', headers=headers, params=params)
except:
print('Impossibile raggiungere il servizio, controlla la tua connessione...')
input('Premi \'Invio\' per chiudere il programma')
quit()
# print(raw_response.url)
response = raw_response.json()
# print(response)
if len(response['tracks']['items']) == 0:
new = input('Non ci sono più risultati, riprovare (vuoto = si, e = exit)?\n')
if new == 'e':
print('\nOk, alla prossima!')
exit()
elif new == '':
again = True
continue
index = 1
for sg in response['tracks']['items']:
artist = sg['artists'][0]['name']
song = sg['name']
print(f'{index}) {artist} - {song}')
index += 1
# print(res)
ind = 1
links = {}
for link in response['tracks']['items']:
url = link['external_urls']['spotify']
links.setdefault(ind, url)
ind += 1
# print(ind_search)
# print(res_search)
final_res = input('\nQuale canzone (inserisci numero, lasciare vuoto per riprovare, n = next, e = exit)?\n')
if len(final_res) == 0:
again = True
offset = 0
elif final_res == 'n':
offset += 5
again = False
elif final_res == 'e':
print('\nOk, alla prossima!')
quit()
elif int(final_res) not in range(index):
print('Numero invalido :(')
continue
else:
break
link = links[int(final_res)]
return link
def settings():
while True:
with open('settings.txt', 'r') as settings:
lines = []
for line in settings:
print(line, end='')
lines.append(line)
setting = input('\nQuale opzione vuoi cambiare?(e = exit)\n')
if setting == 'e':
break
elif setting == '1':
if 'True' in lines[0]:
new = lines[0].replace('True', 'False')
lines.pop(0)
lines.insert(0, new)
else:
new = lines[0].replace('False', 'True')
lines.pop(0)
lines.insert(0, new)
elif setting == '2':
if 'True' in lines[1]:
new = lines[1].replace('True', 'False')
lines.pop(1)
lines.insert(1, new)
else:
new = lines[1].replace('False', 'True')
lines.pop(1)
lines.insert(1, new)
elif setting == '3':
if 'True' in lines[2]:
new = lines[2].replace('True', 'False')
lines.pop(2)
lines.insert(2, new)
else:
new = lines[2].replace('False', 'True')
lines.pop(2)
lines.insert(2, new)
elif setting == '4':
path = input('Inserisci il nuovo percorso=\n')
lines.pop(2)
lines.insert(2, f'3) output = {path}\n')
elif setting == '5':
print(f'\n{generate_token()}\n')
else:
print('Non mi aspettavo questo... solo dal 1 al 4')
return
new_settings = ''.join(lines)
# print(new_settings)
rp = open('settings.txt', 'w')
rp.write(new_settings)
rp.close()
def get_settings():
with open('settings.txt', 'r') as raw_settings:
settings = []
ind = 0
for line in raw_settings:
if ind == 4:
break
settings.append(line.split('= ')[-1].strip())
ind += 1
# settings.pop(-1)
return settings
def check_output(output):
if output == 'NONE':
print('L\'output non è ancora stato modificato, inseriscine uno valido selezionando la quarta opzione.')
settings()
return
elif not os.path.isdir(output):
a = input('La cartella di output data non esiste, crearla? (y/n)\n')
if a == 'y':
checked = output
elif a == 'n':
new_out = input('nuovo output= ')
if not os.path.isdir(new_out):
print('Non importa, metti un output valido nelle opzioni o in quello temporaneo.')
quit()
checked = new_out
else:
print('Lo prendo come un sì')
checked = output
return checked
return output
while True:
# DOWNLOAD INFO
repeat, file_mover, ask_for_output, standard_output = get_settings()
# print(repeat, file_mover, standard_output)
repeat = str(repeat)
file_mover = str(file_mover)
ask_for_output = str(ask_for_output)
search_type = input('Ricerca globale (lasciare vuoto per ricerca specifica, --options = impostazioni):\n')
tracklink = None
albumlink = None
playlink = None
deezlink = None
link = ''
artist = ''
song = ''
out = ''
if search_type == '--options':
settings()
continue
elif 'https://open.spotify.com/' in search_type:
link = search_type
if '/track/' in search_type:
tracklink = True
albumlink = False
elif '/album/' in search_type:
albumlink = True
elif '/playlist/' in search_type:
playlink = True
albumlink = False
elif len(search_type) == 0:
song = input('Brano= ')
artist = input('Artista= ')
tracklink = False
albumlink = False
else:
link = global_search(search_type)
tracklink = True
albumlink = False
if ask_for_output == 'True':
out = input('Cambiare Output (lasciare vuoto per standard)?\n')
if len(out) == 0:
output = check_output(standard_output)
else:
output = check_output(out)
# DEEZER DOWNLOADER
try:
download = deezloader.Login(requests.get('https://pastebin.com/raw/LkP0Hbkq').text) # requests.get('https://pastebin.com/raw/LkP0Hbkq').text
except deezloader.exceptions.BadCredentials:
print('Invalid ARL token, please check you connection or try again later')
input('Premi \'Invio\' per chiudere il programma')
quit()
except Exception:
print('Trovato errore imprevisto...')
input('Premi \'Invio\' per chiudere il programma')
quit()
if tracklink:
try:
download.download_trackspo(
URL=link,
output=output,
quality="MP3_320",
recursive_quality=True,
recursive_download=False,
not_interface=False
)
except(deezloader.exceptions.TrackNotFound, deezloader.exceptions.NoDataApi, deezloader.exceptions.InvalidLink):
print('Brano Non Trovato :(\n')
input('Premi \'Invio\' per chiudere il programma')
quit()
except Exception:
print('Trovato errore imprevisto...')
input('Premi \'Invio\' per chiudere il programma')
quit()
elif albumlink:
try:
download.download_albumspo(
URL=link,
output=output,
quality="MP3_320",
recursive_quality=True,
recursive_download=False,
not_interface=False,
zips=False
)
file_mover = False
except(deezloader.exceptions.TrackNotFound, deezloader.exceptions.NoDataApi, deezloader.exceptions.InvalidLink):
print('Brano Non Trovato :(\n')
input('Premi \'Invio\' per chiudere il programma')
quit()
except Exception:
print('Trovato errore imprevisto...')
input('Premi \'Invio\' per chiudere il programma')
quit()
elif playlink:
try:
download.download_playlistspo(
URL=link,
output=output,
quality="MP3_320",
recursive_quality=True,
recursive_download=False,
not_interface=False,
zips=False
)
file_mover = False
except(deezloader.exceptions.TrackNotFound, deezloader.exceptions.NoDataApi, deezloader.exceptions.InvalidLink):
print('Brano Non Trovato :(\n')
input('Premi \'Invio\' per chiudere il programma')
quit()
except Exception:
print('Trovato errore imprevisto...')
input('Premi \'Invio\' per chiudere il programma')
quit()
else:
try:
download.download_name(
artist=artist.lower(),
song=song.lower(),
output=output,
quality="MP3_320",
recursive_quality=True,
recursive_download=False,
not_interface=False
)
except(deezloader.exceptions.TrackNotFound, deezloader.exceptions.NoDataApi):
print('Brano Non Trovato :(\n')
input('Premi \'Invio\' per chiudere il programma')
quit()
except Exception:
print('Trovato errore imprevisto...')
input('Premi \'Invio\' per chiudere il programma')
quit()
# File Mover
if file_mover == 'True':
project_dir = os.getcwd()
files = glob.glob(f'{output}/*')
folder = max(files, key=os.path.getmtime)
os.chdir(folder)
list2 = os.listdir()
song_move = list2[0]
number = 1
"""for _ in range(len(files)):
if os.path.isfile(f'{output}/{number}.mp3'):
number += 1
else:
break
try:
os.rename(song_move, f'{output}/{number}.mp3')
except:
print('Brano non spostabile :(')
quit()"""
try:
os.rename(song_move, f'{output}/{song_move}')
except:
print('Brano non spostabile :(')
quit()
print('\nBrano spostato!')
os.chdir(project_dir)
os.rmdir(folder)
print('Cartella eliminata!')
if repeat == 'False':
break
print('\nFinito!\n')
quit()