mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-12-08 20:39:08 +00:00
Filter unwanted text from Spotify URLs when extracting information (#394)
* Split unwanted URL part * Convert get_splits() -> extract_spotify_id() * Add tests for extract_spotify_id() * Extract Spotify artist ID when fetching artist albums
This commit is contained in:
@@ -122,17 +122,15 @@ def get_playlists(username):
|
||||
|
||||
|
||||
def fetch_playlist(playlist):
|
||||
splits = internals.get_splits(playlist)
|
||||
try:
|
||||
username = splits[-3]
|
||||
playlist_id = internals.extract_spotify_id(playlist)
|
||||
except IndexError:
|
||||
# Wrong format, in either case
|
||||
log.error("The provided playlist URL is not in a recognized format!")
|
||||
sys.exit(10)
|
||||
playlist_id = splits[-1]
|
||||
try:
|
||||
results = spotify.user_playlist(
|
||||
username, playlist_id, fields="tracks,next,name"
|
||||
user=None, playlist_id=playlist_id, fields="tracks,next,name"
|
||||
)
|
||||
except spotipy.client.SpotifyException:
|
||||
log.error("Unable to find playlist")
|
||||
@@ -151,12 +149,12 @@ def write_playlist(playlist_url, text_file=None):
|
||||
|
||||
|
||||
def fetch_album(album):
|
||||
splits = internals.get_splits(album)
|
||||
album_id = splits[-1]
|
||||
album_id = internals.extract_spotify_id(album)
|
||||
album = spotify.album(album_id)
|
||||
return album
|
||||
|
||||
def fetch_album_from_artist(artist_url, album_type='album'):
|
||||
|
||||
def fetch_album_from_artist(artist_url, album_type="album"):
|
||||
"""
|
||||
This funcction returns all the albums from a give artist_url using the US
|
||||
market
|
||||
@@ -166,16 +164,17 @@ def fetch_album_from_artist(artist_url, album_type='album'):
|
||||
:param return - the album from the artist
|
||||
"""
|
||||
|
||||
#fetching artist's albums limitting the results to the US to avoid duplicate
|
||||
#albums from multiple markets
|
||||
results = spotify.artist_albums(artist_url, album_type=album_type, country='US')
|
||||
# fetching artist's albums limitting the results to the US to avoid duplicate
|
||||
# albums from multiple markets
|
||||
artist_id = internals.extract_spotify_id(artist_url)
|
||||
results = spotify.artist_albums(artist_id, album_type=album_type, country="US")
|
||||
|
||||
albums = results['items']
|
||||
albums = results["items"]
|
||||
|
||||
#indexing all pages of results
|
||||
while results['next']:
|
||||
# indexing all pages of results
|
||||
while results["next"]:
|
||||
results = spotify.next(results)
|
||||
albums.extend(results['items'])
|
||||
albums.extend(results["items"])
|
||||
|
||||
return albums
|
||||
|
||||
@@ -189,27 +188,28 @@ def write_all_albums_from_artist(artist_url, text_file=None):
|
||||
:param text_file - file to write albums to
|
||||
"""
|
||||
|
||||
album_base_url = 'https://open.spotify.com/album/'
|
||||
album_base_url = "https://open.spotify.com/album/"
|
||||
|
||||
#fetching all default albums
|
||||
# fetching all default albums
|
||||
albums = fetch_album_from_artist(artist_url)
|
||||
|
||||
#if no file if given, the default save file is in the current working
|
||||
#directory with the name of the artist
|
||||
# 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'
|
||||
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)
|
||||
# logging album name
|
||||
log.info("Fetching album: " + album["name"])
|
||||
write_album(album_base_url + album["id"], text_file=text_file)
|
||||
|
||||
#fetching all single albums
|
||||
singles = fetch_album_from_artist(artist_url, album_type='single')
|
||||
# fetching all single albums
|
||||
singles = fetch_album_from_artist(artist_url, album_type="single")
|
||||
|
||||
for single in singles:
|
||||
log.info('Fetching single: ' + single['name'])
|
||||
write_album(album_base_url + single['id'], text_file=text_file)
|
||||
log.info("Fetching single: " + single["name"])
|
||||
write_album(album_base_url + single["id"], text_file=text_file)
|
||||
|
||||
|
||||
def write_album(album_url, text_file=None):
|
||||
album = fetch_album(album_url)
|
||||
|
||||
Reference in New Issue
Block a user