commit b1e55adee58eb0ea650888c4caa9fdf1ef030c5c Author: Justin Emter Date: Thu Jul 13 02:01:51 2017 -0700 First commit. diff --git a/pseudo-tv.db b/pseudo-tv.db new file mode 100644 index 0000000..a7e6f94 Binary files /dev/null and b/pseudo-tv.db differ diff --git a/pseudo_channel.py b/pseudo_channel.py new file mode 100644 index 0000000..87ce67f --- /dev/null +++ b/pseudo_channel.py @@ -0,0 +1,138 @@ +#!/usr/bin/python + +from plexapi.server import PlexServer +import sqlite3 +import time +import os, sys +#import Image +import string +import argparse +import datetime + +from pseudo_config import * + +plex = PlexServer(baseurl, token) + +conn = sqlite3.connect('pseudo-tv.db', timeout=10) +c = conn.cursor() + +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 INTO schedule (unix, mediaID, title, duration, startTime, endTime, dayOfWeek, startTimeUnix) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", (unix, mediaID, title, duration, startTime, endTime, dayOfWeek, startTimeUnix)) + conn.commit() + c.close() + conn.close() + # Catch the exception + except Exception as e: + # Roll back any change if something goes wrong + conn.rollback() + c.close() + conn.close() + raise e + + +def convertMillis(millis): + seconds=(millis/1000)%60 + minutes=(millis/(1000*60))%60 + hours=(millis/(1000*60*60))%24 + return str(hours)+':'+str(minutes)+':'+str(seconds) + +# Example 8: Get a URL to stream a movie or show in another client +def getMediaDuration(): + movie = plex.library.section('Movies').get('Dumb and Dumber') + print('The movie is this long:') + print(convertMillis(movie.duration)); + +def showConnectedClients(): + for client in plex.clients(): + print(client.title) + +def playMovie(): + cars = plex.library.section('Movies').get('Dumb and Dumber') + client = plex.client("RasPlex") + client.playMedia(cars) + +def stopMovie(): + client = plex.client("RasPlex") + client.stop(mtype='video') + +def getIsPlayingMedia(): + client = plex.client("RasPlex") + return client.isPlayingMedia(includePaused=False) + +def getEpisodeDuration(): + movie = plex.library.section('TV Shows').get('Seinfeld') + print('The movie is this long:') + print(movie); + for show in movie: + print(show.title) + +# Example 7: List files for the latest episode of Friends. +#episodes = plex.library.section('TV Shows').get('Friends').episodes() +#for episode in episodes: +# print(episode.parentIndex) + +def getSections(): + sections = plex.library.sections() + for section in sections: + print(section.title) + return section + +def getAllTVShows(): + shows = plex.library.section('TV Shows').all() + for show in shows: + print(show.title) + +def getAllMedia(): + sections = plex.library.sections() + for section in sections: + sectionMedia = plex.library.section(section.title).all() + for media in sectionMedia: + print(media.title) + + + +def get_end_time_from_duration(startTime, duration): + 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 add_schedule(media, name, time, day): + print("Adding the following schedule: -Type: "+media+" -Name: "+name+" -Time: "+time+" -Day: "+day) + + sql1 = "SELECT * FROM "+media+" WHERE (title LIKE ?) COLLATE NOCASE" + c.execute(sql1, (name, )) + datalist = list(c.fetchone()) + if datalist > 0: + print(datalist) + add_schedule_to_db(0, datalist[3], 0, time, 0, day) + #get_end_time_from_duration(time, datalist[3]) + else: + print("No entry found in DB to add to schedule.") + + + +parser = argparse.ArgumentParser() + +parser.add_argument('-a', '--add', dest='add_variable', required=True) +parser.add_argument('-n', '--name', dest='name_variable', required=True) +parser.add_argument('-t', '--time', dest='time_variable', required=True) +parser.add_argument('-d', '--day', dest='day_variable', required=True) + +globals().update(vars(parser.parse_args())) + + +if add_variable: + add_schedule(add_variable, name_variable, time_variable, day_variable) + + +# python pseudo-channel.py -a "shows" -n "curb your enthusiasm" -t "7:30 PM" -d "weekdays" + + +# create_tables() +# update_db_with_media() diff --git a/pseudo_config.py b/pseudo_config.py new file mode 100644 index 0000000..a255320 --- /dev/null +++ b/pseudo_config.py @@ -0,0 +1,4 @@ +#!/usr/bin/python + +baseurl = 'http://media.home:32400' +token = 'the-token' \ No newline at end of file diff --git a/pseudo_generate_daily_scheduledb.py b/pseudo_generate_daily_scheduledb.py new file mode 100644 index 0000000..81b8a1d --- /dev/null +++ b/pseudo_generate_daily_scheduledb.py @@ -0,0 +1,77 @@ +#!/usr/bin/python + +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 + +def get_end_time_from_duration(startTime, duration): + #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_title(tvshow): + print("Getting first episode of "+tvshow) + sql1 = "SELECT title, MIN(episodeNumber), MIN(seasonNumber) FROM episodes WHERE ( showTitle LIKE ?) COLLATE NOCASE" + c.execute(sql1, (tvshow, )) + datalist = list(c.fetchone()) + if datalist > 0: + print("first episode of tvshow series: "+datalist[0]) + return datalist[0] + else: + print("No entry found in DB to add to schedule.") + +def generate_daily_schedule(): + create_table() + c.execute("SELECT * FROM schedule ORDER BY datetime(startTimeUnix) ASC") + datalist = list(c.fetchall()) + prev_row = '' + for row in datalist: + first_episode_title = '' + c.execute("SELECT lastEpisodeTitle FROM shows WHERE title = ?", (row[3], )) + lastTitleList = list(c.fetchone()) + if lastTitleList[0] == '': + first_episode_title = get_first_episode_title(row[3]) + update_shows_table_with_last_episode(row[3], first_episode_title) + else: + print("First episode already set in shows") + c.execute("SELECT lastEpisodeTitle FROM shows WHERE title = ?", (row[3], )) + # if prev_row != '': + # print(prev_row) + # else: + # update_shows_table_with_last_episode() + prev_row = row + +generate_daily_schedule() \ No newline at end of file diff --git a/pseudo_updatedb.py b/pseudo_updatedb.py new file mode 100644 index 0000000..e0bd513 --- /dev/null +++ b/pseudo_updatedb.py @@ -0,0 +1,122 @@ +#!/usr/bin/python + +from plexapi.server import PlexServer +import sqlite3 +import time +import os, sys +import string +import argparse +import datetime + +from pseudo_config import * + +plex = PlexServer(baseurl, token) + +conn = sqlite3.connect('pseudo-tv.db') +c = conn.cursor() + +def create_tables(): + c.execute('CREATE TABLE IF NOT EXISTS movies(id INTEGER PRIMARY KEY AUTOINCREMENT, unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER)') + c.execute('CREATE TABLE IF NOT EXISTS shows(id INTEGER PRIMARY KEY AUTOINCREMENT, unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER, lastEpisodeTitle TEXT)') + c.execute('CREATE TABLE IF NOT EXISTS episodes(id INTEGER PRIMARY KEY AUTOINCREMENT, unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER, episodeNumber INTEGER, seasonNumber INTEGER, showTitle TEXT)') + c.execute('CREATE TABLE IF NOT EXISTS commercials(id INTEGER PRIMARY KEY AUTOINCREMENT, unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER)') + 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)') + c.execute('CREATE TABLE IF NOT EXISTS 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)') + #index + c.execute('CREATE UNIQUE INDEX IF NOT EXISTS idx_episode_title ON episodes (title);') + c.execute('CREATE UNIQUE INDEX IF NOT EXISTS idx_movie_title ON movies (title);') + c.execute('CREATE UNIQUE INDEX IF NOT EXISTS idx_commercial_title ON commercials (title);') + +def add_movies_to_db(mediaID, title, duration): + unix = int(time.time()) + try: + c.execute("INSERT OR REPLACE INTO movies (unix, mediaID, title, duration) VALUES (?, ?, ?, ?)", (unix, mediaID, title, duration)) + conn.commit() + # Catch the exception + except Exception as e: + # Roll back any change if something goes wrong + conn.rollback() + raise e + +def add_shows_to_db(mediaID, title, duration, lastEpisodeTitle): + unix = int(time.time()) + try: + c.execute("INSERT OR REPLACE INTO shows (unix, mediaID, title, duration, lastEpisodeTitle) VALUES (?, ?, ?, ?, ?)", (unix, mediaID, title, duration, lastEpisodeTitle)) + conn.commit() + # Catch the exception + except Exception as e: + # Roll back any change if something goes wrong + conn.rollback() + raise e + +def add_episodes_to_db(mediaID, title, duration, episodeNumber, seasonNumber, showTitle): + unix = int(time.time()) + try: + c.execute("INSERT OR REPLACE INTO episodes (unix, mediaID, title, duration, episodeNumber, seasonNumber, showTitle) VALUES (?, ?, ?, ?, ?, ?, ?)", (unix, mediaID, title, duration, episodeNumber, seasonNumber, showTitle)) + conn.commit() + # Catch the exception + except Exception as e: + # Roll back any change if something goes wrong + conn.rollback() + raise e + +def add_commercials_to_db(mediaID, title, duration): + unix = int(time.time()) + try: + c.execute("INSERT OR REPLACE INTO commercials (unix, mediaID, title, duration) VALUES (?, ?, ?, ?)", (unix, mediaID, title, duration)) + conn.commit() + # Catch the exception + except Exception as e: + # Roll back any change if something goes wrong + conn.rollback() + raise e + +def add_schedule_to_db(mediaID, title, duration, startTime, endTime, dayOfWeek): + unix = int(time.time()) + try: + c.execute("INSERT INTO schedule (unix, mediaID, title, duration, startTime, endTime, dayOfWeek) VALUES (?, ?, ?, ?, ?, ?, ?)", (unix, mediaID, title, 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 + +def update_db_with_media(): + sections = plex.library.sections() + for section in sections: + if section.title == "Movies": + sectionMedia = plex.library.section(section.title).all() + for media in sectionMedia: + add_movies_to_db(1, media.title, media.duration) + elif section.title == "TV Shows": + sectionMedia = plex.library.section(section.title).all() + for media in sectionMedia: + add_shows_to_db(2, media.title, media.duration, '') + #add all episodes of each tv show to episodes table + episodes = plex.library.section(section.title).get(media.title).episodes() + for episode in episodes: + duration = episode.duration + if duration: + add_episodes_to_db(4, episode.title, duration, episode.index, episode.parentIndex, media.title) + else: + add_episodes_to_db(4, episode.title, 0, episode.index, episode.parentIndex, media.title) + elif section.title == "Commercials": + sectionMedia = plex.library.section(section.title).all() + for media in sectionMedia: + add_commercials_to_db(3, media.title, media.duration) + c.close() + conn.close() + +def update_db_with_media_test(): + sections = plex.library.sections() + for section in sections: + if section.title == "TV Shows": + sectionMedia = plex.library.section(section.title).all() + for media in sectionMedia: + episodes = plex.library.section(section.title).get(media.title).episodes() + for episode in episodes: + print(episode.parentKey) + +create_tables() +update_db_with_media() \ No newline at end of file diff --git a/schedule.sh b/schedule.sh new file mode 100644 index 0000000..77293bf --- /dev/null +++ b/schedule.sh @@ -0,0 +1,32 @@ + +python pseudo-channel.py -a "shows" -n "looney tunes" -t "6:00 AM" -d "weekdays" +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 "Garfield & Friends" -t "8:00 AM" -d "weekdays" +python pseudo-channel.py -a "shows" -n "Garfield & Friends" -t "8:30 AM" -d "weekdays" + +python pseudo-channel.py -a "shows" -n "talespin" -t "9:00 AM" -d "weekdays" +python pseudo-channel.py -a "shows" -n "talespin" -t "9:30 AM" -d "weekdays" + +python pseudo-channel.py -a "shows" -n "macgyver" -t "10:00 AM" -d "weekdays" +python pseudo-channel.py -a "shows" -n "macgyver" -t "11:00 AM" -d "weekdays" + +python pseudo-channel.py -a "shows" -n "boy meets world" -t "12:00 PM" -d "weekdays" + +python pseudo-channel.py -a "shows" -n "full house" -t "12:30 PM" -d "weekdays" +python pseudo-channel.py -a "shows" -n "full house" -t "1:00 PM" -d "weekdays" +python pseudo-channel.py -a "shows" -n "full house" -t "1:30 PM" -d "weekdays" +python pseudo-channel.py -a "shows" -n "full house" -t "2:00 PM" -d "weekdays" + +python pseudo-channel.py -a "shows" -n "the it crowd" -t "2:30 PM" -d "weekdays" +python pseudo-channel.py -a "shows" -n "the it crowd" -t "3:00 PM" -d "weekdays" + +python pseudo-channel.py -a "shows" -n "strangers with candy" -t "3:30 PM" -d "weekdays" + +python pseudo-channel.py -a "shows" -n "the office (us)" -t "4:00 PM" -d "weekdays" +python pseudo-channel.py -a "shows" -n "the office (us)" -t "4:30 PM" -d "weekdays" + +python pseudo-channel.py -a "shows" -n "friends" -t "5:00 PM" -d "weekdays" +python pseudo-channel.py -a "shows" -n "friends" -t "5:30 PM" -d "weekdays"