2021-10-19 21:38:04 +02:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
"""
|
|
|
|
ZSpotify
|
|
|
|
It's like youtube-dl, but for Spotify.
|
2021-10-22 10:34:17 +02:00
|
|
|
|
|
|
|
(Made by Deathmonger/Footsiefat - @doomslayer117:matrix.org)
|
2021-10-19 08:18:58 +02:00
|
|
|
"""
|
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
from getpass import getpass
|
2021-10-19 08:18:58 +02:00
|
|
|
import json
|
2021-10-13 09:22:22 +02:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import platform
|
|
|
|
import re
|
|
|
|
import sys
|
2021-10-19 08:18:58 +02:00
|
|
|
import time
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-14 03:39:22 +02:00
|
|
|
from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality
|
2021-10-13 09:22:22 +02:00
|
|
|
from librespot.core import Session
|
2021-10-20 22:44:52 +02:00
|
|
|
from librespot.metadata import TrackId, EpisodeId
|
2021-10-23 10:16:53 +02:00
|
|
|
import music_tag
|
2021-10-13 09:22:22 +02:00
|
|
|
from pydub import AudioSegment
|
2021-10-23 10:16:53 +02:00
|
|
|
import requests
|
2021-10-22 04:34:20 +02:00
|
|
|
from tqdm import tqdm
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
QUALITY = None
|
2021-10-19 08:18:58 +02:00
|
|
|
SESSION: Session = None
|
2021-10-23 10:16:53 +02:00
|
|
|
SANITIZE = ["\\", "/", ":", "*", "?", "'", "<", ">", "\""]
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-23 08:28:23 +02:00
|
|
|
# user-customizable variables that adjust the core functionality of ZSpotify
|
2021-10-23 10:16:53 +02:00
|
|
|
with open("zs_config.json", encoding="utf-8") as config_file:
|
2021-10-23 08:28:23 +02:00
|
|
|
ZS_CONFIG = json.load(config_file)
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-22 17:14:13 +02:00
|
|
|
|
2021-10-21 09:18:07 +02:00
|
|
|
# miscellaneous functions for general use
|
2021-10-22 07:23:38 +02:00
|
|
|
|
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
def clear():
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Clear the console window """
|
2021-10-13 09:22:22 +02:00
|
|
|
if platform.system() == "Windows":
|
|
|
|
os.system("cls")
|
|
|
|
else:
|
|
|
|
os.system("clear")
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
def wait(seconds: int = 3):
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Pause for a set number of seconds """
|
2021-10-13 09:22:22 +02:00
|
|
|
for i in range(seconds)[::-1]:
|
2021-10-23 10:16:53 +02:00
|
|
|
print(f"\rWait for {i + 1} second(s)...", end="")
|
2021-10-13 09:22:22 +02:00
|
|
|
time.sleep(1)
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
|
|
|
|
def sanitize_data(value):
|
|
|
|
""" Returns given string with problematic removed """
|
2021-10-23 10:16:53 +02:00
|
|
|
for i in SANITIZE:
|
2021-10-19 08:52:28 +02:00
|
|
|
value = value.replace(i, "")
|
|
|
|
return value.replace("|", "-")
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-22 07:17:05 +02:00
|
|
|
def split_input(selection):
|
|
|
|
""" Returns a list of inputted strings """
|
|
|
|
inputs = []
|
|
|
|
if "-" in selection:
|
|
|
|
for number in range(int(selection.split("-")[0]), int(selection.split("-")[1]) + 1):
|
|
|
|
inputs.append(number)
|
|
|
|
else:
|
|
|
|
selections = selection.split(",")
|
|
|
|
for i in selections:
|
|
|
|
inputs.append(i.strip())
|
2021-10-23 10:16:53 +02:00
|
|
|
return inputs
|
2021-10-22 07:17:05 +02:00
|
|
|
|
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
def splash():
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Displays splash screen """
|
2021-10-22 10:34:17 +02:00
|
|
|
print("""
|
|
|
|
███████ ███████ ██████ ██████ ████████ ██ ███████ ██ ██
|
|
|
|
███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
███ ███████ ██████ ██ ██ ██ ██ █████ ████
|
|
|
|
███ ██ ██ ██ ██ ██ ██ ██ ██
|
|
|
|
███████ ███████ ██ ██████ ██ ██ ██ ██
|
|
|
|
""")
|
2021-10-13 09:22:22 +02:00
|
|
|
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
# two mains functions for logging in and doing client stuff
|
2021-10-13 09:22:22 +02:00
|
|
|
def login():
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Authenticates with Spotify and saves credentials to a file """
|
2021-10-23 10:16:53 +02:00
|
|
|
global SESSION # pylint: disable=global-statement
|
2021-10-13 09:22:22 +02:00
|
|
|
|
|
|
|
if os.path.isfile("credentials.json"):
|
|
|
|
try:
|
2021-10-19 08:18:58 +02:00
|
|
|
SESSION = Session.Builder().stored_file().create()
|
2021-10-13 09:22:22 +02:00
|
|
|
return
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
while True:
|
2021-10-19 21:16:15 +02:00
|
|
|
user_name = input("Username: ")
|
|
|
|
password = getpass()
|
2021-10-13 09:22:22 +02:00
|
|
|
try:
|
2021-10-19 08:18:58 +02:00
|
|
|
SESSION = Session.Builder().user_pass(user_name, password).create()
|
2021-10-13 09:22:22 +02:00
|
|
|
return
|
|
|
|
except RuntimeError:
|
|
|
|
pass
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
def client(): # pylint: disable=too-many-branches,too-many-statements
|
2021-10-19 09:16:25 +02:00
|
|
|
""" Connects to spotify to perform query's and get songs to download """
|
2021-10-23 10:16:53 +02:00
|
|
|
global QUALITY # pylint: disable=global-statement
|
2021-10-13 09:22:22 +02:00
|
|
|
splash()
|
2021-10-18 23:54:30 +02:00
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
token = SESSION.tokens().get("user-read-email")
|
2021-10-18 23:54:30 +02:00
|
|
|
|
2021-10-20 21:37:36 +02:00
|
|
|
if check_premium():
|
2021-10-22 10:34:17 +02:00
|
|
|
print("[ DETECTED PREMIUM ACCOUNT - USING VERY_HIGH QUALITY ]\n\n")
|
2021-10-19 08:18:58 +02:00
|
|
|
QUALITY = AudioQuality.VERY_HIGH
|
2021-10-18 23:54:30 +02:00
|
|
|
else:
|
2021-10-22 10:34:17 +02:00
|
|
|
print("[ DETECTED FREE ACCOUNT - USING HIGH QUALITY ]\n\n")
|
2021-10-19 08:18:58 +02:00
|
|
|
QUALITY = AudioQuality.HIGH
|
2021-10-18 23:54:30 +02:00
|
|
|
|
2021-10-23 10:04:02 +02:00
|
|
|
while True:
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
if sys.argv[1] == "-p" or sys.argv[1] == "--playlist":
|
|
|
|
download_from_user_playlist()
|
|
|
|
elif sys.argv[1] == "-ls" or sys.argv[1] == "--liked-songs":
|
|
|
|
for song in get_saved_tracks(token):
|
2021-10-23 10:16:53 +02:00
|
|
|
if not song["track"]["name"]:
|
2021-10-23 10:04:02 +02:00
|
|
|
print(
|
|
|
|
"### SKIPPING: SONG DOES NOT EXISTS ON SPOTIFY ANYMORE ###")
|
|
|
|
else:
|
2021-10-23 10:16:53 +02:00
|
|
|
download_track(song["track"]["id"], "Liked Songs/")
|
2021-10-23 10:04:02 +02:00
|
|
|
print("\n")
|
|
|
|
else:
|
|
|
|
track_id_str, album_id_str, playlist_id_str, episode_id_str, show_id_str, artist_id_str = regex_input_for_urls(
|
|
|
|
sys.argv[1])
|
|
|
|
|
|
|
|
if track_id_str is not None:
|
|
|
|
download_track(track_id_str)
|
|
|
|
elif artist_id_str is not None:
|
|
|
|
download_artist_albums(artist_id_str)
|
|
|
|
elif album_id_str is not None:
|
|
|
|
download_album(album_id_str)
|
|
|
|
elif playlist_id_str is not None:
|
|
|
|
playlist_songs = get_playlist_songs(token, playlist_id_str)
|
2021-10-23 10:16:53 +02:00
|
|
|
name, _ = get_playlist_info(token, playlist_id_str)
|
2021-10-23 10:04:02 +02:00
|
|
|
for song in playlist_songs:
|
2021-10-23 10:16:53 +02:00
|
|
|
download_track(song["track"]["id"],
|
2021-10-23 10:04:02 +02:00
|
|
|
sanitize_data(name) + "/")
|
|
|
|
print("\n")
|
|
|
|
elif episode_id_str is not None:
|
|
|
|
download_episode(episode_id_str)
|
|
|
|
elif show_id_str is not None:
|
|
|
|
for episode in get_show_episodes(token, show_id_str):
|
|
|
|
download_episode(episode)
|
2021-10-19 01:22:56 +02:00
|
|
|
else:
|
2021-10-23 10:04:02 +02:00
|
|
|
search_text = input("Enter search or URL: ")
|
|
|
|
|
2021-10-23 02:00:46 +02:00
|
|
|
track_id_str, album_id_str, playlist_id_str, episode_id_str, show_id_str, artist_id_str = regex_input_for_urls(
|
2021-10-23 10:04:02 +02:00
|
|
|
search_text)
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-21 04:13:16 +02:00
|
|
|
if track_id_str is not None:
|
2021-10-19 08:18:58 +02:00
|
|
|
download_track(track_id_str)
|
2021-10-21 22:27:02 +02:00
|
|
|
elif artist_id_str is not None:
|
|
|
|
download_artist_albums(artist_id_str)
|
2021-10-21 04:13:16 +02:00
|
|
|
elif album_id_str is not None:
|
2021-10-19 08:18:58 +02:00
|
|
|
download_album(album_id_str)
|
2021-10-21 04:13:16 +02:00
|
|
|
elif playlist_id_str is not None:
|
2021-10-19 08:18:58 +02:00
|
|
|
playlist_songs = get_playlist_songs(token, playlist_id_str)
|
2021-10-23 10:16:53 +02:00
|
|
|
name, _ = get_playlist_info(token, playlist_id_str)
|
2021-10-19 08:18:58 +02:00
|
|
|
for song in playlist_songs:
|
2021-10-23 10:16:53 +02:00
|
|
|
download_track(song["track"]["id"],
|
2021-10-19 08:18:58 +02:00
|
|
|
sanitize_data(name) + "/")
|
2021-10-18 13:04:27 +02:00
|
|
|
print("\n")
|
2021-10-21 04:13:16 +02:00
|
|
|
elif episode_id_str is not None:
|
2021-10-21 04:28:10 +02:00
|
|
|
download_episode(episode_id_str)
|
2021-10-22 00:21:56 +02:00
|
|
|
elif show_id_str is not None:
|
|
|
|
for episode in get_show_episodes(token, show_id_str):
|
|
|
|
download_episode(episode)
|
2021-10-23 10:04:02 +02:00
|
|
|
else:
|
|
|
|
search(search_text)
|
|
|
|
# wait()
|
2021-10-13 09:22:22 +02:00
|
|
|
|
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
def regex_input_for_urls(search_input): # pylint: disable=too-many-locals
|
|
|
|
""" Since many kinds of search may be passed at the command line, process them all here. """
|
2021-10-21 04:13:16 +02:00
|
|
|
track_uri_search = re.search(
|
|
|
|
r"^spotify:track:(?P<TrackID>[0-9a-zA-Z]{22})$", search_input)
|
|
|
|
track_url_search = re.search(
|
|
|
|
r"^(https?://)?open\.spotify\.com/track/(?P<TrackID>[0-9a-zA-Z]{22})(\?si=.+?)?$",
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
album_uri_search = re.search(
|
|
|
|
r"^spotify:album:(?P<AlbumID>[0-9a-zA-Z]{22})$", search_input)
|
|
|
|
album_url_search = re.search(
|
|
|
|
r"^(https?://)?open\.spotify\.com/album/(?P<AlbumID>[0-9a-zA-Z]{22})(\?si=.+?)?$",
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
playlist_uri_search = re.search(
|
|
|
|
r"^spotify:playlist:(?P<PlaylistID>[0-9a-zA-Z]{22})$", search_input)
|
|
|
|
playlist_url_search = re.search(
|
|
|
|
r"^(https?://)?open\.spotify\.com/playlist/(?P<PlaylistID>[0-9a-zA-Z]{22})(\?si=.+?)?$",
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
|
|
|
episode_uri_search = re.search(
|
|
|
|
r"^spotify:episode:(?P<EpisodeID>[0-9a-zA-Z]{22})$", search_input)
|
|
|
|
episode_url_search = re.search(
|
|
|
|
r"^(https?://)?open\.spotify\.com/episode/(?P<EpisodeID>[0-9a-zA-Z]{22})(\?si=.+?)?$",
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
2021-10-22 00:21:56 +02:00
|
|
|
show_uri_search = re.search(
|
|
|
|
r"^spotify:show:(?P<ShowID>[0-9a-zA-Z]{22})$", search_input)
|
|
|
|
show_url_search = re.search(
|
|
|
|
r"^(https?://)?open\.spotify\.com/show/(?P<ShowID>[0-9a-zA-Z]{22})(\?si=.+?)?$",
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
2021-10-21 22:13:39 +02:00
|
|
|
artist_uri_search = re.search(
|
|
|
|
r"^spotify:artist:(?P<ArtistID>[0-9a-zA-Z]{22})$", search_input)
|
|
|
|
artist_url_search = re.search(
|
|
|
|
r"^(https?://)?open\.spotify\.com/artist/(?P<ArtistID>[0-9a-zA-Z]{22})(\?si=.+?)?$",
|
|
|
|
search_input,
|
|
|
|
)
|
|
|
|
|
2021-10-21 04:13:16 +02:00
|
|
|
if track_uri_search is not None or track_url_search is not None:
|
|
|
|
track_id_str = (track_uri_search
|
|
|
|
if track_uri_search is not None else
|
|
|
|
track_url_search).group("TrackID")
|
|
|
|
else:
|
|
|
|
track_id_str = None
|
|
|
|
|
|
|
|
if album_uri_search is not None or album_url_search is not None:
|
|
|
|
album_id_str = (album_uri_search
|
|
|
|
if album_uri_search is not None else
|
|
|
|
album_url_search).group("AlbumID")
|
|
|
|
else:
|
|
|
|
album_id_str = None
|
|
|
|
|
|
|
|
if playlist_uri_search is not None or playlist_url_search is not None:
|
|
|
|
playlist_id_str = (playlist_uri_search
|
|
|
|
if playlist_uri_search is not None else
|
|
|
|
playlist_url_search).group("PlaylistID")
|
|
|
|
else:
|
|
|
|
playlist_id_str = None
|
|
|
|
|
|
|
|
if episode_uri_search is not None or episode_url_search is not None:
|
|
|
|
episode_id_str = (episode_uri_search
|
|
|
|
if episode_uri_search is not None else
|
|
|
|
episode_url_search).group("EpisodeID")
|
|
|
|
else:
|
|
|
|
episode_id_str = None
|
|
|
|
|
2021-10-22 00:21:56 +02:00
|
|
|
if show_uri_search is not None or show_url_search is not None:
|
|
|
|
show_id_str = (show_uri_search
|
|
|
|
if show_uri_search is not None else
|
|
|
|
show_url_search).group("ShowID")
|
|
|
|
else:
|
|
|
|
show_id_str = None
|
|
|
|
|
2021-10-21 22:13:39 +02:00
|
|
|
if artist_uri_search is not None or artist_url_search is not None:
|
|
|
|
artist_id_str = (artist_uri_search
|
2021-10-23 02:00:46 +02:00
|
|
|
if artist_uri_search is not None else
|
|
|
|
artist_url_search).group("ArtistID")
|
2021-10-21 22:13:39 +02:00
|
|
|
else:
|
|
|
|
artist_id_str = None
|
|
|
|
|
2021-10-22 20:21:57 +02:00
|
|
|
return track_id_str, album_id_str, playlist_id_str, episode_id_str, show_id_str, artist_id_str
|
2021-10-21 04:13:16 +02:00
|
|
|
|
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
def get_episode_info(episode_id_str): # pylint: disable=missing-function-docstring
|
2021-10-20 22:44:52 +02:00
|
|
|
token = SESSION.tokens().get("user-read-email")
|
2021-10-23 10:16:53 +02:00
|
|
|
info = json.loads(requests.get(f"https://api.spotify.com/v1/episodes/{episode_id_str}",
|
|
|
|
headers={"Authorization": f"Bearer {token}"}).text)
|
2021-10-20 22:44:52 +02:00
|
|
|
|
2021-10-21 02:12:07 +02:00
|
|
|
if "error" in info:
|
|
|
|
return None, None
|
2021-10-23 10:16:53 +02:00
|
|
|
# print(info["images"][0]["url"])
|
|
|
|
return sanitize_data(info["show"]["name"]), sanitize_data(info["name"])
|
2021-10-22 00:21:56 +02:00
|
|
|
|
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
def get_show_episodes(access_token, show_id_str): # pylint: disable=missing-function-docstring
|
2021-10-22 00:21:56 +02:00
|
|
|
episodes = []
|
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
2021-10-22 00:21:56 +02:00
|
|
|
resp = requests.get(
|
2021-10-23 10:16:53 +02:00
|
|
|
f"https://api.spotify.com/v1/shows/{show_id_str}/episodes", headers=headers).json()
|
2021-10-22 00:21:56 +02:00
|
|
|
|
|
|
|
for episode in resp["items"]:
|
|
|
|
episodes.append(episode["id"])
|
|
|
|
|
|
|
|
return episodes
|
2021-10-20 22:44:52 +02:00
|
|
|
|
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
def download_episode(episode_id_str): # pylint: disable=missing-function-docstring
|
2021-10-21 04:28:10 +02:00
|
|
|
podcast_name, episode_name = get_episode_info(episode_id_str)
|
2021-10-22 00:21:56 +02:00
|
|
|
|
|
|
|
extra_paths = podcast_name + "/"
|
|
|
|
|
2021-10-21 04:28:10 +02:00
|
|
|
if podcast_name is None:
|
2021-10-21 02:12:07 +02:00
|
|
|
print("### SKIPPING: (EPISODE NOT FOUND) ###")
|
|
|
|
else:
|
2021-10-22 07:23:38 +02:00
|
|
|
filename = podcast_name + " - " + episode_name
|
2021-10-21 02:12:07 +02:00
|
|
|
|
|
|
|
episode_id = EpisodeId.from_base62(episode_id_str)
|
|
|
|
stream = SESSION.content_feeder().load(
|
|
|
|
episode_id, VorbisOnlyAudioQuality(QUALITY), False, None)
|
2021-10-22 10:34:17 +02:00
|
|
|
# print("### DOWNLOADING '" + podcast_name + " - " +
|
|
|
|
# episode_name + "' - THIS MAY TAKE A WHILE ###")
|
2021-10-21 02:12:07 +02:00
|
|
|
|
2021-10-23 10:04:02 +02:00
|
|
|
os.makedirs(ZS_CONFIG["ROOT_PODCAST_PATH"] +
|
|
|
|
extra_paths, exist_ok=True)
|
2021-10-21 02:12:07 +02:00
|
|
|
|
2021-10-22 07:23:38 +02:00
|
|
|
total_size = stream.input_stream.size
|
2021-10-23 10:16:53 +02:00
|
|
|
with open(ZS_CONFIG["ROOT_PODCAST_PATH"] + extra_paths + filename + ".wav", "wb") as file, tqdm(
|
2021-10-22 07:23:38 +02:00
|
|
|
desc=filename,
|
|
|
|
total=total_size,
|
2021-10-23 10:16:53 +02:00
|
|
|
unit="B",
|
2021-10-22 07:23:38 +02:00
|
|
|
unit_scale=True,
|
|
|
|
unit_divisor=1024
|
2021-10-23 10:16:53 +02:00
|
|
|
) as p_bar:
|
2021-10-23 08:28:23 +02:00
|
|
|
for _ in range(int(total_size / ZS_CONFIG["CHUNK_SIZE"]) + 1):
|
2021-10-23 10:16:53 +02:00
|
|
|
p_bar.update(file.write(
|
2021-10-23 08:28:23 +02:00
|
|
|
stream.input_stream.stream().read(ZS_CONFIG["CHUNK_SIZE"])))
|
2021-10-20 22:44:52 +02:00
|
|
|
|
2021-10-23 08:28:23 +02:00
|
|
|
# convert_audio_format(ZS_CONFIG["ROOT_PODCAST_PATH"] +
|
2021-10-22 10:34:17 +02:00
|
|
|
# extra_paths + filename + ".wav")
|
|
|
|
|
|
|
|
# related functions that do stuff with the spotify API
|
2021-10-20 22:44:52 +02:00
|
|
|
|
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
def search(search_term): # pylint: disable=too-many-locals,too-many-branches
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Searches Spotify's API for relevant data """
|
|
|
|
token = SESSION.tokens().get("user-read-email")
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
resp = requests.get(
|
|
|
|
"https://api.spotify.com/v1/search",
|
|
|
|
{
|
|
|
|
"limit": "10",
|
|
|
|
"offset": "0",
|
2021-10-19 08:18:58 +02:00
|
|
|
"q": search_term,
|
2021-10-23 10:30:15 +02:00
|
|
|
"type": "track,album,artist,playlist"
|
2021-10-13 09:22:22 +02:00
|
|
|
},
|
2021-10-23 10:16:53 +02:00
|
|
|
headers={"Authorization": f"Bearer {token}"},
|
2021-10-13 09:22:22 +02:00
|
|
|
)
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-23 10:32:15 +02:00
|
|
|
# print(resp.json())
|
2021-10-21 04:13:16 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
i = 1
|
|
|
|
tracks = resp.json()["tracks"]["items"]
|
|
|
|
if len(tracks) > 0:
|
|
|
|
print("### TRACKS ###")
|
|
|
|
for track in tracks:
|
2021-10-21 04:13:16 +02:00
|
|
|
if track["explicit"]:
|
|
|
|
explicit = "[E]"
|
|
|
|
else:
|
|
|
|
explicit = ""
|
2021-10-23 10:16:53 +02:00
|
|
|
print(f"{i}, {track['name']} {explicit} | {','.join([artist['name'] for artist in track['artists']])}")
|
2021-10-13 09:22:22 +02:00
|
|
|
i += 1
|
2021-10-19 08:18:58 +02:00
|
|
|
total_tracks = i - 1
|
2021-10-13 09:22:22 +02:00
|
|
|
print("\n")
|
|
|
|
else:
|
2021-10-19 08:18:58 +02:00
|
|
|
total_tracks = 0
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
albums = resp.json()["albums"]["items"]
|
|
|
|
if len(albums) > 0:
|
|
|
|
print("### ALBUMS ###")
|
|
|
|
for album in albums:
|
2021-10-23 10:16:53 +02:00
|
|
|
print(f"{i}, {album['name']} | {','.join([artist['name'] for artist in album['artists']])}")
|
2021-10-13 09:22:22 +02:00
|
|
|
i += 1
|
2021-10-19 08:18:58 +02:00
|
|
|
total_albums = i - total_tracks - 1
|
2021-10-13 09:22:22 +02:00
|
|
|
print("\n")
|
|
|
|
else:
|
2021-10-19 08:18:58 +02:00
|
|
|
total_albums = 0
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-23 10:30:15 +02:00
|
|
|
artists = resp.json()["artists"]["items"]
|
|
|
|
if len(artists) > 0:
|
|
|
|
print("### ARTISTS ###")
|
|
|
|
for artist in artists:
|
|
|
|
print("%d, %s" % (
|
|
|
|
i,
|
|
|
|
artist["name"],
|
|
|
|
))
|
|
|
|
i += 1
|
|
|
|
total_artists = i - total_tracks - total_albums - 1
|
|
|
|
print("\n")
|
|
|
|
else:
|
|
|
|
total_artists = 0
|
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
playlists = resp.json()["playlists"]["items"]
|
2021-10-19 08:18:58 +02:00
|
|
|
print("### PLAYLISTS ###")
|
|
|
|
for playlist in playlists:
|
2021-10-23 10:16:53 +02:00
|
|
|
print(f"{i}, {playlist['name']} | {playlist['owner']['display_name']}")
|
2021-10-19 08:18:58 +02:00
|
|
|
i += 1
|
|
|
|
print("\n")
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
if len(tracks) + len(albums) + len(playlists) == 0:
|
|
|
|
print("NO RESULTS FOUND - EXITING...")
|
|
|
|
else:
|
2021-10-22 07:17:05 +02:00
|
|
|
selection = str(input("SELECT ITEM(S) BY ID: "))
|
|
|
|
inputs = split_input(selection)
|
|
|
|
for pos in inputs:
|
|
|
|
position = int(pos)
|
|
|
|
if position <= total_tracks:
|
|
|
|
track_id = tracks[position - 1]["id"]
|
|
|
|
download_track(track_id)
|
|
|
|
elif position <= total_albums + total_tracks:
|
|
|
|
download_album(albums[position - total_tracks - 1]["id"])
|
2021-10-23 10:30:15 +02:00
|
|
|
elif position <= total_artists + total_tracks + total_albums:
|
|
|
|
download_artist_albums(artists[position - total_tracks - total_albums -1]["id"])
|
2021-10-22 07:17:05 +02:00
|
|
|
else:
|
|
|
|
playlist_choice = playlists[position -
|
2021-10-23 10:30:15 +02:00
|
|
|
total_tracks - total_albums - total_artists - 1]
|
2021-10-22 07:17:05 +02:00
|
|
|
playlist_songs = get_playlist_songs(
|
2021-10-23 10:16:53 +02:00
|
|
|
token, playlist_choice["id"])
|
2021-10-22 07:17:05 +02:00
|
|
|
for song in playlist_songs:
|
2021-10-23 10:16:53 +02:00
|
|
|
if song["track"]["id"] is not None:
|
|
|
|
download_track(song["track"]["id"], sanitize_data(
|
|
|
|
playlist_choice["name"].strip()) + "/")
|
2021-10-22 07:17:05 +02:00
|
|
|
print("\n")
|
2021-10-18 13:04:27 +02:00
|
|
|
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
def get_song_info(song_id):
|
|
|
|
""" Retrieves metadata for downloaded songs """
|
|
|
|
token = SESSION.tokens().get("user-read-email")
|
|
|
|
|
|
|
|
info = json.loads(requests.get("https://api.spotify.com/v1/tracks?ids=" + song_id +
|
2021-10-23 10:16:53 +02:00
|
|
|
"&market=from_token", headers={"Authorization": f"Bearer {token}"}).text)
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
artists = []
|
2021-10-23 10:16:53 +02:00
|
|
|
for data in info["tracks"][0]["artists"]:
|
|
|
|
artists.append(sanitize_data(data["name"]))
|
|
|
|
album_name = sanitize_data(info["tracks"][0]["album"]["name"])
|
|
|
|
name = sanitize_data(info["tracks"][0]["name"])
|
|
|
|
image_url = info["tracks"][0]["album"]["images"][0]["url"]
|
|
|
|
release_year = info["tracks"][0]["album"]["release_date"].split("-")[0]
|
|
|
|
disc_number = info["tracks"][0]["disc_number"]
|
|
|
|
track_number = info["tracks"][0]["track_number"]
|
|
|
|
scraped_song_id = info["tracks"][0]["id"]
|
|
|
|
is_playable = info["tracks"][0]["is_playable"]
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
return artists, album_name, name, image_url, release_year, disc_number, track_number, scraped_song_id, is_playable
|
2021-10-18 13:04:27 +02:00
|
|
|
|
|
|
|
|
2021-10-20 21:37:36 +02:00
|
|
|
def check_premium():
|
2021-10-19 08:18:58 +02:00
|
|
|
""" If user has spotify premium return true """
|
2021-10-23 08:28:23 +02:00
|
|
|
return bool((SESSION.get_user_attribute("type") == "premium") or ZS_CONFIG["FORCE_PREMIUM"])
|
2021-10-18 23:54:30 +02:00
|
|
|
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
# Functions directly related to modifying the downloaded audio and its metadata
|
|
|
|
def convert_audio_format(filename):
|
|
|
|
""" Converts raw audio into playable mp3 or ogg vorbis """
|
2021-10-23 08:28:23 +02:00
|
|
|
# print("### CONVERTING TO " + ZS_CONFIG["MUSIC_FORMAT"].upper() + " ###")
|
2021-10-13 09:22:22 +02:00
|
|
|
raw_audio = AudioSegment.from_file(filename, format="ogg",
|
2021-10-19 08:18:58 +02:00
|
|
|
frame_rate=44100, channels=2, sample_width=2)
|
|
|
|
if QUALITY == AudioQuality.VERY_HIGH:
|
2021-10-18 13:04:27 +02:00
|
|
|
bitrate = "320k"
|
|
|
|
else:
|
|
|
|
bitrate = "160k"
|
2021-10-23 10:04:02 +02:00
|
|
|
raw_audio.export(
|
|
|
|
filename, format=ZS_CONFIG["MUSIC_FORMAT"], bitrate=bitrate)
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
def set_audio_tags(filename, artists, name, album_name, release_year, disc_number, track_number): # pylint: disable=too-many-arguments
|
2021-10-19 08:18:58 +02:00
|
|
|
""" sets music_tag metadata """
|
2021-10-22 07:23:38 +02:00
|
|
|
# print("### SETTING MUSIC TAGS ###")
|
2021-10-19 08:18:58 +02:00
|
|
|
tags = music_tag.load_file(filename)
|
2021-10-23 10:16:53 +02:00
|
|
|
tags["artist"] = conv_artist_format(artists)
|
|
|
|
tags["tracktitle"] = name
|
|
|
|
tags["album"] = album_name
|
|
|
|
tags["year"] = release_year
|
|
|
|
tags["discnumber"] = disc_number
|
|
|
|
tags["tracknumber"] = track_number
|
2021-10-19 08:18:58 +02:00
|
|
|
tags.save()
|
|
|
|
|
|
|
|
|
|
|
|
def set_music_thumbnail(filename, image_url):
|
|
|
|
""" Downloads cover artwork """
|
2021-10-22 07:23:38 +02:00
|
|
|
# print("### SETTING THUMBNAIL ###")
|
2021-10-19 08:18:58 +02:00
|
|
|
img = requests.get(image_url).content
|
|
|
|
tags = music_tag.load_file(filename)
|
2021-10-23 10:16:53 +02:00
|
|
|
tags["artwork"] = img
|
2021-10-19 08:18:58 +02:00
|
|
|
tags.save()
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
|
|
|
|
def conv_artist_format(artists):
|
|
|
|
""" Returns converted artist format """
|
2021-10-13 09:22:22 +02:00
|
|
|
formatted = ""
|
2021-10-21 04:28:10 +02:00
|
|
|
for artist in artists:
|
|
|
|
formatted += artist + ", "
|
2021-10-13 09:22:22 +02:00
|
|
|
return formatted[:-2]
|
|
|
|
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
# Extra functions directly related to spotify playlists
|
2021-10-13 09:22:22 +02:00
|
|
|
def get_all_playlists(access_token):
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Returns list of users playlists """
|
2021-10-13 09:22:22 +02:00
|
|
|
playlists = []
|
|
|
|
limit = 50
|
|
|
|
offset = 0
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
while True:
|
2021-10-23 10:16:53 +02:00
|
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
|
|
|
params = {"limit": limit, "offset": offset}
|
2021-10-19 08:18:58 +02:00
|
|
|
resp = requests.get("https://api.spotify.com/v1/me/playlists",
|
|
|
|
headers=headers, params=params).json()
|
2021-10-13 09:22:22 +02:00
|
|
|
offset += limit
|
2021-10-23 10:16:53 +02:00
|
|
|
playlists.extend(resp["items"])
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
if len(resp["items"]) < limit:
|
2021-10-13 09:22:22 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
return playlists
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
def get_playlist_songs(access_token, playlist_id):
|
2021-10-19 08:18:58 +02:00
|
|
|
""" returns list of songs in a playlist """
|
2021-10-13 09:22:22 +02:00
|
|
|
songs = []
|
|
|
|
offset = 0
|
|
|
|
limit = 100
|
|
|
|
|
|
|
|
while True:
|
2021-10-23 10:16:53 +02:00
|
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
|
|
|
params = {"limit": limit, "offset": offset}
|
2021-10-19 08:18:58 +02:00
|
|
|
resp = requests.get(
|
2021-10-23 10:16:53 +02:00
|
|
|
f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks", headers=headers, params=params).json()
|
2021-10-13 09:22:22 +02:00
|
|
|
offset += limit
|
2021-10-23 10:16:53 +02:00
|
|
|
songs.extend(resp["items"])
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
if len(resp["items"]) < limit:
|
2021-10-13 09:22:22 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
return songs
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
def get_playlist_info(access_token, playlist_id):
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Returns information scraped from playlist """
|
2021-10-23 10:16:53 +02:00
|
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
2021-10-19 08:18:58 +02:00
|
|
|
resp = requests.get(
|
2021-10-23 10:16:53 +02:00
|
|
|
f"https://api.spotify.com/v1/playlists/{playlist_id}?fields=name,owner(display_name)&market=from_token",
|
|
|
|
headers=headers).json()
|
|
|
|
return resp["name"].strip(), resp["owner"]["display_name"].strip()
|
2021-10-13 09:22:22 +02:00
|
|
|
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
# Extra functions directly related to spotify albums
|
2021-10-13 09:22:22 +02:00
|
|
|
def get_album_tracks(access_token, album_id):
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Returns album tracklist """
|
2021-10-13 09:22:22 +02:00
|
|
|
songs = []
|
|
|
|
offset = 0
|
|
|
|
limit = 50
|
|
|
|
|
|
|
|
while True:
|
2021-10-23 10:16:53 +02:00
|
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
|
|
|
params = {"limit": limit, "offset": offset}
|
2021-10-19 08:18:58 +02:00
|
|
|
resp = requests.get(
|
2021-10-23 10:16:53 +02:00
|
|
|
f"https://api.spotify.com/v1/albums/{album_id}/tracks", headers=headers, params=params).json()
|
2021-10-13 09:22:22 +02:00
|
|
|
offset += limit
|
2021-10-23 10:16:53 +02:00
|
|
|
songs.extend(resp["items"])
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
if len(resp["items"]) < limit:
|
2021-10-13 09:22:22 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
return songs
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
def get_album_name(access_token, album_id):
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Returns album name """
|
2021-10-23 10:16:53 +02:00
|
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
2021-10-19 08:18:58 +02:00
|
|
|
resp = requests.get(
|
2021-10-23 10:16:53 +02:00
|
|
|
f"https://api.spotify.com/v1/albums/{album_id}", headers=headers).json()
|
|
|
|
return resp["artists"][0]["name"], sanitize_data(resp["name"])
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-21 22:15:09 +02:00
|
|
|
# Extra functions directly related to spotify artists
|
2021-10-23 02:00:46 +02:00
|
|
|
|
|
|
|
|
2021-10-21 22:15:09 +02:00
|
|
|
def get_artist_albums(access_token, artist_id):
|
|
|
|
""" Returns artist's albums """
|
2021-10-23 10:16:53 +02:00
|
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
2021-10-21 22:15:09 +02:00
|
|
|
resp = requests.get(
|
2021-10-23 10:16:53 +02:00
|
|
|
f"https://api.spotify.com/v1/artists/{artist_id}/albums", headers=headers).json()
|
2021-10-21 22:38:58 +02:00
|
|
|
# Return a list each album's id
|
2021-10-23 10:16:53 +02:00
|
|
|
return [resp["items"][i]["id"] for i in range(len(resp["items"]))]
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
# Extra functions directly related to our saved tracks
|
2021-10-23 02:00:46 +02:00
|
|
|
|
|
|
|
|
2021-10-19 01:22:56 +02:00
|
|
|
def get_saved_tracks(access_token):
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Returns user's saved tracks """
|
2021-10-19 01:22:56 +02:00
|
|
|
songs = []
|
|
|
|
offset = 0
|
|
|
|
limit = 50
|
|
|
|
|
|
|
|
while True:
|
2021-10-23 10:16:53 +02:00
|
|
|
headers = {"Authorization": f"Bearer {access_token}"}
|
|
|
|
params = {"limit": limit, "offset": offset}
|
|
|
|
resp = requests.get("https://api.spotify.com/v1/me/tracks",
|
2021-10-19 08:18:58 +02:00
|
|
|
headers=headers, params=params).json()
|
2021-10-19 01:22:56 +02:00
|
|
|
offset += limit
|
2021-10-23 10:16:53 +02:00
|
|
|
songs.extend(resp["items"])
|
2021-10-19 01:22:56 +02:00
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
if len(resp["items"]) < limit:
|
2021-10-19 01:22:56 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
return songs
|
2021-10-13 09:22:22 +02:00
|
|
|
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
# Functions directly related to downloading stuff
|
2021-10-23 10:16:53 +02:00
|
|
|
def download_track(track_id_str: str, extra_paths="", prefix=False, prefix_value="", disable_progressbar=False): # pylint: disable=too-many-locals,too-many-branches
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Downloads raw song audio from Spotify """
|
2021-10-21 03:41:22 +02:00
|
|
|
try:
|
|
|
|
artists, album_name, name, image_url, release_year, disc_number, track_number, scraped_song_id, is_playable = get_song_info(
|
|
|
|
track_id_str)
|
|
|
|
|
|
|
|
song_name = artists[0] + " - " + name
|
2021-10-22 16:48:09 +02:00
|
|
|
if prefix:
|
2021-10-23 10:16:53 +02:00
|
|
|
song_name = f"{prefix_value.zfill(2)} - {song_name}" if prefix_value.isdigit(
|
|
|
|
) else f"{prefix_value} - {song_name}"
|
2021-10-23 10:04:02 +02:00
|
|
|
|
|
|
|
if ZS_CONFIG["SPLIT_ALBUM_DISCS"]:
|
|
|
|
filename = os.path.join(ZS_CONFIG["ROOT_PATH"], extra_paths, "Disc " + str(
|
2021-10-23 10:16:53 +02:00
|
|
|
disc_number) + "/" + song_name + "." + ZS_CONFIG["MUSIC_FORMAT"])
|
2021-10-23 10:04:02 +02:00
|
|
|
else:
|
|
|
|
filename = os.path.join(ZS_CONFIG["ROOT_PATH"], extra_paths,
|
2021-10-23 10:16:53 +02:00
|
|
|
song_name + "." + ZS_CONFIG["MUSIC_FORMAT"])
|
|
|
|
except Exception as err: # pylint: disable=broad-except,unused-variable
|
2021-10-21 03:41:22 +02:00
|
|
|
print("### SKIPPING SONG - FAILED TO QUERY METADATA ###")
|
2021-10-23 10:16:53 +02:00
|
|
|
# print(err)
|
2021-10-13 09:22:22 +02:00
|
|
|
else:
|
2021-10-21 03:41:22 +02:00
|
|
|
try:
|
|
|
|
if not is_playable:
|
2021-10-19 08:18:58 +02:00
|
|
|
print("### SKIPPING:", song_name,
|
2021-10-21 03:41:22 +02:00
|
|
|
"(SONG IS UNAVAILABLE) ###")
|
2021-10-16 10:41:30 +02:00
|
|
|
else:
|
2021-10-23 08:28:23 +02:00
|
|
|
if os.path.isfile(filename) and os.path.getsize(filename) and ZS_CONFIG["SKIP_EXISTING_FILES"]:
|
2021-10-21 03:41:22 +02:00
|
|
|
print("### SKIPPING:", song_name,
|
|
|
|
"(SONG ALREADY EXISTS) ###")
|
|
|
|
else:
|
|
|
|
if track_id_str != scraped_song_id:
|
|
|
|
track_id_str = scraped_song_id
|
|
|
|
|
|
|
|
track_id = TrackId.from_base62(track_id_str)
|
2021-10-22 07:23:38 +02:00
|
|
|
# print("### FOUND SONG:", song_name, " ###")
|
2021-10-21 03:41:22 +02:00
|
|
|
|
|
|
|
stream = SESSION.content_feeder().load(
|
|
|
|
track_id, VorbisOnlyAudioQuality(QUALITY), False, None)
|
2021-10-22 07:23:38 +02:00
|
|
|
# print("### DOWNLOADING RAW AUDIO ###")
|
2021-10-21 03:41:22 +02:00
|
|
|
|
2021-10-23 10:04:02 +02:00
|
|
|
if ZS_CONFIG["SPLIT_ALBUM_DISCS"]:
|
|
|
|
os.makedirs(
|
2021-10-23 10:16:53 +02:00
|
|
|
ZS_CONFIG["ROOT_PATH"] + extra_paths + "/Disc " + str(disc_number) + "/", exist_ok=True)
|
2021-10-23 10:04:02 +02:00
|
|
|
else:
|
|
|
|
os.makedirs(ZS_CONFIG["ROOT_PATH"] +
|
|
|
|
extra_paths, exist_ok=True)
|
2021-10-16 10:41:30 +02:00
|
|
|
|
2021-10-22 04:34:20 +02:00
|
|
|
total_size = stream.input_stream.size
|
2021-10-23 10:16:53 +02:00
|
|
|
with open(filename, "wb") as file, tqdm(
|
2021-10-22 04:34:20 +02:00
|
|
|
desc=song_name,
|
|
|
|
total=total_size,
|
2021-10-23 10:16:53 +02:00
|
|
|
unit="B",
|
2021-10-22 04:34:20 +02:00
|
|
|
unit_scale=True,
|
2021-10-22 17:14:13 +02:00
|
|
|
unit_divisor=1024,
|
|
|
|
disable=disable_progressbar
|
2021-10-23 10:16:53 +02:00
|
|
|
) as p_bar:
|
2021-10-23 08:28:23 +02:00
|
|
|
for _ in range(int(total_size / ZS_CONFIG["CHUNK_SIZE"]) + 1):
|
2021-10-23 10:16:53 +02:00
|
|
|
p_bar.update(file.write(
|
2021-10-23 08:28:23 +02:00
|
|
|
stream.input_stream.stream().read(ZS_CONFIG["CHUNK_SIZE"])))
|
2021-10-16 10:41:30 +02:00
|
|
|
|
2021-10-23 08:28:23 +02:00
|
|
|
if not ZS_CONFIG["RAW_AUDIO_AS_IS"]:
|
2021-10-19 21:35:59 +02:00
|
|
|
convert_audio_format(filename)
|
|
|
|
set_audio_tags(filename, artists, name, album_name,
|
2021-10-20 21:37:36 +02:00
|
|
|
release_year, disc_number, track_number)
|
2021-10-19 21:35:59 +02:00
|
|
|
set_music_thumbnail(filename, image_url)
|
2021-10-21 09:18:07 +02:00
|
|
|
|
2021-10-23 08:28:23 +02:00
|
|
|
if not ZS_CONFIG["OVERRIDE_AUTO_WAIT"]:
|
|
|
|
time.sleep(ZS_CONFIG["ANTI_BAN_WAIT_TIME"])
|
2021-10-23 10:16:53 +02:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2021-10-21 03:41:22 +02:00
|
|
|
print("### SKIPPING:", song_name,
|
|
|
|
"(GENERAL DOWNLOAD ERROR) ###")
|
|
|
|
if os.path.exists(filename):
|
|
|
|
os.remove(filename)
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
def download_album(album):
|
|
|
|
""" Downloads songs from an album """
|
|
|
|
token = SESSION.tokens().get("user-read-email")
|
2021-10-13 09:22:22 +02:00
|
|
|
artist, album_name = get_album_name(token, album)
|
|
|
|
tracks = get_album_tracks(token, album)
|
2021-10-23 10:16:53 +02:00
|
|
|
for num, track in tqdm(enumerate(tracks, start=1), unit_scale=True, unit="Song", total=len(tracks)):
|
|
|
|
download_track(track["id"], f"{artist}/{album_name}",
|
|
|
|
prefix=True, prefix_value=str(num), disable_progressbar=True)
|
2021-10-23 02:00:46 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-21 22:27:02 +02:00
|
|
|
def download_artist_albums(artist):
|
|
|
|
""" Downloads albums of an artist """
|
|
|
|
token = SESSION.tokens().get("user-read-email")
|
|
|
|
albums = get_artist_albums(token, artist)
|
2021-10-21 22:40:40 +02:00
|
|
|
for album_id in albums:
|
2021-10-21 22:27:02 +02:00
|
|
|
download_album(album_id)
|
2021-10-22 00:25:27 +02:00
|
|
|
|
2021-10-23 02:00:46 +02:00
|
|
|
|
2021-10-21 23:33:55 +02:00
|
|
|
def download_playlist(playlists, playlist_choice):
|
|
|
|
"""Downloads all the songs from a playlist"""
|
|
|
|
token = SESSION.tokens().get("user-read-email")
|
|
|
|
|
|
|
|
playlist_songs = get_playlist_songs(
|
2021-10-23 10:16:53 +02:00
|
|
|
token, playlists[int(playlist_choice) - 1]["id"])
|
2021-10-21 23:33:55 +02:00
|
|
|
|
|
|
|
for song in playlist_songs:
|
2021-10-23 10:16:53 +02:00
|
|
|
if song["track"]["id"] is not None:
|
|
|
|
download_track(song["track"]["id"], sanitize_data(
|
|
|
|
playlists[int(playlist_choice) - 1]["name"].strip()) + "/")
|
2021-10-21 23:33:55 +02:00
|
|
|
print("\n")
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-22 00:25:27 +02:00
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
def download_from_user_playlist():
|
2021-10-21 23:33:55 +02:00
|
|
|
""" Select which playlist(s) to download """
|
2021-10-19 08:18:58 +02:00
|
|
|
token = SESSION.tokens().get("user-read-email")
|
2021-10-13 09:22:22 +02:00
|
|
|
playlists = get_all_playlists(token)
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
count = 1
|
|
|
|
for playlist in playlists:
|
2021-10-23 10:16:53 +02:00
|
|
|
print(str(count) + ": " + playlist["name"].strip())
|
2021-10-13 09:22:22 +02:00
|
|
|
count += 1
|
2021-10-18 13:04:27 +02:00
|
|
|
|
2021-10-21 23:33:55 +02:00
|
|
|
print("\n> SELECT A PLAYLIST BY ID")
|
|
|
|
print("> SELECT A RANGE BY ADDING A DASH BETWEEN BOTH ID's")
|
|
|
|
print("> For example, typing 10 to get one playlist or 10-20 to get\nevery playlist from 10-20 (inclusive)\n")
|
|
|
|
|
|
|
|
playlist_choices = input("ID(s): ").split("-")
|
|
|
|
|
|
|
|
if len(playlist_choices) == 1:
|
|
|
|
download_playlist(playlists, playlist_choices[0])
|
|
|
|
else:
|
|
|
|
start = int(playlist_choices[0])
|
2021-10-22 16:29:48 +02:00
|
|
|
end = int(playlist_choices[1]) + 1
|
2021-10-21 23:33:55 +02:00
|
|
|
|
|
|
|
print(f"Downloading from {start} to {end}...")
|
|
|
|
|
|
|
|
for playlist in range(start, end):
|
|
|
|
download_playlist(playlists, playlist)
|
|
|
|
|
2021-10-22 00:25:27 +02:00
|
|
|
print("\n**All playlists have been downloaded**\n")
|
2021-10-21 23:33:55 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
# Core functions here
|
|
|
|
|
2021-10-20 21:37:36 +02:00
|
|
|
|
2021-10-23 10:16:53 +02:00
|
|
|
def check_raw(): # pylint: disable=missing-function-docstring
|
2021-10-23 08:28:23 +02:00
|
|
|
if ZS_CONFIG["RAW_AUDIO_AS_IS"]:
|
|
|
|
ZS_CONFIG["MUSIC_FORMAT"] = "wav"
|
2021-10-13 09:22:22 +02:00
|
|
|
|
2021-10-20 21:37:36 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
def main():
|
2021-10-19 08:18:58 +02:00
|
|
|
""" Main function """
|
2021-10-21 04:28:10 +02:00
|
|
|
check_raw()
|
2021-10-18 13:04:27 +02:00
|
|
|
login()
|
2021-10-13 09:22:22 +02:00
|
|
|
client()
|
|
|
|
|
2021-10-19 08:18:58 +02:00
|
|
|
|
2021-10-13 09:22:22 +02:00
|
|
|
if __name__ == "__main__":
|
2021-10-19 21:16:15 +02:00
|
|
|
main()
|