funkwlmpv/src/fw_playlists.py
2022-12-18 04:02:53 +03:00

60 lines
2.0 KiB
Python

from src.fw_api import get_playlists, get_playlist_tracks, get_audio_file
from src.mpv_control import player, player_menu, track_url_to_uuid
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 = get_audio_file(track['listen_url'], True)
storage[track_url_to_uuid(listen_url)] = track
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)