Commit f5b7e0e6 authored by David Trattnig's avatar David Trattnig
Browse files

Trackservice call to Engine API.

parent 72812a16
......@@ -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):
"""
......
......@@ -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"])
......
......@@ -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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment