From d2521d9cb002fa3537d4398bd4e225ed81994274 Mon Sep 17 00:00:00 2001 From: Justin Emter Date: Sun, 16 Jul 2017 21:16:44 -0700 Subject: [PATCH] Reworked the episode gap logic as it seemed to be flakey. Seems to be fully fixed. --- pseudo_commercial_injection.py | 131 ++++++++++++++++++++++++++++ pseudo_generate_daily_scheduledb.py | 57 ++++-------- schedule.sh | 3 + 3 files changed, 153 insertions(+), 38 deletions(-) create mode 100644 pseudo_commercial_injection.py diff --git a/pseudo_commercial_injection.py b/pseudo_commercial_injection.py new file mode 100644 index 0000000..54d3432 --- /dev/null +++ b/pseudo_commercial_injection.py @@ -0,0 +1,131 @@ +#!/usr/bin/python + +from plexapi.server import PlexServer +from datetime import datetime +import sqlite3 + +import logging +import logging.handlers + +from pseudo_config import * + +plex = PlexServer(baseurl, token) + +conn = sqlite3.connect('pseudo-tv.db', timeout=10) +c = conn.cursor() + +my_logger = logging.getLogger('MyLogger') +my_logger.setLevel(logging.DEBUG) + +handler = logging.handlers.SysLogHandler(address = '/dev/log') + +my_logger.addHandler(handler) + + +def get_all_commercials_in_order_from_shortest_to_longest(): + + ascendingCommercialsSQL = "SELECT * FROM commercials ORDER BY duration ASC" + + c.execute(ascendingCommercialsSQL) + + ascendingCommercialsList = list(c.fetchall()) + + return ascendingCommercialsList + + +def get_random_commercial(): + + ''' + * + * Getting a random commercial that will be the commercial we use to find the best fit for the remaining gap. + * + ''' + randomCommercialSQL = "SELECT * FROM commercials WHERE id IN (SELECT id FROM commercials ORDER BY RANDOM() LIMIT 1)" + + c.execute(randomCommercialSQL) + + randomCommercial = list(c.fetchone()) + + print("Random commercial " + str(randomCommercial)) + + return randomCommercial + +def get_commercials_to_fill_up_gap(gapToFill): + + print("Looping through commercials to find good fits for the gap") + + randomCommercial = get_random_commercial() + + randomCommercialDuration = randomCommercial[4] + + ''' + * + * Get all commercials ordered from shortest to longest to use second. + * + ''' + ascendingCommercialsList = get_all_commercials_in_order_from_shortest_to_longest() + + firstAscendingCommercialsList = ascendingCommercialsList[0] + + print("gapToFill - randomCommercialDuration") + print(str(gapToFill - randomCommercialDuration)) + print(str(gapToFill - randomCommercialDuration)) + + ''' + * + * If gapToFill is shorter than the shortest commercial then return empty list + * + ''' + + if gapToFill < firstAscendingCommercialsList[4]: + + print("firstAscendingCommercialsList[4] < gapToFill:") + print(firstAscendingCommercialsList[4]) + + return [-1, -1] + + # print(ascendingCommercialsList) + + if gapToFill - randomCommercialDuration < 0: + + print("the random commercial is longer than the gap") + + if (gapToFill - randomCommercialDuration) > ascendingCommercialsList[0][4]: + + print(ascendingCommercialsList[0][4]) + + print("Gap to fill minus the random commercial is still more than at least the shortest commercial in the library.") + + usableCommercialist = [] + + usableCommercial = None + + for row in ascendingCommercialsList: + + # print(gapToFill - randomCommercialDuration) + + if (gapToFill - randomCommercialDuration) <= row[4]: + + # usableCommercialist.append(row) + + # usableCommercial = row + + + # print("assigning usable commercial to var") + print(row) + + + + else: + + pass + + else: + + print("Random commercial duration: "+str(randomCommercialDuration)) + print("Gap to fill duration: "+str(gapToFill)) + print("gapToFill - randomCommercialDuration: "+str(gapToFill - randomCommercialDuration)) + print("Gap to fill minus random commercial is not more than the shortest commercial") + + +get_commercials_to_fill_up_gap(20000) \ No newline at end of file diff --git a/pseudo_generate_daily_scheduledb.py b/pseudo_generate_daily_scheduledb.py index 99280bd..601a4f2 100644 --- a/pseudo_generate_daily_scheduledb.py +++ b/pseudo_generate_daily_scheduledb.py @@ -160,8 +160,6 @@ def add_daily_schedule_to_db(mediaID, title, episodeNumber, seasonNumber, showTi * ''' def time_diff(time1,time2): - - ''' * * Getting the offest by comparing both times from the unix epoch time and getting the difference. @@ -178,10 +176,6 @@ def time_diff(time1,time2): return int(tdelta/60) -def adjust_start_time_if_duration_is_too_short(duration): - - print("") - ''' * * Passing in the endtime from the prev episode and desired start time of this episode, calculate the best start time @@ -409,12 +403,6 @@ def generate_daily_schedule(): * ''' update_shows_table_with_last_episode(row[3], first_episode_title) - ''' - * - * TODO: generate a reasonable startTime based on previous episode duration - * - ''' - endTime = get_end_time_from_duration(row[5], first_episode[4]); newStartTime = row[5] @@ -422,6 +410,13 @@ def generate_daily_schedule(): newStartTime = calculate_start_time_offset_from_prev_episode_endtime(prevEpisodeEndTime, row[8], first_episode[4], prevEpDuration) + ''' + * + * Generate a new end time from calculated new start time + * + ''' + endTime = get_end_time_from_duration(newStartTime, first_episode[4]); + print("prevEpisodeEndTime: " + str(prevEpisodeEndTime)); startTimeUnix = datetime.datetime.strptime(newStartTime, '%I:%M %p') @@ -469,12 +464,6 @@ def generate_daily_schedule(): print(next_episode[3]) update_shows_table_with_last_episode(row[3], next_episode[3]) - ''' - * - * TODO: generate a reasonable startTime based on previous episode duration - * - ''' - endTime = get_end_time_from_duration(row[5], next_episode[4]); print("End time: " + str(endTime)); @@ -490,11 +479,12 @@ def generate_daily_schedule(): newStartTime = calculate_start_time_offset_from_prev_episode_endtime(prevEpisodeEndTime, row[8], next_episode[4], prevEpDuration) - else: - - prevEpisodeEndTime = endTime - - prevEpDuration = next_episode[4] + ''' + * + * Generate a new end time from calculated new start time + * + ''' + endTime = get_end_time_from_duration(newStartTime, next_episode[4]); print("prevEpisodeEndTime: " + str(prevEpisodeEndTime)); @@ -531,16 +521,6 @@ def generate_daily_schedule(): print(first_episode_title) update_shows_table_with_last_episode(row[3], first_episode_title) - ''' - * - * TODO: generate a reasonable startTime based on previous episode duration - * - ''' - #print(row[5]) - #print(first_episode[4]) - endTime = get_end_time_from_duration(row[5], first_episode[4]); - - print("End time: " + str(endTime)); newStartTime = row[5] @@ -548,11 +528,12 @@ def generate_daily_schedule(): newStartTime = calculate_start_time_offset_from_prev_episode_endtime(prevEpisodeEndTime, row[8], first_episode[4], prevEpDuration) - else: - - prevEpisodeEndTime = endTime - - prevEpDuration = next_episode[4] + ''' + * + * Generate a new end time from calculated new start time + * + ''' + endTime = get_end_time_from_duration(newStartTime, first_episode[4]); print("prevEpisodeEndTime: " + str(prevEpisodeEndTime)); diff --git a/schedule.sh b/schedule.sh index ae10c3f..95f6df3 100644 --- a/schedule.sh +++ b/schedule.sh @@ -40,3 +40,6 @@ python pseudo_channel.py -a "shows" -n "daria" -t "7:30 PM" -d "weekdays" python pseudo_channel.py -a "shows" -n "Futurama" -t "8:00 PM" -d "weekdays" python pseudo_channel.py -a "shows" -n "Futurama" -t "8:30 PM" -d "weekdays" + +python pseudo_channel.py -a "shows" -n "Saved by the Bell" -t "9:00 PM" -d "weekdays" +python pseudo_channel.py -a "shows" -n "Saved by the Bell" -t "9:30 PM" -d "weekdays"