-
David Trattnig authoredDavid Trattnig authored
state.py 4.78 KiB
#
# Aura Engine
#
# Copyright (C) 2020 David Trattnig <david.trattnig@subsquare.at>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from collections import deque
from modules.base.exceptions import NoActiveEntryException
from modules.base.utils import SimpleUtil
from modules.database.model import SingleEntry, SingleEntryMetaData, PlaylistEntry, PlaylistEntryMetaData, TrackService
class PlayerStateService:
"""
PlayerStateService keeps a short history of currently playing entries. It stores the recent
active entries to a local cache `entry_history` being able to manage concurrently playing entries.
It also is in charge of storing relevant meta information of the currently playing entry to
the TrackService table.
"""
config = None
logger = None
entry_history = None
def __init__(self, config):
"""
Constructor
Args:
config (AuraConfig): Holds the engine configuration
"""
self.config = config
self.logger = logging.getLogger("AuraEngine")
self.entry_history = deque([None, None, None])
#
# PUBLIC METHODS
#
def set_active_entry(self, entry):
"""
Saves the currently playing entry to the local cache.
"""
self.entry_history.pop()
self.entry_history.appendleft(entry)
self.logger.info("Active entry history:\n"+str(self.entry_history))
def get_active_entry(self):
"""
Retrieves the currently playing `Entry` from the local cache.
"""
return self.entry_history[0]
def store_trackservice_entry(self, source):
"""
Stores the given entry in the Track Service.
Args:
source (String): The URI of the currently playing source
Raises:
(NoActiveEntryException): In case currently nothing is playing
"""
active_entry = self.get_active_entry()
if not active_entry:
raise NoActiveEntryException
if active_entry.filename == source:
trackservice = TrackService(active_entry)
trackservice.store(add=True, commit=True)
active_entry.trackservice_id = trackservice.id
active_entry.store(add=False, commit=True)
self.logger.info("Stored active entry '%s' to TrackService as '%s'" % (active_entry, trackservice))
else:
msg = "Active entry source '%s' != '%s' activated source." % (active_entry.filename, source)
self.logger.critical(SimpleUtil.red(msg))
# def adapt_trackservice_title(self, filename):
# """
# Updates the track-service entry with the info from a fallback track/playlist.
# """
# liquidsoap_offset = int(self.config.lqs_delay_offset)
# scheduled_entry = self.get_active_entry(liquidsoap_offset)
# entry = SingleEntry()
# meta = SingleEntryMetaData()
# # # Validate artist and title
# # if not title:
# # title = self.config.get("fallback_title_not_available")
# # Create Entry
# entry.filename = filename
# entry.duration = self.fallback_manager.get_track_duration(filename)
# if not entry.duration:
# self.logger.critical("Entry %s has no duration! This may cause malfunction of some engine services." % (str(entry)))
# # Create track service log for local station fallback (type=4)
# trackservice = TrackService(entry, 4)
# trackservice.store(add=True, commit=True)
# entry.store(add=True, commit=True)
# # Create Meta
# meta.artist = "----FIXME"
# meta.album = ""
# meta.title = "----TODO"
# meta.single_entry_id = entry.id
# meta.store(add=True, commit=True)
# # Reference each other
# entry.meta_data_id = meta.id
# entry.trackservice_id = trackservice.id
# entry.store(add=False, commit=True)
# msg = "Track Service active track '%s' updated with fallback source '%s - %s'!" % (scheduled_entry, meta.artist, meta.title)
# self.logger.info(msg)
# return msg
#
# PRIVATE METHODS
#