forked from shane-mason/FieldStation42
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguide_builder.py
169 lines (133 loc) · 5.41 KB
/
guide_builder.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import sys
import os
sys.path.append(os.getcwd())
from fs42.timings import OPERATING_HOURS, DAYS
from fs42.show_block import ShowBlock, ClipBlock, MovieBlocks, ContinueBlock
from fs42.station_manager import StationManager
import json
import pickle
import datetime
import re
import math
def normalize_video_title(title):
if "_V1" in title:
(title, episode_id) = title.split("_V1")
spaced = re.sub("[^a-zA-Z0-9]", " ", title)
titled = spaced.title()
return titled
class PreviewBlock:
def __init__(self, title, width=1, continuation=False):
self.title = title
self.continuation = continuation
self.width = width
self.started_earlier = False
self.ends_later = False
self.is_back_half = False
def __repr__(self):
return f"{self.title}:{self.width}|{self.continuation}"
def toJSON(self):
return json.dumps(self, default=lambda o: o.__dict__)
class ScheduleQuery:
def query_slot(schedule, day, hour):
blocks = []
for i in [0,1]:
slot_key = hour + i
if slot_key >= 24:
slot_key = 0
show = schedule[day][str(slot_key)]
if isinstance(show, ShowBlock):
title = normalize_video_title(show.front.title)
if show.back:
blocks.append(PreviewBlock(title))
back_title = normalize_video_title(show.back.title)
back = PreviewBlock(back_title)
back.is_back_half = True
blocks.append(back)
else:
#then it gets the whole hour
blocks.append(PreviewBlock(title, 2))
elif isinstance(show, ClipBlock):
title = normalize_video_title(show.title)
blocks.append(PreviewBlock(title,2))
elif isinstance(show, MovieBlocks):
title = normalize_video_title(show.title)
blocks.append(PreviewBlock(title, 4))
break;
elif isinstance(show, ContinueBlock):
title = normalize_video_title(show.title)
blocks.append(PreviewBlock(title, 2, True))
return blocks
class GuideBuilder:
def __init__(self, num_blocks=3, template_dir="fs42/guide_render/templates/", static_dir="fs42/guide_render/static/"):
# ordered array of {"conf": station_config, "schedule": schedule}
self.station_schedules = []
self.num_blocks = num_blocks
self.template_dir = template_dir
self.static_dir = static_dir
def build_view(self):
slots = []
view = {'rows': [], 'meta': []}
now = datetime.datetime.now()
week_day = DAYS[now.weekday()]
hour = now.hour
past_half = now.minute>30
#stations are a row
for station in self.station_schedules:
entries = ScheduleQuery.query_slot(station['schedule'], week_day, hour)
#is already past the half hour, remove one block from front
if past_half:
entries[0].width -= 1
#only back halfs would not have started started_earlier
if entries[0].width > 0:
entries[0].started_earlier
#now trim the entries to only contain 3 blocks
count = 0
filtered = []
for entry in entries:
if count + entry.width <= self.num_blocks:
count += entry.width
filtered.append(entry)
elif count < self.num_blocks:
entry.ends_later = True
entry.width = self.num_blocks-count
filtered.append(entry)
count = self.num_blocks
if count >= self.num_blocks:
break
view['rows'].append(filtered)
network_name = station['conf']['network_name']
channel_number = station['conf']['channel_number']
view['meta'].append({"network_name": network_name, "channel_number": channel_number})
timings = []
hour_one = hour
hour_two = hour+1
if hour_two >= 24:
hour_two = 0
#TODO: this isn't an extendable approach - only supports 3 time blocks
if past_half:
timings.append(f"{hour_one}:30")
timings.append(f"{hour_two}:00")
timings.append(f"{hour_two}:30")
else:
timings.append(f"{hour_one}:00")
timings.append(f"{hour_two}:30")
timings.append(f"{hour_two}:00")
formatted_timings = []
#TODO: Add configuration option for 24 vs 12 hour times
for timing in timings:
formatted = datetime.datetime.strptime(timing, "%H:%M").strftime("%I:%M %p")
formatted_timings.append(formatted)
view["timings"] = formatted_timings
return view
def load_schedules(self, station_configs):
for station_config in station_configs:
if station_config['network_type'] != "standard":
pass
else:
with open(station_config['schedule_path'], "rb") as f:
full_schedule = pickle.load(f)
self.station_schedules.append({"conf": station_config, "schedule": full_schedule})
if __name__ == "__main__":
gb = GuideBuilder()
gb.load_schedules(StationManager().stations)
gb.render()