trackservice.py 5.96 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
# Aura Engine (https://gitlab.servus.at/aura/engine)
#
# Copyright (C) 2020 - The Aura Engine Team.
#
# 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/>.


20
import json
21
import logging
22
23
24
import requests

from modules.base.utils import SimpleUtil as SU
25
from modules.core.resources import ResourceUtil
26

27

David Trattnig's avatar
David Trattnig committed
28
class TrackServiceHandler():
29
    """
30
    Sends the trackservice entry and studio clock information to the `engine-api` REST endpoint.
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
    """
    logger = None
    config = None
    soundsystem = None


    def __init__(self, config, soundsystem):
        """
        Initialize.
        """
        self.logger = logging.getLogger("AuraEngine")
        self.config = config
        self.soundsystem = soundsystem


    def on_play(self, entry):
        """
        Some track started playing.
        """
        self.store_trackservice(entry)
51
        self.store_clock_info(entry)
52
53
54
55


    def store_trackservice(self, entry):
        """
56
        Posts the given `PlaylistEntry` to the Engine API Playlog.
57
        """
58
        data = dict()
59
60
61
        diff = (entry.entry_start_actual - entry.entry_start).total_seconds()
        self.logger.info("There's a difference of %s seconds between planned and actual start of the entry" % diff)
        data["track_start"] = entry.entry_start_actual
62
63
64
65
        if entry.meta_data:
            data["track_artist"] = entry.meta_data.artist
            data["track_album"] = entry.meta_data.album
            data["track_title"] = entry.meta_data.title
66
        data["track_duration"] = entry.duration
67
        data["track_num"] = entry.entry_num
68
69
        content_class = ResourceUtil.get_content_class(entry.get_content_type())
        data["track_type"] = content_class.numeric   
70
        data["playlist_id"] = entry.playlist.playlist_id
71
        data["schedule_id"] = entry.playlist.schedule.schedule_id
72
        data["show_id"] = entry.playlist.schedule.show_id
73
74
75
76
77
        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...")        
78
        url = self.config.get("api_engine_store_playlog")
79
80
81
82
83
84
        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)


85
    def store_clock_info(self, entry):
86
        """
87
        Posts the current and next show information to the Engine API.
88
        """
89
        current_playlist = self.soundsystem.scheduler.get_active_playlist()
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        current_schedule = current_playlist.schedule
        next_schedule = self.soundsystem.scheduler.get_next_schedules(1)
        if next_schedule: next_schedule = next_schedule[0]

        data = dict()
        data["engine_source"] = self.config.get("api_engine_number")

        if current_playlist:
            data["current_playlist"] = dict()
            data["current_playlist"]["playlist_id"] = current_playlist.playlist_id
            data["current_playlist"]["entries"] = []
            for e in current_playlist.entries:
                entry = dict()
                entry["track_start"] = e.entry_start
104
105
106
107
                if e.meta_data:                
                    entry["track_artist"] = e.meta_data.artist
                    entry["track_album"] = e.meta_data.album                
                    entry["track_title"] = e.meta_data.title                
108
109
                entry["track_num"] = e.entry_num
                entry["track_duration"] = e.duration
110
111
                content_class = ResourceUtil.get_content_class(e.get_content_type)
                entry["track_type"] = content_class.numeric
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
                entry = SU.clean_dictionary(entry)
                data["current_playlist"]["entries"].append(entry)          

        if current_schedule:
            cs = dict()
            cs["schedule_id"] = current_schedule.schedule_id
            cs["schedule_start"] = current_schedule.schedule_start
            cs["schedule_end"] = current_schedule.schedule_end
            cs["show_id"] = current_schedule.show_id
            cs["show_name"] = current_schedule.show_name
            cs["playlist_id"] = current_schedule.playlist_id
            cs["fallback_type"] = current_schedule.fallback_state.id
            cs = SU.clean_dictionary(cs)
            data["current_schedule"] = cs

        if next_schedule:
            ns = dict()
            ns["schedule_id"] = next_schedule.schedule_id
            ns["schedule_start"] = next_schedule.schedule_start
            ns["schedule_end"] = next_schedule.schedule_end
            ns["show_id"] = next_schedule.show_id
            ns["show_name"] = next_schedule.show_name
            ns["playlist_id"] = next_schedule.playlist_id
            ns["fallback_type"] = next_schedule.fallback_state.id
            ns = SU.clean_dictionary(ns)
            data["next_schedule"] = ns


        data = SU.clean_dictionary(data)

        self.logger.info("Posting clock info update to Engine API...")        
        url = self.config.get("api_engine_store_clock")
        headers = {'content-type': 'application/json'}
        body = json.dumps(data, indent=4, sort_keys=True, default=str)
        response = requests.put(url, data=body, headers=headers)
        self.logger.info("Engine API response: %s" % response.status_code)