From 0ee6922b392b4b00d991dc0e3cc420e3c9069b2b Mon Sep 17 00:00:00 2001 From: Justin Emter Date: Thu, 30 Nov 2017 13:03:39 -0800 Subject: [PATCH] Added random movie logic but commenting it out for now. --- PseudoChannel.py | 23 ++++++++ src/PseudoChannelDatabase.py | 18 +++--- src/PseudoChannelRandomMovie.py | 101 ++++++++++++++++++++++++++++++-- 3 files changed, 130 insertions(+), 12 deletions(-) diff --git a/PseudoChannel.py b/PseudoChannel.py index fa01c5f..88f8620 100644 --- a/PseudoChannel.py +++ b/PseudoChannel.py @@ -29,6 +29,7 @@ from src import Music from src import Video from src import PseudoDailyScheduleController from src import PseudoChannelCommercial +from src import PseudoChannelRandomMovie import pseudo_config as config reload(sys) @@ -62,6 +63,9 @@ class PseudoChannel(): self.CONTROLLER_SERVER_PORT, self.DEBUG ) + + self.movieMagic = PseudoChannelRandomMovie() + """Database functions. update_db(): Grab the media from the Plex DB and store it in the local pseudo-channel.db. drop_db(): Drop the local database. Fresh start. @@ -423,11 +427,30 @@ class PseudoChannel(): d[key] = val.split(',') for movie in movies.search(None, **d): movies_list.append(movie) + + """the_movie = self.db.get_movie(self.movieMagic.get_random_movie_xtra( + self.db.get_movies(),# Movies DB + movies_list # XTRA List + ) + )""" + + """Updating movies table in the db with lastPlayedDate entry""" + self.db.update_movies_table_with_last_played_date(the_movie[3]) + the_movie = self.db.get_movie(random.choice(movies_list).title) except: print("For some reason, I've failed getting movie with xtra args.") the_movie = self.db.get_random_movie() else: + + """the_movie = self.db.get_movie(self.movieMagic.get_random_movie( + self.db.get_movies(),# Movies DB + ) + )""" + + """Updating movies table in the db with lastPlayedDate entry""" + self.db.update_movies_table_with_last_played_date(the_movie[3]) + the_movie = self.db.get_random_movie() else: the_movie = self.db.get_movie(entry[3]) diff --git a/src/PseudoChannelDatabase.py b/src/PseudoChannelDatabase.py index 2e72c06..9bde074 100644 --- a/src/PseudoChannelDatabase.py +++ b/src/PseudoChannelDatabase.py @@ -20,7 +20,7 @@ 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, plexMediaID TEXT, lastPlayedDate TEXT)') + '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, plexMediaID TEXT)') @@ -302,7 +302,7 @@ class PseudoChannelDatabase(): now = datetime.datetime.now() lastPlayedDate = now.strftime('%Y-%m-%d') - sql = "UPDATE movies SET lastPlayedDate = ? WHERE movieTitle LIKE ? COLLATE NOCASE" + sql = "UPDATE movies SET lastPlayedDate = ? WHERE title LIKE ? COLLATE NOCASE" self.cursor.execute(sql, (lastPlayedDate, movieTitle, )) self.conn.commit() @@ -317,11 +317,15 @@ class PseudoChannelDatabase(): def get_media(self, title, mediaType): - media = mediaType - sql = "SELECT * FROM "+media+" WHERE (title LIKE ?) COLLATE NOCASE" - self.cursor.execute(sql, ("%"+title+"%", )) - media_item = self.cursor.fetchone() - return media_item + print "+++++ title:", title + if(title is not None): + media = mediaType + sql = "SELECT * FROM "+media+" WHERE (title LIKE ?) COLLATE NOCASE" + self.cursor.execute(sql, ("%"+title+"%", )) + media_item = self.cursor.fetchone() + return media_item + else: + pass def get_schedule(self): diff --git a/src/PseudoChannelRandomMovie.py b/src/PseudoChannelRandomMovie.py index 5c73724..08aaaf1 100644 --- a/src/PseudoChannelRandomMovie.py +++ b/src/PseudoChannelRandomMovie.py @@ -16,17 +16,108 @@ is then returned. For smaller movie libraries it'll be useful to calculate a reasonable "TIME_GAP_MONTHS". """ +import datetime +import random +from random import shuffle + class PseudoChannelRandomMovie(): - MOVIES = [] - TIME_GAP_MONTHS = 6 + MOVIES_DB = [] + TIME_GAP_DAYS = 182 + MOVIES_XTRA_LIST = [] def __init__(self): pass - def get_random_movie(self, movies): + """ + If not using "xtra" params + """ + def get_random_movie(self, moviesDB): - self.MOVIES = movies + self.MOVIES_DB = moviesDB - pass \ No newline at end of file + shuffle(self.MOVIES_DB) + + for movie in self.MOVIES_DB: + + if(movie[5] is not None): #lastPlayedDate is recorded + + print movie[5] + + now = datetime.datetime.now() + lastPlayedDate = datetime.datetime.strptime(movie[5], '%Y-%m-%d') + + timeDelta = lastPlayedDate - now + + if(timeDelta.days >= self.TIME_GAP_DAYS): + + return movie[3] + + else: + + break + else: + + return movie[3] # No lastPlayedDate is recorded so lets use this movie + + """ + If using "xtra" args, expect a list from the Python PLEX API. + Loop over list titles and lookup the title against the + MOVIES_DB to see the lastPlayedDate, etc. + + @Returns: Movie Title (String) + """ + def get_random_movie_xtra(self, moviesDB, moviesXTRAList): + + self.MOVIES_DB = moviesDB + self.MOVIES_XTRA_LIST = moviesXTRAList + + shuffle(self.MOVIES_DB) + + print("get_random_movie_xtra") + + for movieOne in self.MOVIES_XTRA_LIST: + + print("while i < len(self.MOVIES_XTRA_LIST):") + + movieTitle = movieOne.title + + print("movieTitle", movieTitle) + + for movie in self.MOVIES_DB: + + print("for movie in self.MOVIES_DB:") + + if movie[3] == movieTitle: #title match found + + if(movie[5] is not None): #lastPlayedDate is recorded + + print("I am here") + + now = datetime.datetime.now() + lastPlayedDate = datetime.datetime.strptime(movie[5], '%Y-%m-%d') + + timeDelta = lastPlayedDate - now + + if(timeDelta.days >= self.TIME_GAP_DAYS): + + print("if(timeDelta.months >= self.TIME_GAP_DAYS):") + + return movieTitle + + else: + + break + + else: + + return movieTitle # No lastPlayedDate recorded + + else: + + pass + + return random.choice(self.MOVIES_XTRA_LIST).title # Everything has been played, choosing something. + + pass \ No newline at end of file