diff --git a/helpers/myradio_api.py b/helpers/myradio_api.py index f985ac7..4957023 100644 --- a/helpers/myradio_api.py +++ b/helpers/myradio_api.py @@ -5,6 +5,10 @@ MyRadio API Handler + In an ideal world, this module gives out and is fed PlanItems. + This means it can be swapped for a different backend in the (unlikely) event + someone else wants to integrate BAPsicle with something else. + Authors: Matthew Stratford Michael Grace @@ -15,21 +19,35 @@ import requests import config +from plan import PlanItem from helpers.os_environment import resolve_external_file_path class MyRadioAPI(): @classmethod - def secure_play(self, trackId: int, format: str = "mp3"): - url = "{}/NIPSWeb/secure_play?trackid={}&{}&api_key={}".format(config.MYRADIO_BASE_URL, trackId, format, config.API_KEY) + def get_filename(self, item: PlanItem): + format = "mp3" # TODO: Maybe we want this customisable? + if item.trackId: + itemType = "track" + id = item.trackId + url = "{}/NIPSWeb/secure_play?trackid={}&{}&api_key={}".format(config.MYRADIO_BASE_URL, id, format, config.API_KEY) - request = requests.get(url) + elif item.managedId: + itemType = "managed" + id = item.managedId + url = "{}/NIPSWeb/managed_play?managedid={}&api_key={}".format(config.MYRADIO_BASE_URL, id, config.API_KEY) + + else: + return None + + request = requests.get(url, timeout=10) if request.status_code != 200: - return False + return None + + filename: str = resolve_external_file_path("/music-tmp/{}-{}.{}".format(itemType, id, format)) - filename: str = resolve_external_file_path("/music-tmp/{}.{}".format(trackId,format)) with open(filename, 'wb') as file: file.write(request.content) diff --git a/helpers/state_manager.py b/helpers/state_manager.py index ed45590..680e1b1 100644 --- a/helpers/state_manager.py +++ b/helpers/state_manager.py @@ -45,11 +45,11 @@ class StateManager: try: file_state = json.loads(file_state) - # Turn from JSON -> PlanObject + # Turn from JSON -> PlanItem if "channel" in file_state: - file_state["loaded_item"] = PlanObject( + file_state["loaded_item"] = PlanItem( file_state["loaded_item"]) if file_state["loaded_item"] else None - file_state["show_plan"] = [PlanObject(obj) for obj in file_state["show_plan"]] + file_state["show_plan"] = [PlanItem(obj) for obj in file_state["show_plan"]] # Now feed the loaded state into the initialised state manager. self.state = file_state diff --git a/plan.py b/plan.py index a6963c1..5f31fca 100644 --- a/plan.py +++ b/plan.py @@ -39,6 +39,10 @@ class PlanItem: def trackId(self) -> int: return self._trackId + @property + def managedId(self) -> int: + return self._managedId + @property def __dict__(self) -> Dict[str, any]: return { @@ -60,7 +64,8 @@ class PlanItem: self._artist = new_item["artist"] # Fix any OS specific / or \'s - if os.path.sep == "/": - self._filename = self.filename.replace("\\", '/') - else: - self._filename = self.filename.replace("/", '\\') + if self.filename: + if os.path.sep == "/": + self._filename = self.filename.replace("\\", '/') + else: + self._filename = self.filename.replace("/", '\\') diff --git a/player.py b/player.py index 57e9626..479dae5 100644 --- a/player.py +++ b/player.py @@ -260,7 +260,7 @@ class Player(): filename: str = self.state.state["loaded_item"].filename if (filename == "" or filename == None): - filename = MyRadioAPI.secure_play(trackId = self.state.state["loaded_item"].trackId) + filename = MyRadioAPI.get_filename(item = self.state.state["loaded_item"]) # TODO: Update the show plan filenames try: @@ -404,7 +404,7 @@ class Player(): self.output() if loaded_state["loaded_item"]: - self.logger.log.info("Loading filename: " + loaded_state["loaded_item"].filename) + self.logger.log.info("Loading filename: " + str(loaded_state["loaded_item"].filename)) self.load(loaded_state["loaded_item"].timeslotItemId) if loaded_state["pos_true"] != 0: