2022-11-18 17:57:25 +02:00
|
|
|
from src.fw_api import get_playlists, get_playlist_tracks, get_audio_file
|
2022-11-18 12:22:55 +02:00
|
|
|
from src.mpv_control import player, player_menu, track_url_to_uuid
|
2022-11-10 17:55:36 +02:00
|
|
|
from pyfzf.pyfzf import FzfPrompt
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
fzf = FzfPrompt()
|
|
|
|
|
2022-12-18 03:02:53 +02:00
|
|
|
|
2022-11-10 17:55:36 +02:00
|
|
|
@logger.catch
|
|
|
|
def list_playlists(pg=None, search=None):
|
|
|
|
playlists = get_playlists(q=search, pg=pg)
|
|
|
|
playlists_next = playlists.get('next')
|
|
|
|
playlists_prev = playlists.get('previous')
|
|
|
|
playlists_results = playlists.get('results')
|
|
|
|
view = ['Search']
|
|
|
|
if playlists_next:
|
|
|
|
view.append('Next page')
|
|
|
|
if playlists_prev:
|
|
|
|
view.append('Prev page')
|
|
|
|
|
|
|
|
for i in playlists_results:
|
|
|
|
index = playlists_results.index(i)
|
|
|
|
playlist_name = i.get('name')
|
|
|
|
view.append(f'{index}.{playlist_name}')
|
|
|
|
select = fzf.prompt(view)[0].split('.', 1)[0]
|
|
|
|
if select == 'Next page':
|
|
|
|
list_playlists(pg=playlists_next)
|
|
|
|
elif select == 'Prev page':
|
|
|
|
list_playlists(pg=playlists_prev)
|
|
|
|
elif select == 'Search':
|
|
|
|
print('Search by playlist:')
|
|
|
|
list_playlists(search=input())
|
|
|
|
else:
|
|
|
|
play_playlist(playlist_id=playlists_results[int(select)].get('id'))
|
|
|
|
|
2022-12-18 03:02:53 +02:00
|
|
|
|
2022-11-10 17:55:36 +02:00
|
|
|
def play_playlist(playlist_id):
|
|
|
|
tracks = get_playlist_tracks(playlist_id, 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:
|
|
|
|
track = i.get('track')
|
2022-11-18 17:57:25 +02:00
|
|
|
listen_url = get_audio_file(track['listen_url'], True)
|
2022-11-18 12:22:55 +02:00
|
|
|
storage[track_url_to_uuid(listen_url)] = track
|
2022-11-10 17:55:36 +02:00
|
|
|
player.loadfile(listen_url, 'append-play')
|
|
|
|
if tracks_next:
|
|
|
|
tracks = get_playlist_tracks(playlist=playlist_id, pg=tracks_next)
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
player_menu(f"Playlist playing...", storage)
|