-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
276 lines (212 loc) · 8.3 KB
/
main.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
import locale
import sys
from typing import Any
import click
import mpv
import requests
import spotipy
from dotenv import load_dotenv
from spotipy.oauth2 import SpotifyOAuth
from ytmusicapi import YTMusic
def get_tracks(sp: spotipy.Spotify, query: str) -> dict[str, list[str]]:
names = []
links = []
artists = []
results: Any = sp.search(query, type="track", limit=5)
for item in results["tracks"]["items"]:
names.append(item["name"])
links.append(item["external_urls"]["spotify"])
for artist in item["artists"]:
artists.append(artist["name"])
return {"names": names, "links": links, "artists": artists}
def get_artists(sp: spotipy.Spotify, query: str) -> dict[str, list[str]]:
names = []
links = []
results: Any = sp.search(query, type="artist", limit=5)
for items in results["artists"]["items"]:
names.append(items["name"])
links.append(items["external_urls"]["spotify"])
return {"names": names, "links": links}
def get_artist_albums(sp: spotipy.Spotify, link: str) -> dict[str, list[str]]:
names = []
links = []
results: Any = sp.artist_albums(artist_id=link)
for item in results["items"]:
names.append(item["name"])
links.append(item["external_urls"]["spotify"])
return {"names": names, "links": links}
def get_artist_top_tracks(sp: spotipy.Spotify, link: str) -> dict[str, list[str]]:
names = []
links = []
results: Any = sp.artist_top_tracks(artist_id=link)
for item in results["tracks"]:
names.append(item["name"])
links.append(item["external_urls"]["spotify"])
return {"names": names, "links": links}
def get_related_artists(sp: spotipy.Spotify, link: str) -> dict[str, list[str]]:
names = []
links = []
results: Any = sp.artist_related_artists(artist_id=link)
for item in results["artists"]:
names.append(item["name"])
links.append(item["external_urls"]["spotify"])
return {"names": names, "links": links}
def get_albums(sp: spotipy.Spotify, query: str) -> dict[str, list[str]]:
names = []
links = []
results: Any = sp.search(query, type="album", limit=5)
for items in results["albums"]["items"]:
names.append(items["name"])
links.append(items["external_urls"]["spotify"])
return {"names": names, "links": links}
def get_album_tracks(sp: spotipy.Spotify, query: str) -> dict[str, list[str]]:
names = []
links = []
results: Any = sp.album_tracks(album_id=query)
for item in results["items"]:
names.append(item["name"])
links.append(item["external_urls"]["spotify"])
return {"names": names, "links": links}
def get_playlist_tracks(sp: spotipy.Spotify, id: str) -> dict[str, list[str]]:
names = []
links = []
results: Any = sp.playlist_items(id)
for item in results["items"]:
names.append(item["track"]["name"])
links.append(item["track"]["external_urls"]["spotify"])
return {"names": names, "links": links}
def get_user_playlists(sp: spotipy.Spotify) -> dict[str, list[str]]:
names = []
links = []
results: Any = sp.current_user_playlists(limit=5)
for item in results["items"]:
names.append(item["name"])
links.append(item["external_urls"]["spotify"])
return {"names": names, "links": links}
pause_status = True
def draw_seekbar() -> None:
sys.stdout.write("\r") # Move the cursor to the start of the line
global pause_status
if pause_status:
sys.stdout.write("||")
pause_status = False
else:
sys.stdout.write("|>")
pause_status = True
sys.stdout.flush() # Ensure the output is written immediately
def fetch_jiosaavn(query: str) -> str:
song_link = ""
links = []
explicit_content = []
url = "https://saavn.dev/api/search/songs"
response = requests.get(url, params={"query": query}).json()
for results in response["data"]["results"]:
links.append(results["url"])
explicit_content.append(results["explicitContent"])
songs = {"links": links, "explicit_content": explicit_content}
explicit_status = True
for index, explicit_content in enumerate(songs["explicit_content"]):
if explicit_content == explicit_status:
song_link = songs["links"][index]
break
elif song_link == "":
song_link = songs["links"][index]
return song_link
def fetch_ytmusic(query: str) -> str | None:
yt = YTMusic("browser.json")
song_id = ""
video_ids = []
explicit_content = []
response = yt.search(query=query, filter="songs")
for results in response:
video_ids.append(results["videoId"])
explicit_content.append(results["isExplicit"])
songs = {"video_ids": video_ids, "explicit_content": explicit_content}
explicit_status = True
for index, explicit_content in enumerate(songs["explicit_content"]):
if explicit_content == explicit_status:
song_id = songs["video_ids"][index]
break
elif song_id == "":
song_id = songs["video_ids"][index]
return f"https://music.youtube.com/watch?v={song_id}"
def playback(song_link: str) -> None:
current_locale = locale.getlocale()
if current_locale != ("C", None):
locale.setlocale(locale.LC_ALL, "C")
player = mpv.MPV(ytdl=True, video=False)
print(f"Starting playback of: {song_link}")
player.play(song_link)
while True:
try:
draw_seekbar()
c = click.getchar()
if c == " ":
player.pause = not player.pause
elif c == "q":
break
except Exception as e:
print(f"Playback error: {e}")
break
def main() -> None:
load_dotenv()
scope = "playlist-read-private,user-library-read"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
print("Welcome to Symphony!")
print("1) Home")
print("2) Search Songs")
print("3) Search Albums")
print("4) Search Artists")
option = int(input("Select an option to use: "))
if option == 2:
song_query = input("Enter a song name: ")
tracks = get_tracks(sp, query=song_query)
print("SONGS:")
for index, (name, artist) in enumerate(zip(tracks["names"], tracks["artists"])):
print(f"{index+1} - {name} by {artist}")
print("-" * 50)
index_input = int(input("Enter song index that you want to play: "))
index_input -= 1
song_link = fetch_jiosaavn(
query=f'{tracks["names"][index_input]} - {tracks["artists"][index_input]}'
)
playback(song_link)
if option == 3:
album_query = input("Enter an album: ")
albums = get_albums(sp, query=album_query)
print("ALBUMS:")
for index, name in enumerate(albums["names"]):
print(f"{index+1} - {name}")
print("-" * 50)
index_input = int(input("Enter album index: "))
index_input -= 1
album_tracks = get_album_tracks(sp, query=albums["links"][index_input])
print("ALBUM TRACKS:")
for index, album_track in enumerate(album_tracks["names"]):
print(f"{index+1} - {album_track}")
if option == 4:
artist_query = input("Enter an artist: ")
artists = get_artists(sp, query=artist_query)
print("ARTISTS:")
for index, name in enumerate(artists["names"]):
print(f"{index+1} - {name}")
print("-" * 50)
index_input = int(input("Enter artist index: "))
index_input -= 1
artist_tracks = get_artist_top_tracks(sp, artists["links"][index_input])
artist_albums = get_artist_albums(sp, artists["links"][index_input])
related_artists = get_related_artists(sp, artists["links"][index_input])
print("ARTIST TRACKS:")
for index, artist_track in enumerate(artist_tracks["names"]):
print(f"{index+1} - {artist_track}")
print("-" * 50)
print("ARTIST ALBUMS:")
for index, artist_albums in enumerate(artist_albums["names"]):
print(f"{index+1} - {artist_albums}")
print("-" * 50)
print("RELATED ARTISTS:")
for index, related_artist in enumerate(related_artists["names"]):
print(f"{index+1} - {related_artist}")
print("-" * 50)
if __name__ == "__main__":
main()