funkwlmpv/src/fw_artists.py
localhost_frssoft 06b2ebacd1 Fix menus when exit ESC or CTRL+C
So... After a long time i did fix for some menus
Reduce errors hell in terminal
2023-06-15 00:52:34 +03:00

81 lines
2.8 KiB
Python

from src.fw_api import get_artists, get_tracks, get_audio_file
from src.fw_api_hints import hint_scope
from src.fw_albums import list_albums
from src.fw_libraries import libraries
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_artists(pg=None, search=None, library=None, scope=None):
artists = get_artists(q=search, library=library, pg=pg, scope=scope)
artists_next = artists.get('next')
artists_prev = artists.get('previous')
artists_results = artists.get('results')
view = ['Search', 'Library', 'Limit by scope']
if artists_next:
view.append('Next page')
if artists_prev:
view.append('Prev page')
for i in artists_results:
index = artists_results.index(i)
artist_name = i.get('name')
artist_tracks_count = i.get('tracks_count')
view.append(f'{index}.{artist_name} | {artist_tracks_count}')
select = fzf.prompt(view, '--header=\'map: artist | tracks count\'')
if select == []:
return
else:
select = select[0].split('.', 1)[0]
if select == 'Next page':
list_artists(pg=artists_next)
elif select == 'Prev page':
list_artists(pg=artists_prev)
elif select == 'Search':
print('Search by artist:')
list_artists(search=input())
elif select == 'Library':
select_lib = libraries()
list_artists(library=select_lib)
elif select == 'Limit by scope':
hint_scope()
scope = input()
list_artists(scope=scope)
else:
albums = artists_results[int(select)].get('albums')
if albums:
list_albums(albums=albums)
else: # Fallback on tracks of selected artist
play_artist(artists_results[int(select)]['id'])
def play_artist(artist_id):
tracks = get_tracks(artist=artist_id, include_channels=True, 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:
listen_url = get_audio_file(i['listen_url'], True)
storage[track_url_to_uuid(listen_url)] = i
player.loadfile(listen_url, 'append-play')
if tracks_next:
tracks = get_tracks(
artist=artist_id, include_channels=True, pg=tracks_next)
else:
break
artist_name = tracks.get('results')[0]['artist']['name']
player_menu(f"Artist {artist_name} playing...", storage)