2018-02-27 00:06:15 +01:00
|
|
|
import requests, json
|
|
|
|
from urllib.parse import urlencode
|
2018-02-26 22:55:01 +01:00
|
|
|
from functools import wraps
|
|
|
|
|
2018-03-02 18:35:10 +01:00
|
|
|
# This must be set to be able to use it on remote hosts
|
2018-02-27 00:06:15 +01:00
|
|
|
BASE_URL = "http://localhost:8080/api"
|
2018-03-02 18:35:10 +01:00
|
|
|
def set_endpoint(base_url:str):
|
|
|
|
global BASE_URL
|
|
|
|
BASE_URL = base_url
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
# Exceptions:
|
|
|
|
class APIError(Exception): pass
|
|
|
|
|
|
|
|
# decorator:
|
2018-03-04 15:15:59 +01:00
|
|
|
# (TODO): Add logging
|
|
|
|
def request_delete(func):
|
|
|
|
@wraps(func)
|
|
|
|
def new_func(*args, **kwargs):
|
|
|
|
url, data = func(*args, **kwargs)
|
|
|
|
if type(data) is dict: data = json.dumps(data)
|
|
|
|
response = requests.delete(f"{BASE_URL}/{url}", data=data)
|
|
|
|
data = json.loads(response.text)
|
|
|
|
if "error" not in data or data["error"] != False:
|
|
|
|
print(data)
|
|
|
|
raise APIError(data["error"])
|
|
|
|
return data["success"]
|
|
|
|
return new_func
|
2018-02-26 22:55:01 +01:00
|
|
|
def request_post(func):
|
|
|
|
@wraps(func)
|
|
|
|
def new_func(*args, **kwargs):
|
|
|
|
url, data = func(*args, **kwargs)
|
2018-03-04 04:08:20 +01:00
|
|
|
if type(data) is dict: data = json.dumps(data)
|
2018-02-27 00:06:15 +01:00
|
|
|
response = requests.post(f"{BASE_URL}/{url}", data=data)
|
|
|
|
data = json.loads(response.text)
|
|
|
|
if "error" not in data or data["error"] != False:
|
|
|
|
print(data)
|
2018-02-28 22:17:44 +01:00
|
|
|
raise APIError(data["error"])
|
2018-02-27 00:06:15 +01:00
|
|
|
return data["success"]
|
2018-02-26 22:55:01 +01:00
|
|
|
return new_func
|
|
|
|
def request_get(func):
|
|
|
|
@wraps(func)
|
|
|
|
def new_func(*args, **kwargs):
|
|
|
|
url = func(*args, **kwargs)
|
2018-02-27 00:06:15 +01:00
|
|
|
response = requests.get(f"{BASE_URL}/{url}")
|
|
|
|
data = json.loads(response.text)
|
|
|
|
if "error" not in data or data["error"] != False:
|
2018-02-28 22:17:44 +01:00
|
|
|
raise APIError(data["errortext"])
|
2018-02-27 00:06:15 +01:00
|
|
|
return data["value"]
|
2018-02-26 22:55:01 +01:00
|
|
|
return new_func
|
|
|
|
|
|
|
|
# methods:
|
|
|
|
|
|
|
|
@request_post
|
2022-02-20 00:21:55 +01:00
|
|
|
def load_path(path:str, data: dict = None):
|
2018-02-27 00:06:15 +01:00
|
|
|
args = urlencode(locals())
|
2018-03-04 04:08:20 +01:00
|
|
|
return f"load?{args}", data
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_get
|
|
|
|
def is_playing():
|
2022-02-20 00:21:55 +01:00
|
|
|
return "play"
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_post
|
2022-02-20 00:21:55 +01:00
|
|
|
def set_playing(play: bool):
|
2018-02-27 00:06:15 +01:00
|
|
|
args = urlencode(locals())
|
|
|
|
return f"play?{args}", None
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_get
|
|
|
|
def get_volume():
|
2022-02-20 00:21:55 +01:00
|
|
|
return "volume"
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_post
|
2022-02-20 00:21:55 +01:00
|
|
|
def set_volume(volume: int): # between 0 and 100 (you may also exceed 100)
|
2018-02-27 00:06:15 +01:00
|
|
|
args = urlencode(locals())
|
|
|
|
return f"volume?{args}", None
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_get
|
|
|
|
def get_playlist():
|
2022-02-20 00:21:55 +01:00
|
|
|
return "playlist"
|
2018-02-26 22:55:01 +01:00
|
|
|
|
|
|
|
@request_post
|
|
|
|
def playlist_next():
|
2022-02-20 00:21:55 +01:00
|
|
|
return "playlist/next", None
|
2018-02-26 22:55:01 +01:00
|
|
|
|
2019-03-13 12:08:07 +01:00
|
|
|
@request_post
|
2022-02-20 00:21:55 +01:00
|
|
|
def playlist_goto(index: int):
|
2019-03-13 12:08:07 +01:00
|
|
|
args = urlencode(locals())
|
|
|
|
return f"playlist/goto?{args}", None
|
|
|
|
|
2018-02-26 22:55:01 +01:00
|
|
|
@request_post
|
|
|
|
def playlist_previous():
|
2022-02-20 00:21:55 +01:00
|
|
|
return "playlist/previous", None
|
2018-02-28 22:17:44 +01:00
|
|
|
|
2018-03-06 14:51:04 +01:00
|
|
|
@request_post
|
|
|
|
def playlist_shuffle():
|
2022-02-20 00:21:55 +01:00
|
|
|
return "playlist/shuffle", None
|
2018-03-06 14:51:04 +01:00
|
|
|
|
2018-03-04 15:15:59 +01:00
|
|
|
@request_delete
|
|
|
|
def playlist_clear():
|
2022-02-20 00:21:55 +01:00
|
|
|
return "playlist", None
|
2018-03-04 15:15:59 +01:00
|
|
|
|
|
|
|
@request_delete
|
2022-02-20 00:21:55 +01:00
|
|
|
def playlist_remove(index: int):
|
2018-03-04 15:15:59 +01:00
|
|
|
args = urlencode(locals())
|
|
|
|
return f"playlist?{args}", None
|
|
|
|
|
2018-03-05 21:48:11 +01:00
|
|
|
@request_post
|
2022-02-20 00:21:55 +01:00
|
|
|
def playlist_move(index1: int, index2: int):
|
2018-03-05 21:48:11 +01:00
|
|
|
args = urlencode(locals())
|
|
|
|
return f"playlist/move?{args}", None
|
|
|
|
|
2019-03-13 11:37:49 +01:00
|
|
|
@request_get
|
|
|
|
def get_playlist_looping():
|
2022-02-20 00:21:55 +01:00
|
|
|
return "playlist/loop"
|
2019-03-13 11:37:49 +01:00
|
|
|
|
|
|
|
@request_post
|
2022-02-20 00:21:55 +01:00
|
|
|
def playlist_set_looping(looping: bool):
|
2019-03-13 11:37:49 +01:00
|
|
|
return f"playlist/loop?loop={str(bool(looping)).lower()}", None
|
|
|
|
|
|
|
|
|
2018-02-28 22:17:44 +01:00
|
|
|
@request_get
|
|
|
|
def get_playback_pos():
|
2022-02-20 00:21:55 +01:00
|
|
|
return "time"
|
2018-02-28 22:17:44 +01:00
|
|
|
|
|
|
|
@request_post
|
2022-02-20 00:21:55 +01:00
|
|
|
def seek_absolute(pos: float):
|
2018-02-28 22:17:44 +01:00
|
|
|
args = urlencode(locals())
|
|
|
|
return f"time?{args}", None
|
|
|
|
|
|
|
|
@request_post
|
2022-02-20 00:21:55 +01:00
|
|
|
def seek_percent(percent: int):
|
2018-02-28 22:17:44 +01:00
|
|
|
args = urlencode(locals())
|
|
|
|
return f"time?{args}", None
|