Add managed items support and merge cleanup

This commit is contained in:
Matthew Stratford 2020-11-16 22:49:33 +00:00
parent cb06061143
commit 450698cd37
No known key found for this signature in database
GPG key ID: 9E53C8B3F0B57395
4 changed files with 37 additions and 14 deletions

View file

@ -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)

View file

@ -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

13
plan.py
View file

@ -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("/", '\\')

View file

@ -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: