optimze the fallback_search algorithm
This commit is contained in:
parent
af5a7a4b17
commit
7e508f7596
1 changed files with 10 additions and 4 deletions
|
@ -117,16 +117,22 @@ def remove_channel_from_database(database, scheduler, stream_id, stream_name, st
|
||||||
def fallback_search(database):
|
def fallback_search(database):
|
||||||
logger_job.warning('Searching for a fallback job.')
|
logger_job.warning('Searching for a fallback job.')
|
||||||
current_hour = datetime.now().hour
|
current_hour = datetime.now().hour
|
||||||
hour_set = []
|
scheduled_hours = []
|
||||||
for key, value in database.items():
|
for key, value in database.items():
|
||||||
if value['start_at'] == "now" or value['start_at'] == "never":
|
if value['start_at'] == "now" or value['start_at'] == "never":
|
||||||
# do not use non-time scheduled streams as fallbacks
|
# do not use non-time scheduled streams as fallbacks
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
hour_set.append(value['start_at'])
|
# append the hours in the working set
|
||||||
closest_hour = min(hour_set, key=lambda item: abs(int(item) - current_hour))
|
scheduled_hours.append(value['start_at'])
|
||||||
|
|
||||||
|
# convert the scheduled hours to a circular list
|
||||||
|
scheduled_hours = scheduled_hours + [h + 24 for h in scheduled_hours]
|
||||||
|
|
||||||
|
# find the closest scheduled hour
|
||||||
|
closest_hour = min(scheduled_hours, key=lambda x: abs(x - current_hour))
|
||||||
for key, value in database.items():
|
for key, value in database.items():
|
||||||
if value['start_at'] == str(closest_hour):
|
if value['start_at'] == str(closest_hour % 24):
|
||||||
fallback = { "stream_id": key,
|
fallback = { "stream_id": key,
|
||||||
"stream_name": value['name'],
|
"stream_name": value['name'],
|
||||||
"stream_hls_url": value['src']
|
"stream_hls_url": value['src']
|
||||||
|
|
Loading…
Reference in a new issue