From 1e3972d0098d2de815c3fcb642461ca3ed5da8d3 Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Mon, 2 Jan 2023 17:43:50 +0300 Subject: [PATCH] Now uses listen_token for listening private tracks Also this is required for external transcoder --- src/fw_api.py | 25 ++++++++++++++++++++----- src/mpv_control.py | 4 ---- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/fw_api.py b/src/fw_api.py index 32daf16..6a16bf3 100644 --- a/src/fw_api.py +++ b/src/fw_api.py @@ -1,4 +1,3 @@ -from src.mpv_control import set_http_header from src.settings import get_config import requests import json @@ -32,6 +31,8 @@ class current_instance: s = requests.Session() instance = get_config('instance') token = auth.get(instance) + listen_token = None + @logger.catch def select_instance(new_instance=None): @@ -41,14 +42,14 @@ class current_instance: new_token = auth.get(current_instance.instance) current_instance.s.headers.update({"Authorization": None, "Accept-encoding": 'gzip'}) - set_http_header() + current_instance.token = new_token + current_instance.listen_token = None if new_token: s.get(f'https://{current_instance.instance}') s.headers.update({ "Authorization": "Bearer " + new_token, "Accept-encoding": 'gzip' }) - set_http_header(['Authorization: ' + 'Bearer ' + new_token]) if current_instance.token: @@ -56,12 +57,21 @@ if current_instance.token: "Authorization": "Bearer " + current_instance.token, "Accept-encoding": 'gzip' }) - set_http_header(['Authorization: ' + 'Bearer ' + current_instance.token]) else: current_instance.s.headers.update({"Accept-encoding": 'gzip'}) # Get cookies from unauthorized instance for working some functionality (radios) current_instance.s.get(f'https://{current_instance.instance}/') - set_http_header() + + +@logger.catch +def get_me(): + '''Return current user if token avalaible''' + if not current_instance.token: + return + r = current_instance.s.get(f'https://{current_instance.instance}/api/v1/users/me') + resp = r.json() + current_instance.listen_token = resp['tokens']['listen'] + return resp @logger.catch @@ -79,6 +89,11 @@ def get_audio_file(track_uuid, listen_url=False, download=False, "download": download, "to": to } + if current_instance.token and not current_instance.listen_token: + get_me() + if current_instance.listen_token: + params['token'] = current_instance.listen_token + if not transcoding: del params['to'] if listen_url: diff --git a/src/mpv_control.py b/src/mpv_control.py index 42fe986..fdcf67c 100644 --- a/src/mpv_control.py +++ b/src/mpv_control.py @@ -104,10 +104,6 @@ def universal_observer(_name, value): time.sleep(1) -def set_http_header(headers=[]): - player.http_header_fields = headers - - @logger.catch def player_menu(header='', storage={}): player_fw_storage.storage.update(storage)