-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathradio_program.py
134 lines (114 loc) · 5.84 KB
/
radio_program.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
import json
from datetime import datetime, timedelta
import dateutil.tz
from apscheduler.scheduler import Scheduler
from advertisement_action import AdvertisementAction
from community_action import CommunityAction
from media_action import MediaAction
from news_action import NewsAction
from outcall_action import OutcallAction
from podcast_action import PodcastAction
from rootio_mailer.rootio_mail_message import RootIOMailMessage
class RadioProgram:
def __init__(self, db, program, radio_station):
self.__program_actions = []
self.__status = True
self.id = program.id
self.db = db
self.name = program.id
self.scheduled_program = program
self.radio_station = radio_station
self.__scheduler = Scheduler()
self.__running_action = None
return
def start(self):
self.__rootio_mail_message = RootIOMailMessage()
self.__load_program_actions()
self.__run_program_action() # will call the next one when done
return
'''
Load the definition of components of the program from a JSON definition
'''
def __load_program_actions(self):
data = json.loads(self.scheduled_program.program.structure)
for action in data:
print action
if action['type'] == "Advertisements":
self.__program_actions.insert(0, AdvertisementAction(action["track_id"], action["start_time"],
action["duration"], self))
if action['type'] == "Media":
self.__program_actions.insert(0,
MediaAction(action["track_id"], action["start_time"], action["duration"],
self))
if action['type'] == "Community":
self.__program_actions.insert(0, CommunityAction(action["category_id"], action["start_time"],
action["duration"], self))
if action['type'] == "Podcast":
self.__program_actions.insert(0, PodcastAction(action["track_id"], action["start_time"],
action["duration"], self))
if action['type'] == "Music":
self.radio_station.logger.info("Music program scheduled to start at {0} for a duration {1}".format(action["start_time"], action["duration"]))
if action['type'] == "News":
self.__program_actions.insert(0,
NewsAction(action["track_id"], action["start_time"], action["duration"],
self))
print action
if action['type'] == "Outcall":
self.__program_actions.insert(0,
OutcallAction(action['host_id'], action["start_time"], action['duration'],
self))
return
'''
Schedule the actions of a particular program for playback within the program
'''
def __schedule_program_actions(self):
for program_action in self.__program_actions:
self.__scheduler.add_date_job(getattr(program_action, 'start'),
self.__get_start_datetime(program_action.start_time).replace(tzinfo=None),
misfire_grace_time=program_action.duration)
def set_running_action(self, running_action):
if self.__running_action is not None:
self.__running_action.stop() # clean up any stuff that is not necessary anymore
self.__running_action = running_action
def log_program_activity(self, program_activity):
self.__rootio_mail_message.append_to_body(
'%s %s' % (datetime.now().strftime('%y-%m-%d %H:%M:%S'), program_activity))
pass
def __run_program_action(self):
self.__program_actions.pop().start()
def notify_program_action_stopped(self, played_successfully, call_info): # the next action might need the call.
self.__status = self.__status and played_successfully
if len(self.__program_actions) == 0: # all program actions have run
if call_info is not None:
self.radio_station.call_handler.hangup(call_info['Channel-Call-UUID'])
self.__log_program_status()
self.__send_program_summary()
else:
self.__run_program_action()
def __send_program_summary(self):
try:
self.__rootio_mail_message.set_subject(
'[%s] %s ' % (self.radio_station.station.name, self.scheduled_program.program.name))
self.__rootio_mail_message.set_from('RootIO') # This will come from DB in future
users = self.__get_network_users()
for user in users:
self.__rootio_mail_message.add_to_address(user.email)
self.__rootio_mail_message.send_message()
except Exception as e:
pass #mail error most probably
def __log_program_status(self):
self.db._model_changes = {}
self.scheduled_program.status = self.__status
self.radio_station.db.add(self.scheduled_program)
self.radio_station.db.commit()
def __get_network_users(self):
station_users = self.radio_station.station.network.networkusers
return station_users
'''
Get the time at which to schedule the program action to start
'''
def __get_start_datetime(self, time_part):
now = datetime.now(dateutil.tz.tzlocal())
t = datetime.strptime(time_part, "%H:%M:%S")
time_delta = timedelta(hours=t.hour, minutes=t.minute, seconds=t.second)
return now + time_delta + timedelta(seconds=2) # 2 second scheduling allowance