From ee863eb888e60d8959bf3b4d91531326f7c5a483 Mon Sep 17 00:00:00 2001 From: Justin Emter Date: Thu, 10 Aug 2017 10:02:07 -0700 Subject: [PATCH] Potential fix for the daily schedule update / interruption to currently playing media - issue. --- PseudoChannel.py | 112 ++++++++++++++++++++++++++++++++++++++++++++++- pseudo_config.py | 11 ++++- 2 files changed, 120 insertions(+), 3 deletions(-) diff --git a/PseudoChannel.py b/PseudoChannel.py index 9f37417..1e6a811 100644 --- a/PseudoChannel.py +++ b/PseudoChannel.py @@ -55,6 +55,8 @@ class PseudoChannel(): CONTROLLER_SERVER_PATH = config.controllerServerPath CONTROLLER_SERVER_PORT = config.controllerServerPort + USE_OVERRIDE_CACHE = config.useDailyOverlapCache + DEBUG = config.debug_mode def __init__(self): @@ -960,6 +962,52 @@ class PseudoChannel(): self.db.import_shows_table_by_row(row[2], row[3], row[4], row[5], row[6], row[7]) + def get_daily_schedule_cache_as_json(self): + + data = [] + + try: + with open('../.pseudo-cache/daily-schedule.json') as data_file: + data = json.load(data_file) + + #pprint(data) + + except IOError: + + print ("----- Having issues opening the pseudo-cache file.") + + return data + + def save_daily_schedule_as_json(self): + + daily_schedule_table = self.db.get_daily_schedule() + + json_string = json.dumps(daily_schedule_table) + + print "+++++ Saving Daily Schedule Cache " + + self.save_file(json_string, 'daily-schedule.json', '../.pseudo-cache/') + + def save_file(self, data, filename, path="./"): + + fileName = filename + + writepath = path + + if not os.path.exists(writepath): + + os.makedirs(writepath) + + if os.path.exists(writepath+fileName): + + os.remove(writepath+fileName) + + mode = 'a' if os.path.exists(writepath) else 'w' + + with open(writepath+fileName, mode) as f: + + f.write(data) + def exit_app(self): print " - Exiting Pseudo TV & cleaning up." @@ -1252,11 +1300,70 @@ if __name__ == '__main__': print "##### Generating Memory Schedule." + now = datetime.datetime.now() + + now = now.replace(year=1900, month=1, day=1) + + pseudo_cache = pseudo_channel.get_daily_schedule_cache_as_json() + + prev_end_time_to_watch_for = None + + if pseudo_channel.USE_OVERRIDE_CACHE: + + for cached_item in pseudo_cache: + + prev_start_time = datetime.datetime.strptime(cached_item[8], "%I:%M:%S %p") + + try: + + prev_end_time = datetime.datetime.strptime(cached_item[9], '%Y-%m-%d %H:%M:%S.%f') + + except ValueError: + + prev_end_time = datetime.datetime.strptime(cached_item[9], '%Y-%m-%d %H:%M:%S') + + """If update time is in between the prev media start / stop then there is overlap""" + if prev_start_time < now and prev_end_time > now: + + try: + + print "+++++ It looks like there is update schedule overlap", cached_item[3] + + except: + + pass + + prev_end_time_to_watch_for = prev_end_time + for item in schedulelist: trans_time = datetime.datetime.strptime(item[8], "%I:%M:%S %p").strftime("%H:%M") - schedule.every().day.at(trans_time).do(job_that_executes_once, item, schedulelist).tag('daily-tasks') + new_start_time = datetime.datetime.strptime(item[8], "%I:%M:%S %p") + + if prev_end_time_to_watch_for == None: + + schedule.every().day.at(trans_time).do(job_that_executes_once, item, schedulelist).tag('daily-tasks') + + else: + + """If prev end time is more then the start time of this media, skip it""" + if prev_end_time_to_watch_for > new_start_time: + + try: + + print "Skipping scheduling item do to cached overlap.", item[3] + + except: + + pass + + continue + + else: + + schedule.every().day.at(trans_time).do(job_that_executes_once, item, schedulelist).tag('daily-tasks') + print "+++++ Done." @@ -1281,6 +1388,9 @@ if __name__ == '__main__': def go_generate_daily_sched(): + """Saving current daily schedule as cached .json""" + pseudo_channel.save_daily_schedule_as_json() + schedule.clear('daily-tasks') pseudo_channel.generate_daily_schedule() generate_memory_schedule(pseudo_channel.db.get_daily_schedule()) diff --git a/pseudo_config.py b/pseudo_config.py index 699a3f4..7f625a3 100644 --- a/pseudo_config.py +++ b/pseudo_config.py @@ -59,8 +59,6 @@ plexLibraries = { "Commercials" : ["Commercials"], } -useGoogleCalendar = False - useCommercialInjection = True # How many seconds to pad commercials between each other / other media @@ -76,6 +74,15 @@ You can also leave the below controllerServerPath empty if you'd like to run you controllerServerPath = "http://192.168.1.28" controllerServerPort = "8000" +""" +When the schedule updates every 24 hours, it's possible that it will interrupt any shows / movies that were +playing from the previous day. To fix this, the app saves a "cached" schedule from the previous day to +override any media that is trying to play while the previous day is finishing. +""" +useDailyOverlapCache = True + dailyUpdateTime = "12:00 AM" debug_mode = True + +useGoogleCalendar = False \ No newline at end of file