trackservice.py 5.83 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
25
26
import requests

from modules.base.utils import SimpleUtil as SU


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
    """
    logger = None
    config = None
    soundsystem = None
35
    last_playlist = None
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51


    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)
52
        self.store_clock_info(entry)
53
54
55
56


    def store_trackservice(self, entry):
        """
57
        Posts the given `PlaylistEntry` to the Engine API Playlog.
58
        """
59
        data = dict()
60
61
62
        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
63
64
65
66
67
        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
68
        data["schedule_id"] = entry.playlist.schedule.schedule_id
69
70
71
72
73
        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...")        
74
        url = self.config.get("api_engine_store_playlog")
75
76
77
78
79
80
        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)


81
    def store_clock_info(self, entry):
82
        """
83
        Posts the current and next show information to the Engine API.
84
        """
85
86
87
        current_playlist = entry.playlist
        if current_playlist == self.last_playlist:
            self.logger.info("Playlist didn't change since last update.")
88
        else:
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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
            self.last_playlist = current_playlist            
            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
                    entry["track_artist"] = e.meta_data.artist
                    entry["track_album"] = e.meta_data.album                
                    entry["track_title"] = e.meta_data.title                
                    entry["track_duration"] = e.duration
                    entry["track_type"] = e.get_type().numeric
                    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)