-
Notifications
You must be signed in to change notification settings - Fork 120
Feature: Fix playlist v2 endpoint usage. Add pagination workers from mopidy-tidal #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f81e672
Move "data" field validation/fallback for consistency.
tehkillerbee faa76d3
Playlists: Ensure only PLAYLIST types are returned when fetching from…
tehkillerbee f86a2fc
Moved get playlist_folders() helper for consistency.
tehkillerbee 227a464
Add workers from mopidy-tidal
tehkillerbee f8c151f
Rearrange args (order, order_direction should be last)
tehkillerbee ec3b40d
Add paginated getters for artists, albums, playlists and tracks
tehkillerbee ba20831
Formatting
tehkillerbee 07cedb7
Apply suggestions from code review
tehkillerbee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import logging | ||
from concurrent.futures import ThreadPoolExecutor | ||
from typing import Callable | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def func_wrapper(args): | ||
(f, offset, *args) = args | ||
try: | ||
items = f(*args) | ||
except Exception as e: | ||
log.error("Failed to run %s(offset=%d, args=%s)", f, offset, args) | ||
log.exception(e) | ||
items = [] | ||
return list((i + offset, item) for i, item in enumerate(items)) | ||
|
||
|
||
def get_items( | ||
func: Callable, | ||
*args, | ||
parse: Callable = lambda _: _, | ||
chunk_size: int = 50, | ||
processes: int = 2, | ||
): | ||
"""This function performs pagination on a function that supports `limit`/`offset` | ||
parameters and it runs API requests in parallel to speed things up.""" | ||
items = [] | ||
offsets = [-chunk_size] | ||
remaining = chunk_size * processes | ||
|
||
with ThreadPoolExecutor( | ||
processes, thread_name_prefix=f"mopidy-tidal-{func.__name__}-" | ||
) as pool: | ||
while remaining == chunk_size * processes: | ||
offsets = [offsets[-1] + chunk_size * (i + 1) for i in range(processes)] | ||
|
||
pool_results = pool.map( | ||
func_wrapper, | ||
[ | ||
( | ||
func, | ||
offset, | ||
chunk_size, # limit | ||
offset, # offset | ||
*args, # extra args (e.g. order, order_direction) | ||
) | ||
for offset in offsets | ||
], | ||
) | ||
|
||
new_items = [] | ||
for results in pool_results: | ||
new_items.extend(results) | ||
|
||
remaining = len(new_items) | ||
items.extend(new_items) | ||
|
||
items = [_ for _ in items if _] | ||
sorted_items = list( | ||
map(lambda item: item[1], sorted(items, key=lambda item: item[0])) | ||
) | ||
|
||
return list(map(parse, sorted_items)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.