From 920190626a871cb60f23f0c7ce856a39166a738b Mon Sep 17 00:00:00 2001 From: Justin Emter Date: Tue, 25 Jul 2017 22:00:45 -0700 Subject: [PATCH] Adjusted logic to calculate start / stop times for commercials. This should solve the commercials getting cut off. I do hoever see some issues, pushing anyway. --- src/PseudoChannelCommercial.py | 157 ++++++++++++++++++--------------- 1 file changed, 87 insertions(+), 70 deletions(-) diff --git a/src/PseudoChannelCommercial.py b/src/PseudoChannelCommercial.py index e9e2f3c..bd0fb53 100644 --- a/src/PseudoChannelCommercial.py +++ b/src/PseudoChannelCommercial.py @@ -8,130 +8,147 @@ from src import Commercial class PseudoChannelCommercial(): - MIN_DURATION_FOR_COMMERCIAL = 10 #seconds - daily_schedule = [] + MIN_DURATION_FOR_COMMERCIAL = 10 #seconds + daily_schedule = [] - def __init__(self, commercials): + def __init__(self, commercials): - self.commercials = commercials + self.commercials = commercials - def get_commercials_to_inject(self): + def get_commercials_to_inject(self): - self.go() + self.go() - return None + return None - def get_random_commercial(self): + def get_random_commercial(self): - random_commercial = random.choice(self.commercials) + random_commercial = random.choice(self.commercials) - random_commercial_dur_seconds = (int(random_commercial[4])/1000)%60 + random_commercial_dur_seconds = (int(random_commercial[4])/1000)%60 - while random_commercial_dur_seconds < self.MIN_DURATION_FOR_COMMERCIAL: + while random_commercial_dur_seconds < self.MIN_DURATION_FOR_COMMERCIAL: - random_commercial = random.choice(self.commercials) + random_commercial = random.choice(self.commercials) - random_commercial_dur_seconds = (int(random_commercial[4])/1000)%60 + random_commercial_dur_seconds = (int(random_commercial[4])/1000)%60 - return random_commercial + return random_commercial - def go(self): + def go(self): - shuffled_commercial_list = copy.deepcopy(self.commercials) + shuffled_commercial_list = copy.deepcopy(self.commercials) - random.shuffle(self.commercials, random.random) + random.shuffle(self.commercials, random.random) - #print shuffled_commercial_list + #print shuffled_commercial_list - prev_item = None + prev_item = None - for entry in self.daily_schedule: + for entry in self.daily_schedule: - """First Episode""" - if prev_item == None: + """First Episode""" + if prev_item == None: - prev_item = entry + prev_item = entry - else: + else: - prev_item_end_time = datetime.datetime.strptime(prev_item[9], '%Y-%m-%d %H:%M:%S.%f') + prev_item_end_time = datetime.datetime.strptime(prev_item[9], '%Y-%m-%d %H:%M:%S.%f') - curr_item_start_time = datetime.datetime.strptime(entry[8], '%I:%M:%S %p') + curr_item_start_time = datetime.datetime.strptime(entry[8], '%I:%M:%S %p') - time_diff = (curr_item_start_time - prev_item_end_time) + time_diff = (curr_item_start_time - prev_item_end_time) - days, hours, minutes = time_diff.days, time_diff.seconds // 3600, time_diff.seconds // 60 % 60 + days, hours, minutes = time_diff.days, time_diff.seconds // 3600, time_diff.seconds // 60 % 60 - count = 0 + count = 0 - commercial_list = [] + commercial_list = [] - commercial_dur_sum = 0 + commercial_dur_sum = 0 - while int(time_diff.total_seconds()) >= commercial_dur_sum and count < len(self.commercials): + while int(time_diff.total_seconds()) >= commercial_dur_sum and count < len(self.commercials): - random_commercial = self.get_random_commercial() + random_commercial = self.get_random_commercial() - commercial_list.append(random_commercial) + commercial_list.append(random_commercial) - commercial_dur_sum += int(random_commercial[4]) + commercial_dur_sum += int(random_commercial[4]) - print commercial_list + print commercial_list - prev_item = entry + prev_item = entry - def get_commercials_to_place_between_media(self, last_ep, now_ep): + def timedelta_milliseconds(self, td): + return td.days*86400000 + td.seconds*1000 + td.microseconds/1000 - print last_ep.end_time, now_ep.start_time + def get_commercials_to_place_between_media(self, last_ep, now_ep): - prev_item_end_time = datetime.datetime.strptime(last_ep.end_time.strftime('%Y-%m-%d %H:%M:%S.%f'), '%Y-%m-%d %H:%M:%S.%f') + print last_ep.end_time, now_ep.start_time - curr_item_start_time = datetime.datetime.strptime(now_ep.start_time, '%I:%M:%S %p') + prev_item_end_time = datetime.datetime.strptime(last_ep.end_time.strftime('%Y-%m-%d %H:%M:%S.%f'), '%Y-%m-%d %H:%M:%S.%f') - time_diff = (curr_item_start_time - prev_item_end_time) + curr_item_start_time = datetime.datetime.strptime(now_ep.start_time, '%I:%M:%S %p') - days, hours, minutes = time_diff.days, time_diff.seconds // 3600, time_diff.seconds // 60 % 60 + time_diff = (curr_item_start_time - prev_item_end_time) - count = 0 + count = 0 - commercial_list = [] + commercial_list = [] - commercial_dur_sum = 0 + commercial_dur_sum = 0 - while int(time_diff.total_seconds()) >= commercial_dur_sum and (count*10000) < len(self.commercials): + time_diff_milli = self.timedelta_milliseconds(time_diff) - random_commercial = self.get_random_commercial() + last_commercial = None - new_commercial_seconds = (int(random_commercial[4])/1000)%60 + time_watch = prev_item_end_time - commercial_dur_sum += new_commercial_seconds + print "here", time_diff.seconds - new_commercial_start_time = prev_item_end_time + datetime.timedelta(seconds=commercial_dur_sum) + while curr_item_start_time > time_watch and (count) < len(self.commercials): - new_commercial_end_time = new_commercial_start_time + datetime.timedelta(seconds=int(new_commercial_seconds)) + random_commercial = self.get_random_commercial() - formatted_time_for_new_commercial = new_commercial_start_time.strftime('%I:%M:%S %p') + last_commercial = random_commercial - new_commercial = Commercial( - "Commercials", - random_commercial[3], - formatted_time_for_new_commercial, # natural_start_time - new_commercial_end_time, - random_commercial[4], - "everyday", # day_of_week - "true", # is_strict_time - "1", # time_shift - "0", # overlap_max - "", # plex_media_id - ) + #new_commercial_seconds = (int(random_commercial[4])/1000)%60 - if int(time_diff.total_seconds()) < commercial_dur_sum: + new_commercial_milli = int(random_commercial[4]) - break + commercial_dur_sum += new_commercial_milli - commercial_list.append(new_commercial) + new_commercial_start_time = time_watch - #print "here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + new_commercial_end_time = new_commercial_start_time - return commercial_list \ No newline at end of file + new_commercial_end_time += datetime.timedelta(milliseconds=int(new_commercial_milli)) + + formatted_time_for_new_commercial = time_watch.strftime('%I:%M:%S %p') + + new_commercial = Commercial( + "Commercials", + random_commercial[3], + formatted_time_for_new_commercial, # natural_start_time + new_commercial_end_time, + random_commercial[4], + "everyday", # day_of_week + "true", # is_strict_time + "1", # time_shift + "0", # overlap_max + "", # plex_media_id + ) + + time_watch += datetime.timedelta(milliseconds=commercial_dur_sum) + + if time_diff_milli < commercial_dur_sum or new_commercial_end_time > curr_item_start_time: + + break + + commercial_list.append(new_commercial) + + #print "here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + + return commercial_list \ No newline at end of file