From f5b7e0e6f6f9a45534929d763456f3383d7f3ae0 Mon Sep 17 00:00:00 2001 From: David Trattnig <david.trattnig@o94.at> Date: Fri, 17 Jul 2020 18:15:51 +0200 Subject: [PATCH] Trackservice call to Engine API. --- modules/base/utils.py | 28 ++++++++++++++++++--- modules/core/channels.py | 8 ++++++ modules/plugins/trackservice.py | 44 +++++++++++++++++++++++++++------ 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/modules/base/utils.py b/modules/base/utils.py index 34e7133d..e6a33efe 100644 --- a/modules/base/utils.py +++ b/modules/base/utils.py @@ -69,7 +69,7 @@ class EngineUtil: @staticmethod - def get_playlist_type(id): + def get_playlist_type(fallback_id): """ Converts an playlist type ID to the playlist type object. @@ -79,11 +79,11 @@ class EngineUtil: Returns: type (PlaylistType): The type """ - if id == 0: + if fallback_id == 0: return PlaylistType.DEFAULT - elif id == 1: + elif fallback_id == 1: return PlaylistType.SHOW - elif id == 2: + elif fallback_id == 2: return PlaylistType.TIMESLOT else: return PlaylistType.STATION @@ -145,6 +145,26 @@ class SimpleUtil: """ + @staticmethod + def clean_dictionary(data): + """ + Delete keys with the value `None` in a dictionary, recursively. + This alters the input so you may wish to `copy` the dict first. + + Args: + data (dict): The dicationary + + Returns: + (dict): + """ + for key, value in list(data.items()): + if value is None: + del data[key] + elif isinstance(value, dict): + SimpleUtil.clean_dictionary(value) + return data + + @staticmethod def fmt_time(timestamp): """ diff --git a/modules/core/channels.py b/modules/core/channels.py index dce672c3..b17cac78 100644 --- a/modules/core/channels.py +++ b/modules/core/channels.py @@ -55,18 +55,22 @@ class ChannelType(Enum): """ FILESYSTEM = { "id": "fs", + "numeric": 0, "channels": [Channel.FILESYSTEM_A, Channel.FILESYSTEM_B] } HTTP = { "id": "http", + "numeric": 1, "channels": [Channel.HTTP_A, Channel.HTTP_B] } HTTPS = { "id": "https", + "numeric": 2, "channels": [Channel.HTTPS_A, Channel.HTTPS_B] } LIVE = { "id": "live", + "numeric": 3, "channels": [ Channel.LIVE_0, Channel.LIVE_1, @@ -80,6 +84,10 @@ class ChannelType(Enum): def channels(self): return self.value["channels"] + @property + def numeric(self): + return self.value["numeric"] + def __str__(self): return str(self.value["id"]) diff --git a/modules/plugins/trackservice.py b/modules/plugins/trackservice.py index 715ce886..0e4e8329 100644 --- a/modules/plugins/trackservice.py +++ b/modules/plugins/trackservice.py @@ -17,8 +17,13 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. - +import json import logging +import requests + +from modules.base.utils import SimpleUtil as SU + + class TrackserviceHandler(): """ @@ -27,6 +32,7 @@ class TrackserviceHandler(): logger = None config = None soundsystem = None + last_schedule = None def __init__(self, config, soundsystem): @@ -42,18 +48,42 @@ class TrackserviceHandler(): """ Some track started playing. """ - self.logger.info("::: CALLED TRACKSERVICE HANDLER :::") - self.logger.info("PLAYING: %s <<< " % (str(entry))) self.store_trackservice(entry) - self.store_schedule(entry) + self.store_show_info(entry) def store_trackservice(self, entry): """ - Posts the given `PlaylistEntry` to the Engine API Trackservice. + Posts the given `PlaylistEntry` to the Engine API Playlog. """ + data = dict() + data["track_start"] = entry.entry_start + data["track_artist"] = entry.meta_data.artist + data["track_album"] = entry.meta_data.album + data["track_title"] = entry.meta_data.title + data["track_duration"] = entry.duration + data["track_type"] = entry.get_type().numeric + data["timeslot_id"] = entry.playlist.schedule.schedule_id + data["show_name"] = entry.playlist.schedule.show_name + data["log_source"] = self.config.get("api_engine_number") + data = SU.clean_dictionary(data) + + self.logger.info("Posting schedule update to Engine API...") + url = self.config.get("api_engine_playlog_url") + headers = {'content-type': 'application/json'} + body = json.dumps(data, indent=4, sort_keys=True, default=str) + response = requests.post(url, data=body, headers=headers) + self.logger.info("Engine API response: %s" % response.status_code) + + - def store_schedule(self, entry): + def store_show_info(self, entry): """ - Posts the given `PlaylistEntry` to the Engine API Clock Data. + Posts the current and next show information to the Engine API. """ + current_schedule = entry.playlist.schedule + if current_schedule == self.last_schedule: + self.logger.info("Schedule didn't change since last update.") + else: + self.logger.info("Posting schedule update to Engine API...") + # TODO Implement \ No newline at end of file -- GitLab