mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Link arguments to spotipy helpers
This commit is contained in:
@@ -4,26 +4,47 @@
|
||||
# to `spotify._get_id` in below methods.
|
||||
|
||||
from spotdl.authorize.services import AuthorizeSpotify
|
||||
import spotdl.util
|
||||
|
||||
import sys
|
||||
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
try:
|
||||
from slugify import SLUG_OK, slugify
|
||||
except ImportError:
|
||||
logger.error("Oops! `unicode-slugify` was not found.")
|
||||
logger.info("Please remove any other slugify library and install `unicode-slugify`.")
|
||||
sys.exit(5)
|
||||
|
||||
|
||||
ALBUM_BASE_URL = "https://open.spotify.com/album/"
|
||||
|
||||
class SpotifyHelpers:
|
||||
def __init__(self, spotify=None):
|
||||
self._ALBUM_BASE_URL = ALBUM_BASE_URL
|
||||
if spotify is None:
|
||||
spotify = AuthorizeSpotify()
|
||||
self.spotify = spotify
|
||||
|
||||
def prompt_for_user_playlist(self, username):
|
||||
""" Write user playlists to text_file """
|
||||
links = fetch_user_playlist_urls(username)
|
||||
playlist = internals.input_link(links)
|
||||
return playlist
|
||||
playlists = self.fetch_user_playlist_urls(username)
|
||||
for i, playlist in enumerate(playlists, 1):
|
||||
playlist_details = "{0}. {1:<30} ({2} tracks)".format(
|
||||
i, playlist["name"], playlist["tracks"]["total"]
|
||||
)
|
||||
print(playlist_details, file=sys.stderr)
|
||||
print("", file=sys.stderr)
|
||||
playlist = spotdl.util.prompt_user_for_selection(playlists)
|
||||
return playlist["external_urls"]["spotify"]
|
||||
|
||||
def fetch_user_playlist_urls(self, username):
|
||||
""" Fetch user playlists when using the -u option. """
|
||||
logger.debug('Fetching playlists for "{username}".'.format(username=username))
|
||||
playlists = self.spotify.user_playlists(username)
|
||||
links = []
|
||||
collected_playlists = []
|
||||
check = 1
|
||||
|
||||
while True:
|
||||
@@ -31,56 +52,51 @@ class SpotifyHelpers:
|
||||
# in rare cases, playlists may not be found, so playlists['next']
|
||||
# is None. Skip these. Also see Issue #91.
|
||||
if playlist["name"] is not None:
|
||||
logger.info(
|
||||
# u"{0:>5}. {1:<30} ({2} tracks)".format(
|
||||
# check, playlist["name"], playlist["tracks"]["total"]
|
||||
# )
|
||||
# )
|
||||
playlist_url = playlist["external_urls"]["spotify"]
|
||||
# log.debug(playlist_url)
|
||||
links.append(playlist_url)
|
||||
collected_playlists.append(playlist)
|
||||
check += 1
|
||||
if playlists["next"]:
|
||||
playlists = self.spotify.next(playlists)
|
||||
else:
|
||||
break
|
||||
|
||||
return links
|
||||
return collected_playlists
|
||||
|
||||
def fetch_playlist(self, playlist_url):
|
||||
logger.debug('Fetching playlist "{playlist}".'.format(playlist=playlist_url))
|
||||
try:
|
||||
playlist_id = self.spotify._get_id("playlist", playlist_url)
|
||||
except IndexError:
|
||||
# Wrong format, in either case
|
||||
# log.error("The provided playlist URL is not in a recognized format!")
|
||||
logger.error("The provided playlist URL is not in a recognized format!")
|
||||
sys.exit(10)
|
||||
try:
|
||||
results = self.spotify.user_playlist(
|
||||
user=None, playlist_id=playlist_id, fields="tracks,next,name"
|
||||
)
|
||||
except spotipy.client.SpotifyException:
|
||||
# log.error("Unable to find playlist")
|
||||
logger.info("Make sure the playlist is set to publicly visible and then try again")
|
||||
logger.error("Unable to find playlist")
|
||||
logger.info("Make sure the playlist is set to publicly visible and then try again.")
|
||||
sys.exit(11)
|
||||
|
||||
return results
|
||||
|
||||
def write_playlist(self, playlist, text_file=None):
|
||||
def write_playlist_tracks(self, playlist, text_file=None):
|
||||
tracks = playlist["tracks"]
|
||||
if not text_file:
|
||||
text_file = u"{0}.txt".format(slugify(playlist["name"], ok="-_()[]{}"))
|
||||
return write_tracks(tracks, text_file)
|
||||
return self.write_tracks(tracks, text_file)
|
||||
|
||||
def fetch_album(self, album_url):
|
||||
logger.debug('Fetching album "{album}".'.format(album=album_url))
|
||||
album_id = self.spotify._get_id("album", album_url)
|
||||
album = self.spotify.album(album_id)
|
||||
return album
|
||||
|
||||
def write_album(self, album, text_file=None):
|
||||
def write_album_tracks(self, album, text_file=None):
|
||||
tracks = self.spotify.album_tracks(album["id"])
|
||||
if not text_file:
|
||||
text_file = u"{0}.txt".format(slugify(album["name"], ok="-_()[]{}"))
|
||||
return write_tracks(tracks, text_file)
|
||||
return self.write_tracks(tracks, text_file)
|
||||
|
||||
def fetch_albums_from_artist(self, artist_url, album_type=None):
|
||||
"""
|
||||
@@ -92,9 +108,10 @@ class SpotifyHelpers:
|
||||
:param return - the album from the artist
|
||||
"""
|
||||
|
||||
logger.debug('Fetching all albums for "{artist}".'.format(artist=artist_url))
|
||||
artist_id = self.spotify._get_id("artist", artist_url)
|
||||
# fetching artist's albums limitting the results to the US to avoid duplicate
|
||||
# albums from multiple markets
|
||||
artist_id = self.spotify._get_id("artist", artist_url)
|
||||
results = self.spotify.artist_albums(artist_id, album_type=album_type, country="US")
|
||||
|
||||
albums = results["items"]
|
||||
@@ -106,7 +123,7 @@ class SpotifyHelpers:
|
||||
|
||||
return albums
|
||||
|
||||
def write_all_albums_from_artist(self, albums, text_file=None):
|
||||
def write_all_albums(self, albums, text_file=None):
|
||||
"""
|
||||
This function gets all albums from an artist and writes it to a file in the
|
||||
current working directory called [ARTIST].txt, where [ARTIST] is the artist
|
||||
@@ -115,20 +132,17 @@ class SpotifyHelpers:
|
||||
:param text_file - file to write albums to
|
||||
"""
|
||||
|
||||
album_base_url = "https://open.spotify.com/album/"
|
||||
|
||||
# if no file if given, the default save file is in the current working
|
||||
# directory with the name of the artist
|
||||
if text_file is None:
|
||||
text_file = albums[0]["artists"][0]["name"] + ".txt"
|
||||
|
||||
for album in albums:
|
||||
logging album name
|
||||
log.info("Fetching album: " + album["name"])
|
||||
write_album(album_base_url + album["id"], text_file=text_file)
|
||||
logger.info('Fetching album "{album}".'.format(album=album["name"]))
|
||||
self.write_album_tracks(album, text_file=text_file)
|
||||
|
||||
def write_tracks(self, tracks, text_file):
|
||||
logger.info(u"Writing {0} tracks to {1}".format(tracks["total"], text_file))
|
||||
logger.info(u"Writing {0} tracks to {1}.".format(tracks["total"], text_file))
|
||||
track_urls = []
|
||||
with open(text_file, "a") as file_out:
|
||||
while True:
|
||||
@@ -139,12 +153,13 @@ class SpotifyHelpers:
|
||||
track = item
|
||||
try:
|
||||
track_url = track["external_urls"]["spotify"]
|
||||
# log.debug(track_url)
|
||||
file_out.write(track_url + "\n")
|
||||
track_urls.append(track_url)
|
||||
except KeyError:
|
||||
# log.warning(
|
||||
u"Skipping track {0} by {1} (local only?)".format(
|
||||
# FIXME: Write "{artist} - {name}" instead of Spotify URI for
|
||||
# "local only" tracks.
|
||||
logger.warning(
|
||||
'Skipping track "{0}" by "{1}" (local only?)'.format(
|
||||
track["name"], track["artists"][0]["name"]
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user