Correct way for get configurable listen url track

This commit is contained in:
localhost_frssoft 2022-11-18 18:57:25 +03:00
parent 1f502ef52a
commit d56630b71e
9 changed files with 33 additions and 20 deletions

View file

@ -1,5 +1,5 @@
import src.fw_artists import src.fw_artists
from src.fw_api import get_artists, get_tracks, get_albums, concatinate_endpoint from src.fw_api import get_artists, get_tracks, get_albums, get_audio_file
from src.settings import get_config from src.settings import get_config
from src.mpv_control import player, player_menu, track_url_to_uuid from src.mpv_control import player, player_menu, track_url_to_uuid
from pyfzf.pyfzf import FzfPrompt from pyfzf.pyfzf import FzfPrompt
@ -62,7 +62,7 @@ def play_album(album_id):
tracks_results = tracks.get('results') tracks_results = tracks.get('results')
storage = {} storage = {}
for i in tracks_results: for i in tracks_results:
listen_url = concatinate_endpoint(i.get('listen_url')) listen_url = get_audio_file(i['listen_url'], True)
storage[track_url_to_uuid(listen_url)] = i storage[track_url_to_uuid(listen_url)] = i
player.loadfile(listen_url, 'append-play') player.loadfile(listen_url, 'append-play')
player_menu("Album playing...", storage) player_menu("Album playing...", storage)

View file

@ -58,8 +58,19 @@ def select_instance(new_instance=None):
set_http_header(['Authorization: ' + 'Bearer ' + token]) set_http_header(['Authorization: ' + 'Bearer ' + token])
def concatinate_endpoint(endpoint): @logger.catch
return 'https://' + instance + endpoint def get_audio_file(track_uuid, listen_url=False, download=False, transcoding=False, to='ogg'):
if not transcoding:
to = None
params = {
"download": download,
"to": to
}
if listen_url:
r = s.head(f'https://{instance}{track_uuid}', params=params)
else:
r = s.head(f'https://{instance}/api/v1/listen/{track_uuid}', params=params)
return r.url
@logger.catch @logger.catch

View file

@ -1,4 +1,4 @@
from src.fw_api import get_artists, get_tracks, concatinate_endpoint from src.fw_api import get_artists, get_tracks, get_audio_file
from src.fw_albums import list_albums from src.fw_albums import list_albums
from src.mpv_control import player, player_menu, track_url_to_uuid from src.mpv_control import player, player_menu, track_url_to_uuid
from pyfzf.pyfzf import FzfPrompt from pyfzf.pyfzf import FzfPrompt
@ -48,7 +48,7 @@ def play_artist(artist_id):
tracks_results = tracks.get('results') tracks_results = tracks.get('results')
tracks_next = tracks.get('next') tracks_next = tracks.get('next')
for i in tracks_results: for i in tracks_results:
listen_url = concatinate_endpoint(i.get('listen_url')) listen_url = get_audio_file(i['listen_url'], True)
storage[track_url_to_uuid(listen_url)] = i storage[track_url_to_uuid(listen_url)] = i
player.loadfile(listen_url, 'append-play') player.loadfile(listen_url, 'append-play')
if tracks_next: if tracks_next:

View file

@ -1,5 +1,5 @@
from src.fw_api import get_favorires_tracks, concatinate_endpoint from src.fw_api import get_favorires_tracks, get_audio_file
from src.mpv_control import player, player_menu from src.mpv_control import player, player_menu, track_url_to_uuid
from pyfzf.pyfzf import FzfPrompt from pyfzf.pyfzf import FzfPrompt
from loguru import logger from loguru import logger
@ -49,8 +49,8 @@ Limit the results to a given user or pod:
def play_track(track): def play_track(track):
storage = {} storage = {}
listen_url = concatinate_endpoint(track.get('listen_url')) listen_url = get_audio_file(track['listen_url'], True)
storage[listen_url] = track storage[track_url_to_uuid(listen_url)] = track
player.loadfile(listen_url, 'append-play') player.loadfile(listen_url, 'append-play')
track_name = track.get('title') track_name = track.get('title')
player_menu(f"{track_name} playing...", storage) player_menu(f"{track_name} playing...", storage)

View file

@ -1,4 +1,4 @@
from src.fw_api import get_playlists, get_playlist_tracks, concatinate_endpoint from src.fw_api import get_playlists, get_playlist_tracks, get_audio_file
from src.mpv_control import player, player_menu, track_url_to_uuid from src.mpv_control import player, player_menu, track_url_to_uuid
from pyfzf.pyfzf import FzfPrompt from pyfzf.pyfzf import FzfPrompt
from loguru import logger from loguru import logger
@ -47,7 +47,7 @@ def play_playlist(playlist_id):
tracks_next = tracks.get('next') tracks_next = tracks.get('next')
for i in tracks_results: for i in tracks_results:
track = i.get('track') track = i.get('track')
listen_url = concatinate_endpoint(track['listen_url']) listen_url = get_audio_file(track['listen_url'], True)
storage[track_url_to_uuid(listen_url)] = track storage[track_url_to_uuid(listen_url)] = track
player.loadfile(listen_url, 'append-play') player.loadfile(listen_url, 'append-play')
if tracks_next: if tracks_next:

View file

@ -1,4 +1,4 @@
from src.fw_api import s, get_radios, post_radio_session, get_track_radio, list_libraries, concatinate_endpoint, favorite_track from src.fw_api import s, get_radios, post_radio_session, get_track_radio, list_libraries, favorite_track, get_audio_file
from src.fw_libraries import libraries from src.fw_libraries import libraries
from src.utils import download_track from src.utils import download_track
from src.mpv_control import player, track_url_to_uuid from src.mpv_control import player, track_url_to_uuid
@ -116,7 +116,7 @@ def radio_load(id_radio=None, type_radio='custom', name=None, related_object=Non
player.pause = True player.pause = True
elif select == 'Download': elif select == 'Download':
print('Downloading...') print('Downloading...')
name_downloaded = download_track(player.filename) name_downloaded = download_track(player.stream_open_filename)
print(f'Downloaded: {name_downloaded}') print(f'Downloaded: {name_downloaded}')
elif select == 'Info': elif select == 'Info':
track = audio_info.get(track_url_to_uuid()) track = audio_info.get(track_url_to_uuid())
@ -167,8 +167,8 @@ def radio_get_track(radio_session_id):
return return
else: else:
track = radio_context.get('track') track = radio_context.get('track')
listen_url = track.get('listen_url') listen_url = track['listen_url']
audio_info[track_url_to_uuid(listen_url)] = track audio_info[track_url_to_uuid(listen_url)] = track
player.loadfile(concatinate_endpoint(listen_url), 'append-play') player.loadfile(get_audio_file(listen_url, listen_url=True), 'append-play')

View file

@ -1,4 +1,4 @@
from src.fw_api import get_tracks, concatinate_endpoint from src.fw_api import get_tracks, get_audio_file
from src.mpv_control import player, player_menu, track_url_to_uuid from src.mpv_control import player, player_menu, track_url_to_uuid
from pyfzf.pyfzf import FzfPrompt from pyfzf.pyfzf import FzfPrompt
from loguru import logger from loguru import logger
@ -35,7 +35,7 @@ def list_tracks(pg=None, search=None):
def play_track(track): def play_track(track):
storage = {} storage = {}
listen_url = concatinate_endpoint(track.get('listen_url')) listen_url = get_audio_file(track['listen_url'], True)
storage[track_url_to_uuid(listen_url)] = track storage[track_url_to_uuid(listen_url)] = track
player.loadfile(listen_url, 'append-play') player.loadfile(listen_url, 'append-play')
track_name = track.get('title') track_name = track.get('title')

View file

@ -22,10 +22,11 @@ def set_http_header(headers=[]):
def track_url_to_uuid(listen_url=None): def track_url_to_uuid(listen_url=None):
'''Attempt get uuid from track listen url or current playing url'''
if listen_url: if listen_url:
uuid = listen_url.split(r'/')[-2] uuid = listen_url.split(r'/')[-2]
else: else:
uuid = player.filename.split(r'/')[-2] uuid = player.stream_open_filename.split(r'/')[-2]
return uuid return uuid
@ -53,7 +54,7 @@ def player_menu(header='', storage={}):
player.pause = True player.pause = True
elif select == 'Download': elif select == 'Download':
print('Downloading...') print('Downloading...')
name_downloaded = download_track(player.filename) name_downloaded = download_track(player.stream_open_filename)
print(f'Downloaded: {name_downloaded}') print(f'Downloaded: {name_downloaded}')
elif select == 'Info': elif select == 'Info':
track = storage.get(track_url_to_uuid()) track = storage.get(track_url_to_uuid())

View file

@ -11,6 +11,7 @@ def get_remote_file_name(url):
def download_track(url, name=None): def download_track(url, name=None):
url = url.split('?')[0] # Stripe all params from url
r = src.fw_api.s.get(url) r = src.fw_api.s.get(url)
if not name: if not name:
name = get_remote_file_name(url) name = get_remote_file_name(url)