From e653cf27fe7e233cf35c97b225c8b88ced72f8e7 Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Tue, 15 Nov 2022 04:22:32 +0300 Subject: [PATCH] Added view for favorites tracks --- funkwhale_cli.py | 4 ++++ src/fw_api.py | 15 ++++++++++++ src/fw_fav_tracks.py | 56 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/fw_fav_tracks.py diff --git a/funkwhale_cli.py b/funkwhale_cli.py index 4719b35..2a61c30 100644 --- a/funkwhale_cli.py +++ b/funkwhale_cli.py @@ -5,6 +5,7 @@ from src.fw_albums import list_albums from src.fw_tracks import list_tracks from src.fw_channels import list_channels from src.fw_playlists import list_playlists +from src.fw_fav_tracks import list_favorites_tracks import src.settings as settings import src.mpv_control import json, sys @@ -21,6 +22,7 @@ def main(): 'Tracks', 'Channels', 'Playlists', + 'Favorites', 'Search', 'Switch instance'] if not s.headers.get('Authorization'): @@ -42,6 +44,8 @@ def main(): list_channels() if selected == 'Playlists': list_playlists() + if selected == 'Favorites': + list_favorites_tracks() if selected == 'Search': search_type = fzf.prompt(('Federated', 'All types'))[0] if search_type == 'Federated': diff --git a/src/fw_api.py b/src/fw_api.py index 1da583d..b5a0889 100644 --- a/src/fw_api.py +++ b/src/fw_api.py @@ -79,6 +79,21 @@ def get_tracks(page=None, q=None, artist=None, album=None, favourites=None, incl r = s.get(f'https://{instance}/api/v1/tracks', params=params) return r.json() +@logger.catch +def get_favorires_tracks(page=None, q=None, scope=None, include_channels=None, pg=None): + '''This function get favorites tracks (not only for user)''' + params = { + 'page': page, + 'q': q, + 'scope': scope, + 'include_channels': include_channels + } + if pg: + r = s.get(pg) + else: + r = s.get(f'https://{instance}/api/v1/favorites/tracks/', params=params) + return r.json() + @logger.catch def get_artists(page=None, q=None, artist=None, album=None, favourites=None, refresh=False, pg=None): diff --git a/src/fw_fav_tracks.py b/src/fw_fav_tracks.py new file mode 100644 index 0000000..489ad1c --- /dev/null +++ b/src/fw_fav_tracks.py @@ -0,0 +1,56 @@ +from src.fw_api import get_favorires_tracks, concatinate_endpoint +from src.mpv_control import player, player_menu +from pyfzf.pyfzf import FzfPrompt +from loguru import logger + +fzf = FzfPrompt() + +@logger.catch +def list_favorites_tracks(pg=None, search=None, scope=None): + tracks = get_favorires_tracks(q=search, scope=scope, pg=pg) + tracks_next = tracks.get('next') + tracks_prev = tracks.get('previous') + tracks_results = tracks.get('results') + view = ['Search', 'Limit by scope'] + 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['track'].get('title') + who_user = i['user'].get('username') + view.append(f'{index}.{track_name} | liked: {who_user}') + select = fzf.prompt(view)[0].split('.', 1)[0] + if select == 'Next page': + list_favorites_tracks(pg=tracks_next) + elif select == 'Prev page': + list_favorites_tracks(pg=tracks_prev) + elif select == 'Search': + print('Search by track:') + list_favorites_tracks(search=input()) + elif select == 'Limit by scope': + print(''' +Limit the results to a given user or pod: + + Use all (or do not specify the property to disable scope filtering) + Use me to retrieve content relative to the current user + Use subscribed to retrieve content in libraries you follow + Use actor:alice@example.com to retrieve content relative to the account `alice@example.com + Use domain:example.com to retrieve content relative to the domain `example.com + +''') + scope = input() + list_favorites_tracks(scope=scope, search=search) + else: + play_track(track=tracks_results[int(select)]['track']) + + +def play_track(track): + storage = {} + listen_url = concatinate_endpoint(track.get('listen_url')) + storage[listen_url] = track + player.loadfile(listen_url, 'append-play') + track_name = track.get('title') + player_menu(f"{track_name} playing...", storage)