You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wrote some code the other day in my repo spotify_to_tidal like below. It would be good to either incorporate a similar API upstream, or to expose a cleaner way for clients to do this without relying on internal implementation details like ._base_url and .request. My concern is that there will be a lot of maintenance work on my side to keep this code working in subsequent releases of this repo
asyncdef_get_all_chunks(url, session, parser, params={}):
""" Helper function to get all items from a Tidal endpoint in parallel The main library doesn't provide the total number of items or expose the raw json, so use this wrapper instead """def_make_request(offset: int=0):
new_params=paramsnew_params['offset'] =offsetreturnsession.request.map_request(url, params=new_params)
first_chunk_raw=_make_request()
limit=first_chunk_raw['limit']
total=first_chunk_raw['totalNumberOfItems']
items=session.request.map_json(first_chunk_raw, parse=parser)
iflen(items) <total:
offsets= [limit*nforninrange(1, math.ceil(total/limit))]
extra_results=awaitatqdm.gather(
*[asyncio.to_thread(lambdaoffset: session.request.map_json(_make_request(offset), parse=parser), offset) foroffsetinoffsets],
desc="Fetching additional data chunks"
)
forextra_resultinextra_results:
items.extend(extra_result)
returnitemsasyncdefget_all_favorites(favorites: tidalapi.Favorites, order: str="NAME", order_direction: str="ASC", chunk_size: int=100) ->List[tidalapi.Track]:
""" Get all favorites from Tidal playlist in chunks """params= {
"limit": chunk_size,
"order": order,
"orderDirection": order_direction,
}
returnawait_get_all_chunks(f"{favorites.base_url}/tracks", session=favorites.session, parser=favorites.session.parse_track, params=params)
asyncdefget_all_playlists(user: tidalapi.User, chunk_size: int=10) ->List[tidalapi.Playlist]:
""" Get all user playlists from Tidal in chunks """print(f"Loading playlists from Tidal user")
params= {
"limit": chunk_size,
}
returnawait_get_all_chunks(f"users/{user.id}/playlists", session=user.session, parser=user.playlist.parse_factory, params=params)
asyncdefget_all_playlist_tracks(playlist: tidalapi.Playlist, chunk_size: int=20) ->List[tidalapi.Track]:
""" Get all tracks from Tidal playlist in chunks """params= {
"limit": chunk_size,
}
print(f"Loading tracks from Tidal playlist '{playlist.name}'")
returnawait_get_all_chunks(f"{playlist._base_url%playlist.id}/tracks", session=playlist.session, parser=playlist.session.parse_track, params=params)
The text was updated successfully, but these errors were encountered:
Good point. I did think about backporting similar functionality from mopidy-tidal to python-tidal as it makes sense to have this functionality in python-tidal to avoid the issues that you now face.
I wrote some code the other day in my repo spotify_to_tidal like below. It would be good to either incorporate a similar API upstream, or to expose a cleaner way for clients to do this without relying on internal implementation details like
._base_url
and.request
. My concern is that there will be a lot of maintenance work on my side to keep this code working in subsequent releases of this repoThe text was updated successfully, but these errors were encountered: