2022-11-03 23:45:40 +00:00
|
|
|
from src.mpv_control import player
|
2022-11-01 10:15:28 +00:00
|
|
|
import requests, json, time
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
with open('.auth.json', 'rt') as f:
|
|
|
|
auth = json.loads(f.read())
|
|
|
|
|
2022-11-03 23:45:40 +00:00
|
|
|
s = requests.Session()
|
2022-11-01 10:15:28 +00:00
|
|
|
instance = 'fw.ponychord.rocks'
|
2022-11-03 23:45:40 +00:00
|
|
|
token = auth.get(instance)
|
|
|
|
|
|
|
|
if token:
|
2022-11-06 01:26:50 +00:00
|
|
|
s.headers.update({
|
2022-11-03 23:45:40 +00:00
|
|
|
"Authorization": "Bearer " + token
|
2022-11-06 01:26:50 +00:00
|
|
|
})
|
2022-11-03 23:45:40 +00:00
|
|
|
player.http_header_fields = ['Authorization: ' + 'Bearer ' + token]
|
|
|
|
else:
|
|
|
|
s.get(f'https://{instance}/') # Get cookies from unauthorized instance for working some functionality (radios)
|
|
|
|
player.http_header_fields = []
|
|
|
|
|
|
|
|
|
|
|
|
def select_instance(new_instance=None):
|
|
|
|
global instance
|
|
|
|
instance = new_instance
|
2022-11-08 14:09:54 +00:00
|
|
|
with open('.auth.json', 'rt') as f:
|
|
|
|
auth = json.loads(f.read())
|
2022-11-03 23:45:40 +00:00
|
|
|
new_token = auth.get(instance)
|
2022-11-06 01:26:50 +00:00
|
|
|
s.headers.update({"Authorization": None})
|
2022-11-03 23:45:40 +00:00
|
|
|
player.http_header_fields = []
|
|
|
|
if new_token:
|
|
|
|
s.get(f'https://{instance}')
|
2022-11-06 01:26:50 +00:00
|
|
|
s.headers.update({
|
2022-11-03 23:45:40 +00:00
|
|
|
"Authorization": "Bearer " + new_token
|
2022-11-06 01:26:50 +00:00
|
|
|
})
|
2022-11-03 23:45:40 +00:00
|
|
|
player.http_header_fields = ['Authorization: ' + 'Bearer ' + new_token]
|
|
|
|
|
|
|
|
|
2022-11-01 10:15:28 +00:00
|
|
|
def concatinate_endpoint(endpoint):
|
|
|
|
return 'https://' + instance + endpoint
|
|
|
|
|
2022-11-03 23:45:40 +00:00
|
|
|
|
|
|
|
def list_tracks(page=None, q=None, artist=None, album=None, favourites=None):
|
|
|
|
'''This function get tracks by params'''
|
|
|
|
params = {
|
|
|
|
'page': page,
|
|
|
|
'q': q,
|
|
|
|
'artist': artist,
|
|
|
|
'album': album,
|
|
|
|
'favourites': favourites
|
|
|
|
}
|
|
|
|
|
|
|
|
r = requests.get(f'https://{instance}/api/v1/tracks', params)
|
|
|
|
return r.json()
|
|
|
|
|
|
|
|
|
2022-11-08 00:29:44 +00:00
|
|
|
def list_libraries(page=None, page_size=None, q=None, scope='all'):
|
|
|
|
params = {
|
|
|
|
'page': page,
|
|
|
|
'page_size': page_size,
|
|
|
|
'q': q,
|
|
|
|
'scope': scope,
|
|
|
|
}
|
|
|
|
r = requests.get(f'https://{instance}/api/v1/libraries', params)
|
|
|
|
return r.json()
|
|
|
|
|
|
|
|
|
2022-11-01 10:15:28 +00:00
|
|
|
# [FunkWhale radios]
|
|
|
|
def get_radios():
|
2022-11-03 23:45:40 +00:00
|
|
|
r = s.get(f'https://{instance}/api/v1/radios/radios/')
|
2022-11-01 10:15:28 +00:00
|
|
|
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
def post_radio_session(requested_radio):
|
2022-11-03 23:45:40 +00:00
|
|
|
r = s.post(f'https://{instance}/api/v1/radios/sessions/', json=requested_radio)
|
2022-11-01 10:15:28 +00:00
|
|
|
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
def get_track_radio(radio_session):
|
2022-11-03 23:45:40 +00:00
|
|
|
r = s.post(f'https://{instance}/api/v1/radios/tracks/',json=radio_session)
|
2022-11-06 22:21:49 +00:00
|
|
|
try:
|
|
|
|
r.raise_for_status()
|
|
|
|
return r.json()
|
|
|
|
except:
|
|
|
|
logger.exception('Radio: get next track failed')
|
2022-11-01 10:15:28 +00:00
|
|
|
|
|
|
|
|