BAPSicle/api_handler.py

100 lines
3.9 KiB
Python
Raw Normal View History

from helpers.state_manager import StateManager
2021-03-21 13:05:33 +00:00
import json
2021-04-08 20:15:15 +00:00
from multiprocessing import Queue, current_process
2021-03-21 13:05:33 +00:00
from helpers.logging_manager import LoggingManager
from helpers.myradio_api import MyRadioAPI
from setproctitle import setproctitle
from os import _exit
2021-03-21 13:05:33 +00:00
# The API handler is needed from the main flask thread to process API requests.
# Flask is not able to handle these during page loads, requests.get() hangs.
2021-04-08 20:15:15 +00:00
2021-04-08 19:53:51 +00:00
class APIHandler:
logger: LoggingManager
api: MyRadioAPI
server_to_q: Queue
server_from_q: Queue
2021-03-21 13:05:33 +00:00
def __init__(self, server_from_q: Queue, server_to_q: Queue, server_config: StateManager):
2021-04-08 19:53:51 +00:00
process_title = "APIHandler"
setproctitle(process_title)
2021-04-08 20:15:15 +00:00
current_process().name = process_title
2021-04-08 19:53:51 +00:00
self.server_from_q = server_from_q
self.server_to_q = server_to_q
self.logger = LoggingManager("APIHandler")
self.api = MyRadioAPI(self.logger, server_config)
2021-03-21 13:05:33 +00:00
2021-04-08 19:53:51 +00:00
self.handle()
2021-03-21 13:05:33 +00:00
2021-04-08 19:53:51 +00:00
def handle(self):
try:
while self.server_from_q:
# Wait for an API request to come in.
request = self.server_from_q.get()
self.logger.log.info("Received Request: {}".format(request))
if request == "LIST_PLANS":
self.server_to_q.put(
request + ":" + json.dumps(self.api.get_showplans())
)
elif request == "LIST_PLAYLIST_MUSIC":
self.server_to_q.put(
2021-04-08 20:15:15 +00:00
request + ":" +
json.dumps(self.api.get_playlist_music())
2021-04-08 19:53:51 +00:00
)
elif request == "LIST_PLAYLIST_AUX":
self.server_to_q.put(
request + ":" + json.dumps(self.api.get_playlist_aux())
)
2021-04-08 19:53:51 +00:00
else:
# Commands with params
command = request[: request.index(":")]
2021-04-08 20:15:15 +00:00
params = request[request.index(":") + 1:]
2021-04-08 19:53:51 +00:00
if command == "GET_PLAYLIST_AUX":
self.server_to_q.put(
request
+ ":"
+ json.dumps(self.api.get_playlist_aux_items(str(params)))
)
elif command == "GET_PLAYLIST_MUSIC":
self.server_to_q.put(
request
+ ":"
+ json.dumps(self.api.get_playlist_music_items(str(params)))
)
elif command == "SEARCH_TRACK":
try:
params = json.loads(params)
2021-04-08 19:53:51 +00:00
self.server_to_q.put(
request
+ ":"
+ json.dumps(
self.api.get_track_search(
2021-04-08 20:15:15 +00:00
str(params["title"]), str(
params["artist"])
2021-04-08 19:53:51 +00:00
)
)
)
except Exception as e:
self.logger.log.exception(
"Failed to parse params with message {}, command {}, params {}\n{}".format(
request, command, params, e
)
)
2021-04-08 19:53:51 +00:00
# Catch the handler being killed externally.
except KeyboardInterrupt:
self.logger.log.info("Received KeyboardInterupt")
except SystemExit:
self.logger.log.info("Received SystemExit")
except Exception as e:
2021-04-08 20:15:15 +00:00
self.logger.log.exception(
"Received unexpected exception: {}".format(e))
2021-04-08 19:53:51 +00:00
del self.logger
_exit(0)