funkwlmpv/src/fw_albums.py

69 lines
2.4 KiB
Python

import src.fw_artists
from src.fw_api import get_artists, get_tracks, get_albums, get_audio_file
from src.settings import get_config
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_albums(albums=None, pg=None, search=None, artist=None, include_channels=None, refresh=False):
show_artist_name_in_albums = get_config('show_artist_name_in_albums')
albums_next = None
albums_prev = None
play_artist_albums = False
if not albums:
albums = get_albums(q=search, artist=artist, include_channels=include_channels, refresh=refresh, pg=pg)
albums_next = albums.get('next')
albums_prev = albums.get('previous')
albums_results = albums.get('results')
if artist:
play_artist_albums = True
else:
play_artist_albums = True
albums_results = albums
view = ['Search']
if play_artist_albums:
view.append('Play all')
if albums_next:
view.append('Next page')
if albums_prev:
view.append('Prev page')
for i in albums_results:
index = albums_results.index(i)
album_name = i.get('title')
option_str = f'{index}.{album_name}'
artist_name = i.get('artist')
if show_artist_name_in_albums and isinstance(artist_name, dict):
artist_name = artist_name.get('name')
option_str += f' | Artist: {artist_name}'
view.append(f'{option_str}')
select = fzf.prompt(view)[0].split('.', 1)[0]
if select == 'Next page':
list_albums(pg=albums_next)
elif select == 'Prev page':
list_albums(pg=albums_prev)
elif select == 'Search':
print('Search by albums: ')
list_albums(search=input())
elif select == 'Play all':
if artist:
src.fw_artists.play_artist(artist)
else:
src.fw_artists.play_artist(albums_results[0].get('artist'))
else:
play_album(album_id=albums_results[int(select)].get('id'))
def play_album(album_id):
tracks = get_tracks(album=album_id, include_channels=True)
tracks_results = tracks.get('results')
storage = {}
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')
player_menu("Album playing...", storage)