trackservice.py 2.89 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
28
29

class TrackserviceHandler():
    """
30
    Sends the trackservice entry to the `engine-api` REST endpoint.
31
32
33
34
    """
    logger = None
    config = None
    soundsystem = None
35
    last_schedule = 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_show_info(entry)
53
54
55
56


    def store_trackservice(self, entry):
        """
57
        Posts the given `PlaylistEntry` to the Engine API Playlog.
58
        """
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        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)


79

80
    def store_show_info(self, entry):
81
        """
82
        Posts the current and next show information to the Engine API.
83
        """
84
85
86
87
88
89
        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