-
Notifications
You must be signed in to change notification settings - Fork 0
/
deezer.py
461 lines (412 loc) · 16.4 KB
/
deezer.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
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
import requests
import pygame
from unidecode import unidecode
import os
import re
from bs4 import BeautifulSoup
import auth
import appdirs
import datetime
import gui
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import sys
import argparse
import sub
from pathlib import Path
import crypto
import datetime
import time
import updater
import ctypes
from tqdm import tqdm
# Variables
APP_NAME = 'speakDeezer'
appdata_dir = Path.home() / 'AppData' / 'Local' / 'speakDeezer'
HISTORY_FILE = appdata_dir/'history.txt'
USER_FILE = os.path.join(appdirs.user_data_dir(APP_NAME), 'users.txt')
API_BASE_URL = "https://api.deezer.com/"
USERNAME = auth.get_current_user()
def log_history(input_str):
# get current timestamp
if not os.path.exists(HISTORY_FILE):
# create history file if it doesn't exist
with open(HISTORY_FILE, 'w') as hf:
hf.write('')
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# log user input to history file
with open(HISTORY_FILE, 'a') as hf:
hf.write(f'{timestamp}: {USERNAME} - {input_str}\n')
def search_song(query):
log_history(query)
url = f"https://api.deezer.com/search?q={query}"
response = requests.get(url)
log_history(response)
return response.json()
def select_song(search_results):
print("Select a song from the list:")
for i, song in enumerate(search_results["data"]):
print(f"{i + 1}. {song['title']} by {song['artist']['name']}")
while True:
try:
user_choice = int(input("Enter the number of your choice: ")) - 1
if user_choice < 0 or user_choice >= len(search_results["data"]):
print("Invalid choice. Please try again.")
else:
return search_results["data"][user_choice]
except ValueError:
print("Invalid input. Please enter a number.")
def download_song(song):
song_url = song["preview"]
response = requests.get(song_url)
if response.status_code != 200:
print(f"Failed to download song {song['title']} - Status code: {response.status_code}")
return None
filename = f"{song['title']}.mp3"
try:
music_dir = os.path.join(os.path.expanduser("~"), "Music")
os.makedirs(music_dir, exist_ok=True)
filepath = os.path.join(music_dir + "/speakDeezer/", filename)
with open(filepath, "wb") as f:
f.write(response.content)
except IOError:
print(f"Failed to write song {song['title']} to file")
return None
return filepath
def play_song(filename):
try:
pygame.mixer.init()
pygame.mixer.music.load(filename)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
except Exception as e:
print(e)
def search_and_play():
query = input("Enter the name of the song you want to search: ")
search_results = search_song(query)
if len(search_results["data"]) == 0:
print("No results found.")
else:
selected_song = select_song(search_results)
print(f"Now downloading {selected_song['title']} by {selected_song['artist']['name']}...")
filename = download_song(selected_song)
print(filename)
play_song(filename)
def select_song_for_lyrics():
query = input("Enter the name of the song for which you want to see the lyrics: ")
search_results = search_song(query)
if "data" not in search_results:
print("An error occurred while retrieving the search results.")
return None
elif len(search_results["data"]) == 0:
print("No results found.")
return None
else:
selected_song = select_song(search_results)
return selected_song
def sanitize_name(name):
"""Removes all non-alphabetic characters from a string."""
return re.sub(r'[^a-zA-Z]', '', name)
def show_lyrics():
song = select_song_for_lyrics()
if song:
title = song["title"]
artist = unidecode(song["artist"]["name"]).lower().replace(" ", "")
title_normalized = unidecode(title).lower().replace(" ", "")
print(f"Lyrics for {title} by {song['artist']['name']}:")
url = f"https://www.azlyrics.com/lyrics/{artist}/{title_normalized}.html"
try:
response = requests.get(url)
response.raise_for_status()
except requests.exceptions.HTTPError:
print("An error occurred while retrieving the lyrics.")
return None
soup = BeautifulSoup(response.content, "html.parser")
lyrics_div = soup.find("div", class_=False, id=False)
if lyrics_div:
lyrics = lyrics_div.get_text().strip()
print(lyrics)
else:
print("Lyrics not found.")
def search_songs_by_artist(artist):
response = requests.get(f"{API_BASE_URL}/search?q={artist}&limit=20&type=artist")
if response.status_code == 200:
data = response.json()["data"]
if len(data) > 0:
artist_data = data[0].get("artist", None)
if artist_data:
artist_id = artist_data.get("id", None)
if artist_id:
songs_response = requests.get(f"{API_BASE_URL}/artist/{artist_id}/top?limit=20")
if songs_response.status_code == 200:
songs = songs_response.json().get("data", None)
if songs:
if len(songs) > 0:
return songs
else:
print(f"No songs found for artist {artist}.")
else:
print(f"Error: Could not retrieve songs for artist {artist}.")
else:
print(f"Error: Could not retrieve songs for artist {artist}.")
else:
print(f"Error: Artist ID not found for artist {artist}.")
else:
print(f"Error: Artist data not found for artist {artist}.")
else:
print(f"No artist found with name {artist}.")
else:
print("Error retrieving search results.")
def search_songs_by_genre(genre):
response = requests.get(f"{API_BASE_URL}/search?q={genre}&limit=20&type=genre")
if response.status_code == 200:
try:
data = response.json()["data"]
except KeyError:
print(f"Error parsing response data for genre {genre}.")
return None
if len(data) > 0:
genre_id = data[0]["id"]
songs_response = requests.get(f"{API_BASE_URL}/genres/{genre_id}/songs?limit=20")
if songs_response.status_code == 200:
try:
songs = songs_response.json()["data"]
except KeyError:
print(f"Error parsing response data for songs in genre {genre}.")
return None
if len(songs) > 0:
return songs
else:
print(f"No songs found for genre {genre}.")
else:
print(f"Error retrieving songs for genre {genre}.")
else:
print(f"No genre found with name {genre}.")
else:
print("Error retrieving search results.")
def play_songs_by_artist():
artist_name = input("Enter the name of the artist: ")
songs = search_songs_by_artist(artist_name)
if songs is None:
print(f"Error: Could not retrieve songs by {artist_name}.")
return
elif not songs:
print(f"No songs found by {artist_name}.")
return
print(f"Found {len(songs)} songs by {artist_name}:")
for i, song in enumerate(songs):
print(f"{i + 1}. {song['title']} by {song['artist']['name']}")
try:
song_choice = int(input("Enter the number of the song you want to play: "))
except ValueError:
print("Invalid input. Please enter a number.")
return
if song_choice < 1 or song_choice > len(songs):
print("Invalid song choice.")
return
song = songs[song_choice - 1]
play_song(download_song(song))
def play_songs_by_genre():
genre_name = input("Enter the name of the genre: ")
log_history(f"{genre_name}")
songs = search_songs_by_genre(genre_name)
if not songs:
print(f"No songs found in the {genre_name} genre.")
return
print(f"Found {len(songs)} songs in the {genre_name} genre:")
for i, song in enumerate(songs):
print(f"{i + 1}. {song['title']} by {song['artist']['name']}")
log_history(f"{i + 1}. {song['title']} by {song['artist']['name']}")
try:
song_choice = int(input("Enter the number of the song you want to play: "))
if song_choice < 1 or song_choice > len(songs):
print("Invalid song choice.")
log_history("Invalid song choice.")
return
except ValueError:
print("Invalid input. Please enter a number.")
log_history("Invalid input. Please enter a number.")
return
song = songs[song_choice - 1]
play_song(song)
def show_recommendations():
print("Recommendations are not available at this time.")
def exit_program():
print("Exiting program.")
pygame.mixer.music.stop()
pygame.quit()
quit()
substates = {
"Pro",
"Invalid",
"Trail"
}
def main(user):
valid = sub.check_subscription(user)
os.system(f"title speakDeezer({valid})")
substate = ""
if(valid == "valid"):
substate = "Pro"
elif(valid.startswith == "trial"):
substate = valid
elif(valid == "invalid"):
substate = "Invalid"
python = sys.executable
os.execl(python, python, *sys.argv)
pygame.init()
while True:
os.system('cls' if os.name == 'nt' else 'clear')
print(f"Welcome {user}, what would you like to do?\n")
print("1. Search for a song and play it")
print("2. Show song lyrics")
print("3. Show youre recommendations")
print("4. Play songs by artist")
print("5. Play songs by genre")
print("6. Exit program")
print(f"\nLogout({user} - {substate})")
sub.write_subscriptions()
try:
user_choice = int(input("Enter the number of your choice: "))
if user_choice == 1:
log_history(f"{user_choice}")
search_and_play()
input("Press Enter to continue...")
os.system('cls' if os.name == 'nt' else 'clear')
elif user_choice == 2:
log_history(f"{user_choice}")
show_lyrics()
input("Press Enter to continue...")
os.system('cls' if os.name == 'nt' else 'clear')
elif user_choice == 3:
log_history(f"{user_choice}")
show_recommendations()
input("Press Enter to continue...")
os.system('cls' if os.name == 'nt' else 'clear')
elif user_choice == 4:
log_history(f"{user_choice}")
play_songs_by_artist()
input("Press Enter to continue...")
os.system('cls' if os.name == 'nt' else 'clear')
elif user_choice == 5:
log_history(f"{user_choice}")
print("This featute is not available at this time.")
elif user_choice == 6:
exit_program()
break
elif user_choice == 7:
log_history(f"{user_choice}")
auth.logout()
python = sys.executable
os.execl(python, python, *sys.argv)
elif user_choice == 69:
history_folder = os.path.dirname(HISTORY_FILE)
os.startfile(history_folder)
break
elif user_choice == 68:
try:
print(os.path.join(appdirs.user_data_dir(APP_NAME), 'users.txt'))
input("Press Enter to continue...")
except Exception as e:
print(e)
else:
print("Invalid choice. Please try again.")
except ValueError:
print("Invalid input. Please enter a number.")
def boot_up_animation():
print("Starting Deezer Player...")
time.sleep(0.1)
print("\nLoading...\n")
animations = ["|/-\\", "▖▘-◑-▗", "◢◣◤◥", "▌▄▐▀", "╰( ⁰ ਊ ⁰ )━☆゚.*・。゚"]
animation_idx = 0
api_available = check_api_availability()
while not api_available:
animation = animations[animation_idx % len(animations)]
print("Checking API availability " + animation, end="\r")
animation_idx += 1
time.sleep(0.1)
api_available = check_api_availability()
print("\nAPI is ready!")
music_dir = os.path.join(os.path.expanduser("~"), "Music")
if not os.path.exists(music_dir + "/speakDeezer/"):
os.makedirs(music_dir + "/speakDeezer/")
print("Created 'speakDezzer' directory for storing songs/lysrics/playlist and keys.")
input("Press Enter to continue...")
os.system('cls' if os.name == 'nt' else 'clear')
def create_playlist(username, playlist_name):
"""Creates a new playlist for the user with the given username."""
# Save the playlist to a file or database associated with the username
pass
def save_playlist(username, playlist_name):
"""Saves an existing playlist for the user with the given username."""
# Save the playlist to a file or database associated with the username
pass
def load_playlist(username, playlist_name):
"""Loads a playlist for the user with the given username."""
# Load the playlist from a file or database associated with the username
pass
def check_api_availability(base_url=API_BASE_URL):
try:
response = requests.get(f"{base_url}/ping")
response.raise_for_status()
print("Deezer API is available.")
return True
except requests.exceptions.RequestException:
print("Deezer API is not available.")
return False
def login():
while True:
current_user = auth.get_current_user()
if not current_user:
# prompt user to log in or register
while True:
choice = input('Please log in (1) or register (2): ')
if choice == '1':
# prompt user to enter username and password
username = input('Username: ')
password = input('Password: ')
# log in user
if auth.login(username, password):
USERNAME = current_user
return username
break
elif choice == '2':
# prompt user to enter desired username and password
username = input('Desired username: ')
password = input('Desired password: ')
# register user
if auth.register(username, password):
USERNAME = current_user
return username
break
else:
print('Invalid choice')
else:
USERNAME = current_user
return current_user
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def run_as_admin():
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='speakDeezer')
parser.add_argument('query', nargs='?', help='search query or track id')
parser.add_argument('--gui', action='store_true', help='start with GUI')
args = parser.parse_args()
if(args.gui):
app = QApplication([])
window = gui.App()
app.exec_()
else:
os.system('cls' if os.name == 'nt' else 'clear')
print("Started Updater...")
updater.check_update_and_download('JWLMT88', 'speakDeezer', 'deezer.py')
updater.check_update_and_download('JWLMT88', 'speakDeezer', 'gui.py')
input("Press enter to continue")
os.system('cls' if os.name == 'nt' else 'clear')
boot_up_animation()
main(login())