From 8d78bb5fd283efc06b90a7d759c025ab4d2a6f26 Mon Sep 17 00:00:00 2001 From: Justin Emter Date: Fri, 14 Jul 2017 10:14:56 -0700 Subject: [PATCH] Added endTime to scheduled content --- pseudo-tv.db | Bin 460800 -> 461824 bytes pseudo_generate_daily_scheduledb.py | 102 ++++++++++++++++++++++------ 2 files changed, 83 insertions(+), 19 deletions(-) diff --git a/pseudo-tv.db b/pseudo-tv.db index 00423dc972b617d2d83b194ddb003c378ae2941a..bfcd998aca6574cd5de267701f4c15183f6bb140 100644 GIT binary patch delta 2539 zcmZveZ)g)|9LMi@o;2yTO=mi5wSTs&rfa*_^X`(%pHo(ArFCLgwTU%sh)46Z2}>{K zl33!4Mds)WePKn$DGYRDu)!EiJGQYm8#>s_F&K``Hv^Fkd@-5e8)e(?Nlo*l>2a?v zzkKfd{Qi7@cW$No+|}-t&bCfP5YQc=dX+8@Yq(RndHUm*TQGS7KcMf(Q}SfhxBV~D zi0-t*gYTQ)^)LI;vL7$|$+F+I>~HwW?{@v-M&z16u0iM}(nhzn2(HFB61y6^Mtc&e zr(Zh1KgNBfaXU?|WKK5%iga{RjVj7+MTJ{Z!-gI-G*ylU6u6cseLHCub6$|pEDQ_e zb25eh##ixc=mENj6yZ1FJUp(s;?3-xeWEzj*i(Mzttm^&r>Hbetyy{?Ta+eCl;(5N zOxDgMRn1T~YS0Xg-Lj&9uyqZwCdS=^#GVQ_mP*q(DLW?}C?w5An#+?(b9SP5hT5vi zsG~I}Q;lH6P$F_vibfKmj<*!95XX?dNFN9Sv6`jD^Wua7)n6aLP3sw z(o<$L$!qMJ9J`8E2dm`TY=l(_(2!(ZlY=TRd&bSsyO5|>&@-l$r%7qXOj7Ku{Nvs+ z%C(CUVFGnk4hB?SU<0G{i&e@|YR*zP`msWq<{4%y)L_G5BZdYnD0`GId)!rKA>3C{ zG-Xj~GHX*QmR_LEo{LAaS&J5>Siz#XaD5KpQ5dQw2f{q|l#eiOl;b-4A2)0}EN%qW z=>%HH&?9m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%BLJ@BQU;$>peM=&-X%&r)m>4MwgEE{ z1z4MEoO}RBKqt6RgV>BO@_R-c3=|~qwOwS2wU(TyaBV|hemvb z)R70|I@*?S4|(fCsdL>mR-_;aH-I_)+SQLqcKyop8@qKq-;4#s10D_;$R8+2em|2p zbEreWT~Np2-;wkPX==N@KUgq7t|5Gl-|-`aAK~x$4fXMOdYva#!LEkWoV}E;32q{) zs;O{>%pfAJ8N{?Pn|rXuF5vah&Eap8%%tRugOf%+LVM>=hts_-EK z@-HHA8`ZD*Ty{^P5O08H_A^n+UHSXYVbEG2AYSmWKe19(k~e2q4JcnS%`Ml~IPgv6 z!0Qcgmq@S*i!ce#!%?u|b@J&}jiki!TA;wuRw3j%M5;G?cgwN1dZ|y;N>-hci)42R zWP@z5OE)Et{h338<^dPml$8oK-68CltXMCQ-;j)u$0;B#=-|pPlJw-{X*y^k!w&!P RiGSY?qyOPXW>b{M{{z!72uc6| diff --git a/pseudo_generate_daily_scheduledb.py b/pseudo_generate_daily_scheduledb.py index aa9d894..901e12e 100644 --- a/pseudo_generate_daily_scheduledb.py +++ b/pseudo_generate_daily_scheduledb.py @@ -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