mirror of
https://github.com/THIS-IS-NOT-A-BACKUP/zspotify.git
synced 2024-11-26 09:53:17 +01:00
Rewrote the search function
It still works in the same way and no other functions need to be changed. Just changed it to store needed data about each track/album/playlist in a dictionary so tracking it in the end is simpler.
This commit is contained in:
parent
3c3a2acf27
commit
7d81eb5cc6
169
zspotify.py
169
zspotify.py
@ -351,88 +351,143 @@ def search(search_term):
|
|||||||
""" Searches Spotify's API for relevant data """
|
""" Searches Spotify's API for relevant data """
|
||||||
token = SESSION.tokens().get("user-read-email")
|
token = SESSION.tokens().get("user-read-email")
|
||||||
|
|
||||||
|
params = {
|
||||||
|
"limit" : "10",
|
||||||
|
"q" : search_term,
|
||||||
|
"type" : ["track","album","playlist"]
|
||||||
|
}
|
||||||
|
|
||||||
|
# ADD BLOCK FOR READING OPTIONS AND CLEAN SEARCH_TERM
|
||||||
|
|
||||||
resp = requests.get(
|
resp = requests.get(
|
||||||
"https://api.spotify.com/v1/search",
|
"https://api.spotify.com/v1/search",
|
||||||
{
|
{
|
||||||
"limit": "10",
|
"limit": params["limit"],
|
||||||
"offset": "0",
|
"offset": "0",
|
||||||
"q": search_term,
|
"q": params["q"],
|
||||||
"type": "track,album,playlist"
|
"type": ",".join(params["type"])
|
||||||
},
|
},
|
||||||
headers={"Authorization": "Bearer %s" % token},
|
headers={"Authorization": "Bearer %s" % token},
|
||||||
)
|
)
|
||||||
|
|
||||||
# print(resp.json())
|
# print(resp.json())
|
||||||
|
|
||||||
i = 1
|
enum = 1
|
||||||
tracks = resp.json()["tracks"]["items"]
|
dics = []
|
||||||
if len(tracks) > 0:
|
|
||||||
print("### TRACKS ###")
|
# add all returned tracks to dics
|
||||||
for track in tracks:
|
if "track" in params["type"]:
|
||||||
if track["explicit"]:
|
tracks = resp.json()["tracks"]["items"]
|
||||||
explicit = "[E]"
|
if len(tracks) > 0:
|
||||||
else:
|
print("### TRACKS ###")
|
||||||
explicit = ""
|
for track in tracks:
|
||||||
print("%d, %s %s | %s" % (
|
if track["explicit"]:
|
||||||
i,
|
explicit = "[E]"
|
||||||
track["name"],
|
else:
|
||||||
explicit,
|
explicit = ""
|
||||||
",".join([artist["name"] for artist in track["artists"]]),
|
# collect needed data
|
||||||
))
|
dic = {
|
||||||
i += 1
|
"id" : track["id"],
|
||||||
total_tracks = i - 1
|
"name" : track["name"],
|
||||||
|
"artists/owner" : [artist["name"] for artist in track["artists"]],
|
||||||
|
"type" : "track",
|
||||||
|
}
|
||||||
|
dics.append(dic)
|
||||||
|
|
||||||
|
print("{}, {} {} | {}".format(
|
||||||
|
enum,
|
||||||
|
dic["name"],
|
||||||
|
explicit,
|
||||||
|
",".join(dic["artists/owner"]),
|
||||||
|
))
|
||||||
|
enum += 1
|
||||||
|
total_tracks = enum - 1
|
||||||
print("\n")
|
print("\n")
|
||||||
|
# free up memory
|
||||||
|
del tracks
|
||||||
else:
|
else:
|
||||||
total_tracks = 0
|
total_tracks = 0
|
||||||
|
|
||||||
albums = resp.json()["albums"]["items"]
|
if "album" in params["type"]:
|
||||||
if len(albums) > 0:
|
albums = resp.json()["albums"]["items"]
|
||||||
print("### ALBUMS ###")
|
if len(albums) > 0:
|
||||||
for album in albums:
|
print("### ALBUMS ###")
|
||||||
print("%d, %s | %s" % (
|
for album in albums:
|
||||||
i,
|
# collect needed data
|
||||||
album["name"],
|
dic = {
|
||||||
",".join([artist["name"] for artist in album["artists"]]),
|
"id" : album["id"],
|
||||||
))
|
"name" : album["name"],
|
||||||
i += 1
|
"artists/owner" : [artist["name"] for artist in album["artists"]],
|
||||||
total_albums = i - total_tracks - 1
|
"type" : "album",
|
||||||
|
}
|
||||||
|
dics.append(dic)
|
||||||
|
|
||||||
|
print("{}, {} | {}".format(
|
||||||
|
enum,
|
||||||
|
dic["name"],
|
||||||
|
",".join(dic["artists/owner"]),
|
||||||
|
))
|
||||||
|
enum += 1
|
||||||
|
total_albums = enum - total_tracks - 1
|
||||||
print("\n")
|
print("\n")
|
||||||
|
# free up memory
|
||||||
|
del albums
|
||||||
else:
|
else:
|
||||||
total_albums = 0
|
total_albums = 0
|
||||||
|
|
||||||
playlists = resp.json()["playlists"]["items"]
|
if "playlist" in params["type"]:
|
||||||
print("### PLAYLISTS ###")
|
playlists = resp.json()["playlists"]["items"]
|
||||||
for playlist in playlists:
|
print("### PLAYLISTS ###")
|
||||||
print("%d, %s | %s" % (
|
if len(playlists) > 0:
|
||||||
i,
|
for playlist in playlists:
|
||||||
playlist["name"],
|
# collect needed data
|
||||||
playlist['owner']['display_name'],
|
dic = {
|
||||||
))
|
"id" : playlist["id"],
|
||||||
i += 1
|
"name" : playlist["name"],
|
||||||
print("\n")
|
"artists/owner" : [playlist["owner"]["display_name"]],
|
||||||
|
"type" : "playlist",
|
||||||
|
}
|
||||||
|
dics.append(dic)
|
||||||
|
|
||||||
if len(tracks) + len(albums) + len(playlists) == 0:
|
print("{}, {} | {}".format(
|
||||||
|
enum,
|
||||||
|
dic["name"],
|
||||||
|
",".join(dic['artists/owner']),
|
||||||
|
))
|
||||||
|
|
||||||
|
enum += 1
|
||||||
|
total_playlists = enum - total_tracks - total_albums - 1
|
||||||
|
print("\n")
|
||||||
|
# free up memory
|
||||||
|
del playlists
|
||||||
|
else:
|
||||||
|
total_playlists = 0
|
||||||
|
|
||||||
|
if total_tracks + total_albums + total_playlists == 0:
|
||||||
print("NO RESULTS FOUND - EXITING...")
|
print("NO RESULTS FOUND - EXITING...")
|
||||||
else:
|
else:
|
||||||
selection = str(input("SELECT ITEM(S) BY ID: "))
|
selection = str(input("SELECT ITEM(S) BY ID: "))
|
||||||
inputs = split_input(selection)
|
inputs = split_input(selection)
|
||||||
for pos in inputs:
|
for pos in inputs:
|
||||||
position = int(pos)
|
position = int(pos)
|
||||||
if position <= total_tracks:
|
for dic in dics:
|
||||||
track_id = tracks[position - 1]["id"]
|
# find dictionary
|
||||||
download_track(track_id)
|
print_pos = dics.index(dic) + 1
|
||||||
elif position <= total_albums + total_tracks:
|
if print_pos == position:
|
||||||
download_album(albums[position - total_tracks - 1]["id"])
|
# if request is for track
|
||||||
else:
|
if dic["type"] == "track":
|
||||||
playlist_choice = playlists[position -
|
download_track(dic["id"])
|
||||||
total_tracks - total_albums - 1]
|
# if request is for album
|
||||||
playlist_songs = get_playlist_songs(
|
if dic["type"] == "album":
|
||||||
token, playlist_choice['id'])
|
download_album(dic["id"])
|
||||||
for song in playlist_songs:
|
# if request is for playlist
|
||||||
if song['track']['id'] is not None:
|
if dic["type"] == "playlist":
|
||||||
download_track(song['track']['id'], sanitize_data(
|
playlist_songs = get_playlist_songs(token, dic["id"])
|
||||||
playlist_choice['name'].strip()) + "/")
|
for song in playlist_songs:
|
||||||
print("\n")
|
if song["track"]["id"] is not None:
|
||||||
|
download_track(song["track"]["id"],
|
||||||
|
sanitize_data(dic["name"].strip()) + "/")
|
||||||
|
print("\n")
|
||||||
|
|
||||||
|
|
||||||
def get_song_info(song_id):
|
def get_song_info(song_id):
|
||||||
|
Loading…
Reference in New Issue
Block a user