-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.py
315 lines (240 loc) · 8.67 KB
/
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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#program.py - zardządzanie ramówką
from json.decoder import JSONDecodeError
#from emiter_core import cfg
import requests
#import datetime
import time
import json
import logging
class Program:
#deprecated
#margin = 5 # 5 minut
program_table = []
schedule = []
programs = []
#pobiera program z API
def update_from_api(self,url,sufix="default"):
#pobierz listę slotów
try:
rq = requests.get(url+"/timeslots")
except:
logging.error("API connection error")
return False
#jeśli odpowiada JSONem
try:
timeslots = rq.json()
#zapisz kopię do json-ów
# with open(cfg.cfg['path_schedules']+"timeslots_"+sufix,mode='w') as f:
# f.write(rq.text)
# f.close()
except JSONDecodeError:
logging.warning("API response /timeslots decoding error (status %d)"% rq.status_code)
return False
#pobierz listę programów
try:
rq = requests.get(url+"/programs")
except:
logging.error("API connection error")
return False
#jeśli odpowiada JSONem
try:
programs = rq.json()
#zapisz kopię do json-ów
# with open(cfg.cfg['path_schedules']+"programs_"+sufix,mode='w') as f:
# f.write(rq.text)
# f.close()
except JSONDecodeError:
logging.warning("API response /programs decoding error (status %d)"% rq.status_code)
return False
#przerzut do listy jeśli audycja ma być widoczna przez system
for uuid in timeslots:
if timeslots[uuid]["program"]["broadcast_visible"]:
self.schedule.append(timeslots[uuid])
#sortowanie slotów po czasie
self.schedule.sort(key=self.slot_minute_of_week)
#lista wszystkich występujących slugów w timeslotach
slugs = []
for slot in self.schedule:
slug = slot["program"]["slug"]
if slug not in slugs:
slugs.append(slug)
for slug in slugs:
#przeszukaj wśród zaimportowanych programów
found = False
for uuid in programs:
#gdy znaleziono pasujący program
if programs[uuid]["slug"] == slug:
self.programs.append(programs[uuid].copy())
found = True
break
#jeśli nie znaleziono
if not found:
stub_program = {
"slug": slug,
"name": slug,
"rds": "",
"people": []
}
self.programs.append(stub_program)
return True
#funkcja do sortowania timeslotów
def slot_minute_of_week(self,slot):
return self.minute_of_week(slot["weekday"],slot["begin_h"],slot["begin_m"])
#minuta w tygodniu
def minute_of_week(self,wd,h,m):
return (wd-1)*60*24 + h*60 + m
#uwzględnia margines czasu do przodu lub do tyłu
def time_margin(self,weekday,hour,min,margin):
m = min + margin
h = hour
wd = weekday
#carry
if m >= 60:
m -= 60
h += 1
if h >= 24:
h -= 24
wd +=1
if wd > 7:
wd -= 7
#borrow
if m < 0:
m += 60
h -= 1
if h < 0:
h += 24
wd -= 1
if wd < 1:
wd += 7
return wd, h, m
#szukaj rekordu danego dnia tygodnia o danej godzinie
def get_program(self,prev,next,wd,h,m,time_margin=5):
#przesuwanie czasu do przodu/tyłu
if(prev):
weekday, hour, min = self.time_margin(wd,h,m,-time_margin)
elif(next):
weekday, hour, min = self.time_margin(wd,h,m,time_margin)
else:
weekday = wd
hour = h
min = m
#minuta w tygodniu
now_min_of_week = self.minute_of_week(weekday,hour,min)
found = False
target_program = {}
for slot in self.schedule:
#minuta tygodnia, gdy slot się zaczyna i kończy
slot_min_of_week = self.slot_minute_of_week(slot)
end_min_of_week = slot_min_of_week + slot["duration"]
#jeśli audycja przechodzi przez koniec tygodnia (zaczyna się w niedzielę, kończy w poniedziałek)
if end_min_of_week > self.minute_of_week(8,0,0):
end_min_of_week -= self.minute_of_week(8,0,0)
found = now_min_of_week >= slot_min_of_week or now_min_of_week < end_min_of_week
else:
#jeśli nie
#czy czas mieści się w granicach?
found = now_min_of_week >= slot_min_of_week and now_min_of_week < end_min_of_week
#czy znaleziono audycję?
if found:
#wpisz dane
target_program = {
"slug": slot["program"]["slug"],
"weekday":slot["weekday"],
"duration":slot["duration"],
"start_h":slot["begin_h"],
"start_m":slot["begin_m"],
"replay":slot["replay"]
}
#wydź z pętli
break
#dodaj flagę found i zwróć program
target_program["found"] = found
return target_program
#wyszukuje n audycje/powtórek od danego punktu
#jeśli num = 0, wyszukuje wszystkie pozostałe danego dnia
def list_programs(self,wd,h,m,num):
#minuta w tygodniu
now_min_of_week = self.minute_of_week(wd,h,m)
i = 0
last_program_end = 0
while True:
slot = self.schedule[i]
#minuta tygodnia, gdy slot się zaczyna i kończy
slot_min_of_week = self.slot_minute_of_week(slot)
#czy audycja zaczyna się po timestampie
if slot_min_of_week > now_min_of_week:
#wyjdź
break
else:
#nastepny
i+=1
auds = []
#dzień tygodnia
dow = slot["weekday"]
while True:
slot = self.schedule[i]
if num == 0 and slot["weekday"] != dow:
break
if num > 0 and len(auds) >= num:
break
#dodaj audycję
auds.append(slot)
i+=1
#gdy poza ostatnim rekordem?
if i >= len(self.schedule):
#wróć do początku
i = 0
return auds
def get_program_with_split(self,next,wd,h,m,time_margin=5):
#pobierz audycję przed/po
aud = self.get_program(not next,next,wd,h,m,time_margin=time_margin)
#pobierz audycję teraz
now = self.get_program(False,False,wd,h,m)
#czy przejście między audycjami?
if aud["found"] and now["found"]:
#Czy audycja się zmieniła?
changed = (aud['slug'] != now['slug'])
else:
#przejście z/do playlisty
changed = (aud["found"] != now["found"])
#dopisz do listy informację o zmianie audycji
aud['changed'] = changed
#zwróć
return aud
#zwróć wszystkie sloty
def list_all_slots(self):
return self.list_programs(1,0,0,len(self.schedule))
#zwróć wszystkie audycje
def list_all_programs(self):
return self.programs
#lista slugów
def list_all_slugs(self):
slugs = []
for program in self.programs:
slugs.append(program["slug"])
return slugs
#bindy dla czasu teraz
def get_program_now(self,prev,next,time_margin=5):
t = time.localtime()
wd = t.tm_wday+1
h = t.tm_hour
m = t.tm_min
return self.get_program(prev,next,wd,h,m,time_margin=time_margin)
def get_program_with_split_now(self,next,time_margin=5):
t = time.localtime()
wd = t.tm_wday+1
h = t.tm_hour
m = t.tm_min
return self.get_program_with_split(next,wd,h,m,time_margin=time_margin)
def list_programs_now(self,num):
t = time.localtime()
wd = t.tm_wday+1
h = t.tm_hour
m = t.tm_min
return self.list_programs(wd,h,m,num)
def list_programs_today(self):
t = time.localtime()
wd = t.tm_wday+1
h = t.tm_hour
m = t.tm_min
return self.list_programs(wd,h,m,0)