diff --git a/PseudoChannel.py b/PseudoChannel.py index b7916d3..0556578 100644 --- a/PseudoChannel.py +++ b/PseudoChannel.py @@ -100,7 +100,7 @@ class PseudoChannel(): for i, media in enumerate(sectionMedia): - self.db.add_movies_to_db(1, media.title, media.duration) + self.db.add_movies_to_db(1, media.title, media.duration, media.key) self.print_progress( i + 1, @@ -125,7 +125,7 @@ class PseudoChannel(): backgroundImgURL = config.baseurl+backgroundImagePath.art+"?X-Plex-Token="+config.token - self.db.add_shows_to_db(2, media.title, media.duration, '', backgroundImgURL) + self.db.add_shows_to_db(2, media.title, media.duration, '', backgroundImgURL, media.key) self.print_progress( i + 1, @@ -150,7 +150,8 @@ class PseudoChannel(): duration, episode.index, episode.parentIndex, - media.title + media.title, + episode.key ) else: @@ -161,7 +162,8 @@ class PseudoChannel(): 0, episode.index, episode.parentIndex, - media.title + media.title, + episode.key ) elif correct_lib_name == "Commercials": @@ -172,7 +174,7 @@ class PseudoChannel(): for i, media in enumerate(sectionMedia): - self.db.add_commercials_to_db(3, media.title, media.duration) + self.db.add_commercials_to_db(3, media.title, media.duration, media.key) self.print_progress( i + 1, @@ -589,6 +591,7 @@ class PseudoChannel(): entry[10], # is_strict_time entry[11], # time_shift entry[12], # overlap_max + next_episode[8], # plex id entry[3], # show_series_title next_episode[5], # episode_number next_episode[6] # season_number @@ -623,7 +626,8 @@ class PseudoChannel(): entry[7], # day_of_week entry[10], # is_strict_time entry[11], # time_shift - entry[12] # overlap_max + entry[12], # overlap_max + the_movie[6] # plex id ) #print(movie.natural_end_time) @@ -649,7 +653,8 @@ class PseudoChannel(): entry[7], # day_of_week entry[10], # is_strict_time entry[11], # time_shift - entry[12] # overlap_max + entry[12], # overlap_max + the_music[6], # plex id ) #print(music.natural_end_time) @@ -675,7 +680,8 @@ class PseudoChannel(): entry[7], # day_of_week entry[10], # is_strict_time entry[11], # time_shift - entry[12] # overlap_max + entry[12], # overlap_max + the_video[6] # plex id ) #print(music.natural_end_time) @@ -751,6 +757,10 @@ class PseudoChannel(): previous_episode = entry + def make_xml_schedule(self): + + self.controller.make_xml_schedule(self.db.get_daily_schedule()) + def get_daily_schedule_as_media_object_list(self): for i, item in enumerate(self.db.get_daily_schedule(), start=0): @@ -887,6 +897,15 @@ if __name__ == '__main__': action='store_true', help='Updates the schedule based on entries in the google calendar.') + ''' + * + * Make XML / HTML Schedule: "python PseudoChannel.py -m" + * + ''' + parser.add_argument('-m', + action='store_true', + help='Makes the XML / HTML schedule based on the daily_schedule table.') + globals().update(vars(parser.parse_args())) args = parser.parse_args() @@ -917,6 +936,10 @@ if __name__ == '__main__': pseudo_channel.show_schedule() + if args.m: + + pseudo_channel.make_xml_schedule() + if args.r: try: @@ -948,6 +971,8 @@ if __name__ == '__main__': pseudo_channel.generate_daily_schedule() + pseudo_channel.make_xml_schedule() + pseudo_channel.controller.tv_controller(pseudo_channel.db.get_daily_schedule()) t = datetime.datetime.utcnow() diff --git a/src/Commercial.py b/src/Commercial.py index 254c4da..800f217 100644 --- a/src/Commercial.py +++ b/src/Commercial.py @@ -26,7 +26,8 @@ class Commercial(Media): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id ): super(Commercial, self).__init__( @@ -38,5 +39,6 @@ class Commercial(Media): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id ) diff --git a/src/Episode.py b/src/Episode.py index a9f902e..7286184 100644 --- a/src/Episode.py +++ b/src/Episode.py @@ -30,9 +30,10 @@ class Episode(Media): is_strict_time, time_shift, overlap_max, + plex_media_id, show_series_title, episode_number, - season_number + season_number, ): super(Episode, self).__init__( @@ -44,7 +45,8 @@ class Episode(Media): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id ) self.show_series_title = show_series_title diff --git a/src/Media.py b/src/Media.py index 992261f..9c37abe 100644 --- a/src/Media.py +++ b/src/Media.py @@ -31,7 +31,8 @@ class Media(object): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id ): self.section_type = section_type @@ -43,6 +44,7 @@ class Media(object): self.is_strict_time = is_strict_time self.time_shift = time_shift self.overlap_max = overlap_max + self.plex_media_id = plex_media_id self.start_time = natural_start_time self.end_time = natural_end_time diff --git a/src/Movie.py b/src/Movie.py index d2975d8..37644fe 100644 --- a/src/Movie.py +++ b/src/Movie.py @@ -26,7 +26,8 @@ class Movie(Media): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id ): super(Movie, self).__init__( @@ -38,5 +39,6 @@ class Movie(Media): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id ) diff --git a/src/Music.py b/src/Music.py index 4b8ee25..e63bdd7 100644 --- a/src/Music.py +++ b/src/Music.py @@ -26,7 +26,8 @@ class Music(Media): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id ): super(Music, self).__init__( @@ -38,5 +39,6 @@ class Music(Media): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id ) diff --git a/src/PseudoChannelDatabase.py b/src/PseudoChannelDatabase.py index 5391edd..6c424bf 100644 --- a/src/PseudoChannelDatabase.py +++ b/src/PseudoChannelDatabase.py @@ -23,29 +23,30 @@ class PseudoChannelDatabase(): self.cursor.execute('CREATE TABLE IF NOT EXISTS ' 'movies(id INTEGER PRIMARY KEY AUTOINCREMENT, ' - 'unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER, lastPlayedDate TEXT)') + 'unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER, ' + 'lastPlayedDate TEXT, plexMediaID TEXT)') self.cursor.execute('CREATE TABLE IF NOT EXISTS ' 'videos(id INTEGER PRIMARY KEY AUTOINCREMENT, ' - 'unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER)') + 'unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER, plexMediaID TEXT)') self.cursor.execute('CREATE TABLE IF NOT EXISTS ' 'music(id INTEGER PRIMARY KEY AUTOINCREMENT, ' - 'unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER)') + 'unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER, plexMediaID TEXT)') self.cursor.execute('CREATE TABLE IF NOT EXISTS ' 'shows(id INTEGER PRIMARY KEY AUTOINCREMENT, ' 'unix INTEGER, mediaID INTEGER, title TEXT, duration INTEGER, ' - 'lastEpisodeTitle TEXT, fullImageURL TEXT)') + 'lastEpisodeTitle TEXT, fullImageURL TEXT, plexMediaID TEXT)') self.cursor.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)') + 'episodeNumber INTEGER, seasonNumber INTEGER, showTitle TEXT, plexMediaID TEXT)') self.cursor.execute('CREATE TABLE IF NOT EXISTS ' 'commercials(id INTEGER PRIMARY KEY AUTOINCREMENT, unix INTEGER, ' - 'mediaID INTEGER, title TEXT, duration INTEGER)') + 'mediaID INTEGER, title TEXT, duration INTEGER, plexMediaID TEXT)') self.cursor.execute('CREATE TABLE IF NOT EXISTS ' 'schedule(id INTEGER PRIMARY KEY AUTOINCREMENT, unix INTEGER, ' @@ -57,7 +58,7 @@ class PseudoChannelDatabase(): '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, sectionType TEXT)') + 'dayOfWeek TEXT, sectionType TEXT, plexMediaID TEXT)') self.cursor.execute('CREATE TABLE IF NOT EXISTS ' 'app_settings(id INTEGER PRIMARY KEY AUTOINCREMENT, version TEXT)') @@ -119,12 +120,12 @@ class PseudoChannelDatabase(): Setters, etc. """ - def add_movies_to_db(self, mediaID, title, duration): + def add_movies_to_db(self, mediaID, title, duration, plexMediaID): unix = int(time.time()) try: self.cursor.execute("INSERT OR REPLACE INTO movies " - "(unix, mediaID, title, duration) VALUES (?, ?, ?, ?)", - (unix, mediaID, title, duration)) + "(unix, mediaID, title, duration, plexMediaID) VALUES (?, ?, ?, ?, ?)", + (unix, mediaID, title, duration, plexMediaID)) self.conn.commit() # Catch the exception @@ -133,12 +134,12 @@ class PseudoChannelDatabase(): self.conn.rollback() raise e - def add_videos_to_db(self, mediaID, title, duration): + def add_videos_to_db(self, mediaID, title, duration, plexMediaID): unix = int(time.time()) try: self.cursor.execute("INSERT OR REPLACE INTO videos " - "(unix, mediaID, title, duration) VALUES (?, ?, ?, ?)", - (unix, mediaID, title, duration)) + "(unix, mediaID, title, duration, plexMediaID) VALUES (?, ?, ?, ?, ?)", + (unix, mediaID, title, duration, plexMediaID)) self.conn.commit() # Catch the exception @@ -147,12 +148,12 @@ class PseudoChannelDatabase(): self.conn.rollback() raise e - def add_shows_to_db(self, mediaID, title, duration, lastEpisodeTitle, fullImageURL): + def add_shows_to_db(self, mediaID, title, duration, lastEpisodeTitle, fullImageURL, plexMediaID): unix = int(time.time()) try: self.cursor.execute("INSERT OR REPLACE INTO shows " - "(unix, mediaID, title, duration, lastEpisodeTitle, fullImageURL) VALUES (?, ?, ?, ?, ?, ?)", - (unix, mediaID, title, duration, lastEpisodeTitle, fullImageURL)) + "(unix, mediaID, title, duration, lastEpisodeTitle, fullImageURL, plexMediaID) VALUES (?, ?, ?, ?, ?, ?, ?)", + (unix, mediaID, title, duration, lastEpisodeTitle, fullImageURL, plexMediaID)) self.conn.commit() # Catch the exception except Exception as e: @@ -160,12 +161,12 @@ class PseudoChannelDatabase(): self.conn.rollback() raise e - def add_episodes_to_db(self, mediaID, title, duration, episodeNumber, seasonNumber, showTitle): + def add_episodes_to_db(self, mediaID, title, duration, episodeNumber, seasonNumber, showTitle, plexMediaID): unix = int(time.time()) try: self.cursor.execute("INSERT OR REPLACE INTO episodes " - "(unix, mediaID, title, duration, episodeNumber, seasonNumber, showTitle) VALUES (?, ?, ?, ?, ?, ?, ?)", - (unix, mediaID, title, duration, episodeNumber, seasonNumber, showTitle)) + "(unix, mediaID, title, duration, episodeNumber, seasonNumber, showTitle, plexMediaID) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + (unix, mediaID, title, duration, episodeNumber, seasonNumber, showTitle, plexMediaID)) self.conn.commit() # Catch the exception except Exception as e: @@ -173,15 +174,16 @@ class PseudoChannelDatabase(): self.conn.rollback() raise e - def add_commercials_to_db(self, mediaID, title, duration): + def add_commercials_to_db(self, mediaID, title, duration, plexMediaID): unix = int(time.time()) try: self.cursor.execute("INSERT OR REPLACE INTO commercials " - "(unix, mediaID, title, duration) VALUES (?, ?, ?, ?)", - (unix, mediaID, title, duration)) + "(unix, mediaID, title, duration, plexMediaID) VALUES (?, ?, ?, ?, ?)", + (unix, mediaID, title, duration, plexMediaID)) self.conn.commit() # Catch the exception except Exception as e: + print plexMediaID # Roll back any change if something goes wrong self.conn.rollback() raise e @@ -211,7 +213,8 @@ class PseudoChannelDatabase(): startTime, endTime, dayOfWeek, - sectionType + sectionType, + plexMediaID ): unix = int(time.time()) @@ -220,8 +223,8 @@ class PseudoChannelDatabase(): self.cursor.execute("INSERT OR REPLACE INTO daily_schedule " "(unix, mediaID, title, episodeNumber, seasonNumber, " - "showTitle, duration, startTime, endTime, dayOfWeek, sectionType) " - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "showTitle, duration, startTime, endTime, dayOfWeek, sectionType, plexMediaID) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", ( unix, mediaID, @@ -233,7 +236,8 @@ class PseudoChannelDatabase(): startTime, endTime, dayOfWeek, - sectionType + sectionType, + plexMediaID )) self.conn.commit() @@ -261,7 +265,8 @@ class PseudoChannelDatabase(): media.start_time, media.end_time, media.day_of_week, - media.section_type + media.section_type, + media.plex_media_id ) """Database functions. diff --git a/src/PseudoDailyScheduleController.py b/src/PseudoDailyScheduleController.py index dde5d78..5b81c5e 100644 --- a/src/PseudoDailyScheduleController.py +++ b/src/PseudoDailyScheduleController.py @@ -5,6 +5,7 @@ from datetime import datetime import sqlite3 from yattag import Doc +from yattag import indent import os, sys import logging @@ -56,6 +57,63 @@ class PseudoDailyScheduleController(): return backgroundImgURL + def get_xml_from_daily_schedule(self, currentTime, bgImageURL, datalist): + + now = datetime.now() + + time = now.strftime("%B %d, %Y") + + doc, tag, text, line = Doc( + + ).ttl() + + doc.asis('') + + with tag('schedule', currently_playing_bg_image=bgImageURL if bgImageURL != None else ''): + + for row in datalist: + + timeB = datetime.strptime(row[8], '%I:%M %p') + + if currentTime == None: + + with tag('time', + ('data-key', str(row[12])), + ('data-current', 'false'), + ('data-type', str(row[11])), + ('data-title', str(row[3])), + ('data-start-time', str(row[8])), + ): + + text(row[8]) + + elif currentTime.hour == timeB.hour and currentTime.minute == timeB.minute: + + with tag('time', + ('data-key', str(row[12])), + ('data-current', 'true'), + ('data-type', str(row[11])), + ('data-title', str(row[3])), + ('data-start-time', str(row[8])), + ): + + text(row[8]) + + else: + + with tag('time', + ('data-key', str(row[12])), + ('data-current', 'false'), + ('data-type', str(row[11])), + ('data-title', str(row[3])), + ('data-start-time', str(row[8])), + ): + + text(row[8]) + + return indent(doc.getvalue()) + + ''' * * Get the generated html for the .html file that is the schedule. @@ -176,7 +234,7 @@ class PseudoDailyScheduleController(): text(row[8]) - return doc.getvalue() + return indent(doc.getvalue()) ''' * @@ -207,6 +265,35 @@ class PseudoDailyScheduleController(): f.write(data) + ''' + * + * Create 'schedules' dir & write the generated xml to .xml file. + * @param data: xml string + * @return null + * + ''' + def write_xml_to_file(self, data): + + now = datetime.now() + + fileName = "pseudo_schedule.xml" + + writepath = './schedules/' + + if not os.path.exists(writepath): + + os.makedirs(writepath) + + if os.path.exists(writepath+fileName): + + os.remove(writepath+fileName) + + mode = 'a' if os.path.exists(writepath) else 'w' + + with open(writepath+fileName, mode) as f: + + f.write(data) + ''' * * Trigger "playMedia()" on the Python Plex API for specified media. @@ -283,6 +370,7 @@ class PseudoDailyScheduleController(): print("Ok end time found") self.write_schedule_to_file(self.get_html_from_daily_schedule(None, None, datalist)) + self.write_xml_to_file(self.get_xml_from_daily_schedule(None, None, datalist)) break ''' @@ -328,6 +416,19 @@ class PseudoDailyScheduleController(): ) ) + """Generate / write XML to file + """ + self.write_xml_to_file( + self.get_xml_from_daily_schedule( + timeB, + self.get_show_photo( + row[11], + row[6] if row[11] == "TV Shows" else row[3] + ), + datalist + ) + ) + self.my_logger.debug('Trying to play: ' + row[3]) break @@ -336,4 +437,11 @@ class PseudoDailyScheduleController(): if datalistLengthMonitor >= len(datalist): - self.check_for_end_time(datalist) \ No newline at end of file + self.check_for_end_time(datalist) + + def make_xml_schedule(self, datalist): + + print "+++++ ", "Writing XML / HTML to file." + + self.write_schedule_to_file(self.get_html_from_daily_schedule(None, None, datalist)) + self.write_xml_to_file(self.get_xml_from_daily_schedule(None, None, datalist)) \ No newline at end of file diff --git a/src/Video.py b/src/Video.py index d09fa8d..6507255 100644 --- a/src/Video.py +++ b/src/Video.py @@ -26,7 +26,8 @@ class Video(Media): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id ): super(Video, self).__init__( @@ -38,5 +39,6 @@ class Video(Media): day_of_week, is_strict_time, time_shift, - overlap_max + overlap_max, + plex_media_id )