funkwlmpv/src/fw_artists.py

81 lines
2.8 KiB
Python
Raw Normal View History

from src.fw_api import get_artists, get_tracks, get_audio_file
2023-06-12 14:30:49 +03:00
from src.fw_api_hints import hint_scope
2022-11-08 19:58:27 +02:00
from src.fw_albums import list_albums
from src.fw_libraries import libraries
2022-11-18 12:22:55 +02:00
from src.mpv_control import player, player_menu, track_url_to_uuid
2022-11-08 19:58:27 +02:00
from pyfzf.pyfzf import FzfPrompt
from loguru import logger
fzf = FzfPrompt()
2022-12-18 03:02:53 +02:00
2022-11-08 19:58:27 +02:00
@logger.catch
2023-06-12 14:30:49 +03:00
def list_artists(pg=None, search=None, library=None, scope=None):
artists = get_artists(q=search, library=library, pg=pg, scope=scope)
2022-11-08 19:58:27 +02:00
artists_next = artists.get('next')
artists_prev = artists.get('previous')
artists_results = artists.get('results')
2023-06-12 14:30:49 +03:00
view = ['Search', 'Library', 'Limit by scope']
2022-11-08 19:58:27 +02:00
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')
2023-06-12 14:06:12 +03:00
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]
2022-11-08 19:58:27 +02:00
if select == 'Next page':
list_artists(pg=artists_next)
elif select == 'Prev page':
list_artists(pg=artists_prev)
2022-11-08 23:24:59 +02:00
elif select == 'Search':
print('Search by artist:')
list_artists(search=input())
elif select == 'Library':
select_lib = libraries()
list_artists(library=select_lib)
2023-06-12 14:30:49 +03:00
elif select == 'Limit by scope':
hint_scope()
scope = input()
list_artists(scope=scope)
2022-11-08 19:58:27 +02:00
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'])
2022-11-08 19:58:27 +02:00
def play_artist(artist_id):
2022-11-09 13:47:38 +02:00
tracks = get_tracks(artist=artist_id, include_channels=True, pg=None)
tracks_next = tracks.get('next')
tracks_count = tracks.get('count')
2022-11-08 19:58:27 +02:00
storage = {}
2022-11-09 13:47:38 +02:00
if tracks_count > 50:
print(f'Loading {tracks_count} tracks...')
2022-11-10 01:23:18 +02:00
elif tracks_count == 0:
logger.warning('Empty tracks. Nothing to do')
return
2022-11-09 13:47:38 +02:00
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)
2022-11-18 12:22:55 +02:00
storage[track_url_to_uuid(listen_url)] = i
2022-11-09 13:47:38 +02:00
player.loadfile(listen_url, 'append-play')
if tracks_next:
2022-12-18 03:02:53 +02:00
tracks = get_tracks(
artist=artist_id, include_channels=True, pg=tracks_next)
2022-11-09 13:47:38 +02:00
else:
break
2022-11-18 12:22:55 +02:00
artist_name = tracks.get('results')[0]['artist']['name']
2022-11-10 01:23:18 +02:00
player_menu(f"Artist {artist_name} playing...", storage)