funkwlmpv/src/fw_tracks.py

43 lines
1.3 KiB
Python
Raw Normal View History

from src.fw_api import get_tracks, get_audio_file
2022-11-18 10:22:55 +00:00
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 = get_audio_file(track['listen_url'], True)
2022-11-18 10:22:55 +00:00
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)