From 8949b9a692c8fabac266f95f2a2a5895daaf7769 Mon Sep 17 00:00:00 2001 From: kuba Date: Fri, 25 Aug 2023 23:09:53 +0200 Subject: [PATCH] check if track is a song when downloading playlists --- spotdl/types/playlist.py | 27 ++++++++++++++++----------- spotdl/utils/web.py | 8 ++++---- tests/test_matching.py | 4 ++-- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/spotdl/types/playlist.py b/spotdl/types/playlist.py index 8ab4c8af9..d617bdff1 100644 --- a/spotdl/types/playlist.py +++ b/spotdl/types/playlist.py @@ -2,6 +2,7 @@ Playlist module for retrieving playlist data from Spotify. """ +import logging from dataclasses import dataclass from typing import Any, Dict, List, Tuple @@ -10,6 +11,8 @@ __all__ = ["Playlist", "PlaylistError"] +logger = logging.getLogger(__name__) + class PlaylistError(Exception): """ @@ -82,20 +85,22 @@ def get_metadata(url: str) -> Tuple[Dict[str, Any], List[Song]]: songs = [] for track in tracks: - if ( - not isinstance(track, dict) - or track.get("track") is None - or track.get("track", {}).get("is_local") - ): + if not isinstance(track, dict) or track.get("track") is None: + continue + + track_meta = track["track"] + + if track_meta.get("is_local") or track_meta.get("type") != "track": + logger.warning( + "Skipping track: %s local tracks and %s are not supported", + track_meta.get("id"), + track_meta.get("type"), + ) + continue - track_meta = track.get("track", {}) track_id = track_meta.get("id") - if ( - track_meta == {} - or track_id is None - or track_meta.get("duration_ms") == 0 - ): + if track_id is None or track_meta.get("duration_ms") == 0: continue album_meta = track_meta.get("album", {}) diff --git a/spotdl/utils/web.py b/spotdl/utils/web.py index 68092086d..7aaeddfb7 100644 --- a/spotdl/utils/web.py +++ b/spotdl/utils/web.py @@ -31,15 +31,15 @@ from spotdl._version import __version__ from spotdl.download.downloader import Downloader from spotdl.download.progress_handler import ProgressHandler, SongTracker +from spotdl.types.album import Album +from spotdl.types.artist import Artist from spotdl.types.options import ( DownloaderOptionalOptions, DownloaderOptions, WebOptions, ) -from spotdl.types.song import Song from spotdl.types.playlist import Playlist -from spotdl.types.album import Album -from spotdl.types.artist import Artist +from spotdl.types.song import Song from spotdl.utils.arguments import create_parser from spotdl.utils.config import ( DOWNLOADER_OPTIONS, @@ -310,7 +310,7 @@ def songs_from_url(url: str) -> List[Song]: ### Returns - returns a list with Song objects to be downloaded. """ - + if "playlist" in url: playlist = Playlist.from_url(url) return list(map(Song.from_url, playlist.urls)) diff --git a/tests/test_matching.py b/tests/test_matching.py index 8334e9759..5950349cb 100644 --- a/tests/test_matching.py +++ b/tests/test_matching.py @@ -344,7 +344,7 @@ "https://www.youtube.com/watch?v=unrdoZwLYbU", "https://www.youtube.com/watch?v=B_HSa1dEL9s", "https://www.youtube.com/watch?v=tp83QrhNguM", - "https://www.youtube.com/watch?v=oru1gyxUImI" + "https://www.youtube.com/watch?v=oru1gyxUImI", ], ), ( @@ -352,7 +352,7 @@ "https://open.spotify.com/track/4Dy9azWTLsDMUdUxrT1170", [ "https://www.youtube.com/watch?v=KD5ncAFjRzY", - "https://www.youtube.com/watch?v=9DKKhnmgCIo" + "https://www.youtube.com/watch?v=9DKKhnmgCIo", ], ) # (