Skip to content

Commit

Permalink
Sort playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Apr 27, 2024
1 parent ace4427 commit 68c9e64
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions twitchdl/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def select_playlist_by_name(playlists: List[Playlist], quality: str) -> Playlist


def select_playlist_interactive(playlists: List[Playlist]) -> Playlist:
playlists = sorted(playlists, key=_playlist_key)
headers = ["#", "Name", "Group ID", "Resolution"]

rows = [
Expand All @@ -147,3 +148,23 @@ def select_playlist_interactive(playlists: List[Playlist]) -> Playlist:
no = utils.read_int("\nChoose quality", min=1, max=len(playlists) + 1, default=default)
playlist = playlists[no - 1]
return playlist


MAX = 1_000_000


def _playlist_key(playlist: Playlist) -> int:
"""Attempt to sort playlists so that source quality is on top, audio only
is on bottom and others are sorted descending by resolution."""
if playlist.is_source:
return 0

if playlist.group_id == "audio_only":
return MAX

try:
return MAX - int(playlist.name.split("p")[0])
except Exception:
pass

return MAX

0 comments on commit 68c9e64

Please sign in to comment.