funkwlmpv/src/fw_playlists.py

58 lines
2.0 KiB
Python
Raw Normal View History

2022-11-10 17:55:36 +02:00
from src.fw_api import get_playlists, get_playlist_tracks, concatinate_endpoint
2022-11-18 12:22:55 +02:00
from src.mpv_control import player, player_menu, track_url_to_uuid
2022-11-10 17:55:36 +02:00
from pyfzf.pyfzf import FzfPrompt
from loguru import logger
fzf = FzfPrompt()
@logger.catch
def list_playlists(pg=None, search=None):
playlists = get_playlists(q=search, pg=pg)
playlists_next = playlists.get('next')
playlists_prev = playlists.get('previous')
playlists_results = playlists.get('results')
view = ['Search']
if playlists_next:
view.append('Next page')
if playlists_prev:
view.append('Prev page')
for i in playlists_results:
index = playlists_results.index(i)
playlist_name = i.get('name')
view.append(f'{index}.{playlist_name}')
select = fzf.prompt(view)[0].split('.', 1)[0]
if select == 'Next page':
list_playlists(pg=playlists_next)
elif select == 'Prev page':
list_playlists(pg=playlists_prev)
elif select == 'Search':
print('Search by playlist:')
list_playlists(search=input())
else:
play_playlist(playlist_id=playlists_results[int(select)].get('id'))
def play_playlist(playlist_id):
tracks = get_playlist_tracks(playlist_id, pg=None)
tracks_next = tracks.get('next')
tracks_count = tracks.get('count')
storage = {}
if tracks_count > 50:
print(f'Loading {tracks_count} tracks...')
elif tracks_count == 0:
logger.warning('Empty tracks. Nothing to do')
return
while True:
tracks_results = tracks.get('results')
tracks_next = tracks.get('next')
for i in tracks_results:
track = i.get('track')
listen_url = concatinate_endpoint(track['listen_url'])
2022-11-18 12:22:55 +02:00
storage[track_url_to_uuid(listen_url)] = track
2022-11-10 17:55:36 +02:00
player.loadfile(listen_url, 'append-play')
if tracks_next:
tracks = get_playlist_tracks(playlist=playlist_id, pg=tracks_next)
else:
break
player_menu(f"Playlist playing...", storage)