Skip to content

Commit 4fb702d

Browse files
authored
Filter out podcast episodes (#83)
* Filter out podcast episodes from playlists * Don't create empty playlists * Bump version 1.0.2 Fixes #30
1 parent 693dcd1 commit 4fb702d

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "spotify_to_tidal"
7-
version = "1.0.1"
7+
version = "1.0.2"
88
requires-python = ">= 3.10"
99

1010
dependencies = [

src/spotify_to_tidal/sync.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,13 @@ async def _fetch_all_from_spotify_in_chunks(fetch_function: Callable) -> List[di
178178

179179
async def get_tracks_from_spotify_playlist(spotify_session: spotipy.Spotify, spotify_playlist):
180180
def _get_tracks_from_spotify_playlist(offset: int, playlist_id: str):
181-
fields = "next,total,limit,items(track(name,album(name,artists),artists,track_number,duration_ms,id,external_ids(isrc)))"
181+
fields = "next,total,limit,items(track(name,album(name,artists),artists,track_number,duration_ms,id,external_ids(isrc))),type"
182182
return spotify_session.playlist_tracks(playlist_id=playlist_id, fields=fields, offset=offset)
183183

184184
print(f"Loading tracks from Spotify playlist '{spotify_playlist['name']}'")
185-
return await repeat_on_request_error( _fetch_all_from_spotify_in_chunks, lambda offset: _get_tracks_from_spotify_playlist(offset=offset, playlist_id=spotify_playlist["id"]))
186-
185+
items = await repeat_on_request_error( _fetch_all_from_spotify_in_chunks, lambda offset: _get_tracks_from_spotify_playlist(offset=offset, playlist_id=spotify_playlist["id"]))
186+
track_filter = lambda item: item.get('type', 'track') == 'track' # type may be 'episode' also
187+
return list(filter(track_filter, items))
187188

188189
def populate_track_match_cache(spotify_tracks_: Sequence[t_spotify.SpotifyTrack], tidal_tracks_: Sequence[tidalapi.Track]):
189190
""" Populate the track match cache with all the existing tracks in Tidal playlist corresponding to Spotify playlist """
@@ -282,14 +283,18 @@ async def _run_rate_limiter(semaphore):
282283

283284
async def sync_playlist(spotify_session: spotipy.Spotify, tidal_session: tidalapi.Session, spotify_playlist, tidal_playlist: tidalapi.Playlist | None, config: dict):
284285
""" sync given playlist to tidal """
285-
# Create a new Tidal playlist if required
286-
if not tidal_playlist:
286+
# Get the tracks from both Spotify and Tidal, creating a new Tidal playlist if necessary
287+
spotify_tracks = await get_tracks_from_spotify_playlist(spotify_session, spotify_playlist)
288+
if len(spotify_tracks) == 0:
289+
return # nothing to do
290+
if tidal_playlist:
291+
old_tidal_tracks = await get_all_playlist_tracks(tidal_playlist)
292+
else:
287293
print(f"No playlist found on Tidal corresponding to Spotify playlist: '{spotify_playlist['name']}', creating new playlist")
288294
tidal_playlist = tidal_session.user.create_playlist(spotify_playlist['name'], spotify_playlist['description'])
295+
old_tidal_tracks = []
289296

290297
# Extract the new tracks from the playlist that we haven't already seen before
291-
spotify_tracks = await get_tracks_from_spotify_playlist(spotify_session, spotify_playlist)
292-
old_tidal_tracks = await get_all_playlist_tracks(tidal_playlist)
293298
populate_track_match_cache(spotify_tracks, old_tidal_tracks)
294299
await search_new_tracks_on_tidal(tidal_session, spotify_tracks, spotify_playlist['name'], config)
295300
new_tidal_track_ids = get_tracks_for_new_tidal_playlist(spotify_tracks)

0 commit comments

Comments
 (0)