Added endTime to scheduled content

This commit is contained in:
Justin Emter
2017-07-14 10:14:56 -07:00
parent ac3569f979
commit 8d78bb5fd2
2 changed files with 83 additions and 19 deletions

Binary file not shown.

View File

@@ -1,72 +1,122 @@
#!/usr/bin/python
'''
*
* This script is for generating a daily schedule using a crontab as the clock.
* It queries the "schedule" table and the "shows" table to determine the next episode in a TV Series.
* A time will be generated based on the scheduled time and the duration of the previous episode (or movie).
* After the data has been generated, an entry will be made in the "daily_schedule" table.
* This should be run every day at midnight:
*
* crontab -e
* 0 0 * * * python /home/justin/this-repo-folder/pseudo_generate_daily_scheduledb.py
*
'''
import sqlite3
import time
import os, sys
#import Image
import string
import argparse
import datetime
conn = sqlite3.connect('pseudo-tv.db')
c = conn.cursor()
def create_table():
c.execute('DROP TABLE IF EXISTS daily_schedule')
c.execute('CREATE TABLE daily_schedule(id INTEGER PRIMARY KEY AUTOINCREMENT, unix INTEGER, mediaID INTEGER, title TEXT, episodeNumber INTEGER, seasonNumber INTEGER, showTitle TEXT, duration INTEGER, startTime INTEGER, endTime INTEGER, dayOfWeek TEXT)')
def add_daily_schedule_to_db(mediaID, title, episodeNumber, seasonNumber, showTitle, duration, startTime, endTime, dayOfWeek):
unix = int(time.time())
try:
c.execute("INSERT OR REPLACE INTO daily_schedule (unix, mediaID, title, episodeNumber, seasonNumber, showTitle, duration, startTime, endTime, dayOfWeek) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (unix, mediaID, title, episodeNumber, seasonNumber, showTitle, duration, startTime, endTime, dayOfWeek))
conn.commit()
# Catch the exception
except Exception as e:
# Roll back any change if something goes wrong
conn.rollback()
raise e
'''
*
* Using datetime to figure out when the media item will end based on the scheduled start time or the offset
* generated by the previous media item.
* Returns time
*
'''
def get_end_time_from_duration(startTime, duration):
#time = datetime.datetime.strptime(startTime, '%I:%M %p')
time = datetime.datetime.strptime(startTime, '%I:%M %p')
show_time_plus_duration = time + datetime.timedelta(milliseconds=duration)
#print(show_time_plus_duration.minute)
return show_time_plus_duration
#sql1 = "SELECT * FROM "+media+" WHERE (episodeNumber = 10 AND showTitle LIKE ?) COLLATE NOCASE"
#def get_next_episode_title():
def update_shows_table_with_last_episode(showTitle, lastEpisodeTitle):
sql1 = "UPDATE shows SET lastEpisodeTitle = ? WHERE title = ?"
c.execute(sql1, (lastEpisodeTitle, showTitle, ))
conn.commit()
def get_first_episode(tvshow):
#print("Getting first episode of "+tvshow)
sql1 = "SELECT id, unix, mediaID, title, duration, MIN(episodeNumber), MIN(seasonNumber), showTitle FROM episodes WHERE ( showTitle = ?) COLLATE NOCASE"
c.execute(sql1, (tvshow, ))
datalist = list(c.fetchone())
if datalist > 0:
#print("first episode of tvshow series: "+datalist[0])
return datalist
else:
print("No entry found in DB to add to schedule.")
'''
*
* When incrementing episodes in a series I am advancing by "id"
*
'''
def get_episode_id(episodeTitle):
#print("Getting episode id of "+episodeTitle)
sql1 = "SELECT id FROM episodes WHERE ( title = ?) COLLATE NOCASE"
c.execute(sql1, (episodeTitle, ))
datalist = list(c.fetchone())
if datalist > 0:
return datalist[0]
else:
print("No entry found in DB to add to schedule.")
def generate_daily_schedule():
'''
*
* Everytime this function is run it drops the previous "scheduled_shows" table recreates it
* Everytime this function is run it drops the previous "scheduled_shows" & table recreates it
*
'''
create_table()
@@ -116,14 +166,18 @@ def generate_daily_schedule():
* TODO: generate a reasonable startTime based on previous episode duration
*
'''
add_daily_schedule_to_db(0, first_episode_title, first_episode[5], first_episode[6], row[3], 0, row[5], 0, row[7])
'''
*
* The last episode stored in the "shows" table was not empty... get the next episode in the series
*
'''
else:
endTime = get_end_time_from_duration(row[5], first_episode[4]);
print("End time: " + str(endTime));
add_daily_schedule_to_db(0, first_episode_title, first_episode[5], first_episode[6], row[3], 0, row[5], endTime, row[7])
else:
'''
*
* The last episode stored in the "shows" table was not empty... get the next episode in the series
*
'''
print("First episode already set in shows, advancing episodes forward")
print(str(get_episode_id(lastTitleList[0])))
@@ -154,7 +208,11 @@ def generate_daily_schedule():
* TODO: generate a reasonable startTime based on previous episode duration
*
'''
add_daily_schedule_to_db(0, next_episode[3], next_episode[5], next_episode[6], row[3], 0, row[5], 0, row[7])
endTime = get_end_time_from_duration(row[5], next_episode[4]);
print("End time: " + str(endTime));
add_daily_schedule_to_db(0, next_episode[3], next_episode[5], next_episode[6], row[3], 0, row[5], endTime, row[7])
else:
print("Not grabbing next episode for some reason")
@@ -175,7 +233,13 @@ def generate_daily_schedule():
* TODO: generate a reasonable startTime based on previous episode duration
*
'''
add_daily_schedule_to_db(0, first_episode_title, first_episode[5], first_episode[6], row[3], 0, row[5], 0, row[7])
#print(row[5])
#print(first_episode[4])
endTime = get_end_time_from_duration(row[5], first_episode[4]);
print("End time: " + str(endTime));
add_daily_schedule_to_db(0, first_episode_title, first_episode[5], first_episode[6], row[3], 0, row[5], endTime, row[7])
# raise e