funkwlmpv/src/fw_tracks.py
2022-11-18 13:22:55 +03:00

43 lines
1.3 KiB
Python

from src.fw_api import get_tracks, concatinate_endpoint
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_tracks(pg=None, search=None):
tracks = get_tracks(q=search, pg=pg)
tracks_next = tracks.get('next')
tracks_prev = tracks.get('previous')
tracks_results = tracks.get('results')
view = ['Search']
if tracks_next:
view.append('Next page')
if tracks_prev:
view.append('Prev page')
for i in tracks_results:
index = tracks_results.index(i)
track_name = i.get('title')
view.append(f'{index}.{track_name}')
select = fzf.prompt(view)[0].split('.', 1)[0]
if select == 'Next page':
list_tracks(pg=tracks_next)
elif select == 'Prev page':
list_tracks(pg=tracks_prev)
elif select == 'Search':
print('Search by track:')
list_tracks(search=input())
else:
play_track(track=tracks_results[int(select)])
def play_track(track):
storage = {}
listen_url = concatinate_endpoint(track.get('listen_url'))
storage[track_url_to_uuid(listen_url)] = track
player.loadfile(listen_url, 'append-play')
track_name = track.get('title')
player_menu(f"{track_name} playing...", storage)