From e0d78ad4ba34639914fc332a97edb405d8befd07 Mon Sep 17 00:00:00 2001 From: Justin Emter Date: Sun, 16 Jul 2017 00:28:46 -0700 Subject: [PATCH] Added a interval config var to set repositioning of shows to start at say, 15, 30, 45 minutes past the hour. --- .gitignore | 3 +- pseudo_channel.py | 7 +++ pseudo_generate_daily_scheduledb.py | 70 ++++++++++++++++++++++++----- schedule.sh | 5 +++ 4 files changed, 73 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 06fd472..3cd4342 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ pseudo_config.pyc pseudo_config.* -pseudo_tv.db \ No newline at end of file +pseudo_tv.db +pseudo-tv.db diff --git a/pseudo_channel.py b/pseudo_channel.py index 757fdf8..f123020 100644 --- a/pseudo_channel.py +++ b/pseudo_channel.py @@ -15,10 +15,17 @@ plex = PlexServer(baseurl, token) conn = sqlite3.connect('pseudo-tv.db', timeout=10) c = conn.cursor() +def create_table(): + + c.execute('DROP TABLE IF EXISTS schedule') + + c.execute('CREATE TABLE IF NOT EXISTS schedule(id INTEGER PRIMARY KEY AUTOINCREMENT, unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER, startTime INTEGER, endTime INTEGER, dayOfWeek TEXT, startTimeUnix INTEGER)') + def add_schedule_to_db(mediaID, title, duration, startTime, endTime, dayOfWeek): unix = int(time.time()) startTimeUnix = str(datetime.datetime.strptime(startTime, '%I:%M %p')) try: + c.execute("INSERT OR REPLACE INTO schedule (unix, mediaID, title, duration, startTime, endTime, dayOfWeek, startTimeUnix) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (unix, mediaID, title, duration, startTime, endTime, dayOfWeek, startTimeUnix)) conn.commit() c.close() diff --git a/pseudo_generate_daily_scheduledb.py b/pseudo_generate_daily_scheduledb.py index 316ad44..b843e9d 100644 --- a/pseudo_generate_daily_scheduledb.py +++ b/pseudo_generate_daily_scheduledb.py @@ -20,6 +20,9 @@ import string import argparse import datetime import calendar +import itertools + +from pseudo_config import * conn = sqlite3.connect('pseudo-tv.db') @@ -105,29 +108,72 @@ def calculate_start_time_offset_from_prev_episode_endtime(prevEndTime, intendedS * If time difference is negative, then we know there is overlap * ''' - if timeDiff <= 0: + if timeDiff < 0: print("There is overlap ") + ''' + * + * If there is an overlap, then the overlapGap var in config will determine the next increment. If it is set to "15", then the show will will bump up to the next 15 minute interval past the hour. + * + ''' + timeset=[datetime.time(h,m).strftime("%H:%M") for h,m in itertools.product(xrange(0,24),xrange(0,60,int(overlapGap)))] + + print(timeset) + + timeSetToUse = None + + for time in timeset: + + #print(time) + theTimeSetInterval = datetime.datetime.strptime(time, '%H:%M') + + # print(theTimeSetInterval) + + # print(prevEndTime) + + if theTimeSetInterval >= prevEndTime: + + print("Setting new time by interval... " + time) + print("made it!") + + newStartTime = theTimeSetInterval + + break + + #newStartTime = newTimeObj + datetime.timedelta(minutes=abs(timeDiff + overlapGap)) + + elif (timeDiff >= 0) and (timeGap != -1): ''' * - * If timeDiff is not more than 15 minutes, or 30 minutes just adjust by 15 or 30. If more than adjust by the exact difference + * If there this value is configured, then the timeGap var in config will determine the next increment. If it is set to "15", then the show will will bump up to the next 15 minute interval past the hour. * ''' - newStartTime = newTimeObj + datetime.timedelta(minutes=abs(timeDiff + 5)) + timeset=[datetime.time(h,m).strftime("%H:%M") for h,m in itertools.product(xrange(0,24),xrange(0,60,int(timeGap)))] + + # print(timeset) - elif timeDiff <= 5: + timeSetToUse = None - print("timeDiff is less than or equal to 5 minutes") + for time in timeset: - elif timeDiff > 5: + #print(time) + theTimeSetInterval = datetime.datetime.strptime(time, '%H:%M') - print("Time gap is more than ten, let's start next media earlier than inended") + # print(theTimeSetInterval) - # if prevEpDuration < 10 + # print(prevEndTime) + + if theTimeSetInterval >= prevEndTime: + + print("Setting new time by interval... " + time) + print("made it!") + + newStartTime = theTimeSetInterval + + break - newStartTime = newTimeObj - datetime.timedelta(minutes=timeDiff-5) else: @@ -339,7 +385,7 @@ def generate_daily_schedule(): prevEpisodeEndTime = endTime - prevEpDuration = first_episode[4] + prevEpDuration = next_episode[4] print("prevEpisodeEndTime: " + str(prevEpisodeEndTime)); @@ -356,6 +402,8 @@ def generate_daily_schedule(): print("Not grabbing next episode for some reason") except Exception as e: + + #raise e ''' * * Let's assume that this error is always because we hit the end of the series and start over... @@ -402,7 +450,7 @@ def generate_daily_schedule(): prevEpDuration = next_episode[4] - # raise e + generate_daily_schedule() diff --git a/schedule.sh b/schedule.sh index 7ba283e..8595bb8 100644 --- a/schedule.sh +++ b/schedule.sh @@ -5,6 +5,11 @@ python pseudo_channel.py -a "shows" -n "looney tunes" -t "6:30 AM" -d "weekdays" python pseudo_channel.py -a "shows" -n "looney tunes" -t "7:00 AM" -d "weekdays" python pseudo_channel.py -a "shows" -n "looney tunes" -t "7:30 AM" -d "weekdays" +# python pseudo_channel.py -a "shows" -n "daria" -t "6:00 AM" -d "weekdays" +# python pseudo_channel.py -a "shows" -n "daria" -t "6:30 AM" -d "weekdays" +# python pseudo_channel.py -a "shows" -n "looney tunes" -t "7:00 AM" -d "weekdays" +# python pseudo_channel.py -a "shows" -n "looney tunes" -t "7:30 AM" -d "weekdays" + python pseudo_channel.py -a "shows" -n "Garfield & Friends" -t "8:00 AM" -d "weekdays" python pseudo_channel.py -a "shows" -n "Garfield & Friends" -t "8:30 AM" -d "weekdays"