From 82e056b99a22799275e99b4651b4a0ede9c5d292 Mon Sep 17 00:00:00 2001 From: Moe Fwacky Date: Sun, 21 Feb 2021 14:40:15 -0800 Subject: [PATCH] Setup script and updates for python 3 migration Most updates related to adding install script and schedule generator for first install --- both-dir/PseudoChannel.py | 17 +++++-- main-dir/Global_DatabaseUpdate.py | 77 ++++++++++++++++++++++--------- requirements.txt | 1 + setup.py | 33 +++++++++++++ 4 files changed, 100 insertions(+), 28 deletions(-) diff --git a/both-dir/PseudoChannel.py b/both-dir/PseudoChannel.py index a7bbe09..292b180 100644 --- a/both-dir/PseudoChannel.py +++ b/both-dir/PseudoChannel.py @@ -666,11 +666,18 @@ class PseudoChannel(): for key, val in weekday_dict.items(): if str(entry[7]) in str(val) and int(weekno) == int(key): if section == "TV Shows": - minmax = entry[4].split(",") - min = int(minmax[0]) - min = min * 60000 - max = int(minmax[1]) - max = max * 60000 + try: + minmax = entry[4].split(",") + min = int(minmax[0]) + min = min * 60000 + max = int(minmax[1]) + max = max * 60000 + except: + minmax = entry[4] + min = int(minmax) + min = min * 60000 + max = int(minmax) + max = max * 60000 if str(entry[3]).lower() == "random": sections = self.PLEX.library.sections() shows_list = [] diff --git a/main-dir/Global_DatabaseUpdate.py b/main-dir/Global_DatabaseUpdate.py index fedda58..5bc46cf 100644 --- a/main-dir/Global_DatabaseUpdate.py +++ b/main-dir/Global_DatabaseUpdate.py @@ -11,8 +11,10 @@ import sqlite3 import os import datetime import time +import math from shutil import copy2 from pseudo_config import plexLibraries as global_commercials +from src import PseudoChannelDatabase channel_dir_increment_symbol = "_" @@ -43,7 +45,7 @@ update_call = "python PseudoChannel.py %s" % update_flags # Step ONE: Global database update -print("NOTICE: Doing global update from PLEX: %s" % update_flags) +print("ACTION: Doing global update from PLEX: %s" % update_flags) try: os.rename("pseudo-channel.db", "pseudo-channel.bak") except OSError: @@ -76,7 +78,7 @@ for channel_dir in channel_dirs: os.chdir(os.path.join(channel_dirA, channel_dir)) channel_dirA = os.getcwd() db_path = os.path.join(channel_dirA, "pseudo-channel.db") - print("NOTICE: Importing from " + db_path) + print("ACTION: Importing from " + db_path) try: conn = sqlite3.connect(db_path) @@ -103,12 +105,12 @@ for channel_dir in channel_dirs: # Step THREE: Delete the previous database, replace with the recently created global one - print("NOTICE: Copying global update to " + db_path) + print("ACTION: Copying global update to " + db_path) copy2('../pseudo-channel.db','.') # Step FOUR: Import the previous information we exported previously - print("NOTICE: Exporting to " + db_path) + print("ACTION: Exporting to " + db_path) conn = sqlite3.connect(db_path) table = conn.cursor() @@ -120,40 +122,69 @@ for channel_dir in channel_dirs: sql = "UPDATE movies SET lastPlayedDate=? WHERE title=?" table.execute(sql,lastMovie_export[i]) if len(schedule) == 0: + #db = PseudoChannelDatabase("./pseudo-channel.db") print("NOTICE: Schedule Not Found, Creating Default Schedule") entryList = {} entryList['id'] = "1" entryList['unix'] = str(time.time()) - entryList['mediaID'] = "999" - entryList['title'] = "random" - entryList['duration'] = "10,90" + entryList['mediaID'] = "0" + rndsql = "SELECT * FROM shows WHERE (customSectionName NOT LIKE 'playlist' AND duration BETWEEN 6000 and 999000) ORDER BY RANDOM() LIMIT 1" + table.execute(rndsql) + the_show = table.fetchone() + entryList['duration'] = str("1,"+str(int(the_show[4]) / 60000)) + entryList['title'] = the_show[3] entryList['startTime'] = "00:00:00" - entryList['endTime'] = "0" entryList['dayOfWeek'] = "everyday" - entryList['startTimeUnix'] = str(time.mktime(time.strptime("2000/01/01 00:00:00", "%Y/%m/%d %H:%M:%S"))) + entryList['startTimeUnix'] = time.mktime(time.strptime("2000/01/01 00:00:00", "%Y/%m/%d %H:%M:%S")) entryList['section'] = "TV Shows" - entryList['strictTime'] = "true" - entryList['timeShift'] = "5" + if entryList['startTime'] == "00:00:00": + entryList['strictTime'] = "true" + else: + entryList['strictTime'] = "secondary" + entryList['endTime'] = datetime.datetime.fromtimestamp(float(entryList['startTimeUnix']) + the_show[4]/1000).strftime("%H:%M:%S") + entryList['timeShift'] = 15 entryList['overlapMax'] = 15 entryList['xtra'] = "" + print("INFO: Adding "+entryList['startTime']+" - "+entryList['title']+"\033[K",end='\n') sql = "INSERT INTO schedule(id,unix,mediaID,title,duration,startTime,endTime,dayOfWeek,startTimeUnix,section,strictTime,timeShift,overlapMax,xtra) \ VALUES(:id,:unix,:mediaID,:title,:duration,:startTime,:endTime,:dayOfWeek,:startTimeUnix,:section,:strictTime,:timeShift,:overlapMax,:xtra)" table.execute(sql,entryList) - while int(entryList['id']) < 96: + timediff = datetime.datetime.strptime("23:59:59", "%H:%M:%S") - datetime.datetime.strptime(entryList['startTime'], "%H:%M:%S") + print("INFO: "+str(timediff.seconds)+" to midnight\033[K",end='\n') + endloop = 0 + while timediff.seconds > 900 and endloop == 0: entryList['id'] = str(int(entryList['id']) + 1) entryList['unix'] = str(time.time()) - entryList['startTimeUnix'] = float(entryList['startTimeUnix']) + 900 - entryList['startTime'] = str(datetime.datetime.fromtimestamp(entryList['startTimeUnix']).strftime("%H:%M:%S")) + prevEndTimeUnix = float(entryList['startTimeUnix']) + the_show[4]/1000 + prevEndTime = datetime.datetime.fromtimestamp(prevEndTimeUnix) + entryList['startTime'] = prevEndTime + (datetime.datetime.min - prevEndTime) % datetime.timedelta(minutes=entryList['timeShift']) + entryList['startTime'] = entryList['startTime'].strftime("%H:%M:%S") + entryList['startTimeUnix'] = time.mktime(time.strptime("2000/01/01 "+entryList['startTime'], "%Y/%m/%d %H:%M:%S")) + print("INFO: "+str(entryList['startTimeUnix'])+" - New Unix Time Start\033[K",end='\n') + print("INFO: "+str(entryList['startTime'])+" - New Start Time\033[K",end='\n') + if entryList['startTime'] == "00:00:00": + entryList['strictTime'] = "true" + else: + entryList['strictTime'] = "secondary" timediff = datetime.datetime.strptime("23:59:59", "%H:%M:%S") - datetime.datetime.strptime(entryList['startTime'], "%H:%M:%S") - durationSplit = entryList['duration'].split(',') - if timediff.seconds < 5399: - maxTime = round(timediff.seconds / 60) - minTime = int(durationSplit[0]) - if minTime > maxTime: - minTime = maxTime - 1 - entryList['duration'] = str(minTime)+','+str(maxTime) - entryList['overlapMax'] = round(int(durationSplit[0]) * 1.5) - table.execute(sql,entryList) + maxMS = timediff.seconds * 1000 + if 0 < int(entryList['endTime'].split(':')[1]) <= 15 or 30 < int(entryList['endTime'].split(':')[1]) <= 45: + maxMS = 15*60*1000 + rndsql = "SELECT * FROM shows WHERE (customSectionName NOT LIKE 'playlist' AND duration BETWEEN ? and ?) ORDER BY RANDOM() LIMIT 1" + table.execute(rndsql, ("60000", str(maxMS))) + the_show = table.fetchone() + entryList['duration'] = str("1,"+str(int(the_show[4]) / 60000)) + entryList['endTime'] = datetime.datetime.fromtimestamp(float(entryList['startTimeUnix']) + the_show[4]/1000).strftime("%H:%M:%S") + entryList['title'] = the_show[3] + entryList['overlapMax'] = round(int(entryList['duration'].split(',')[1]) * 0.5) + print("INFO: Adding "+entryList['startTime']+" - "+entryList['title']+"\033[K",end='\n') + sql = "INSERT INTO schedule(id,unix,mediaID,title,duration,startTime,endTime,dayOfWeek,startTimeUnix,section,strictTime,timeShift,overlapMax,xtra) \ + VALUES(:id,:unix,:mediaID,:title,:duration,:startTime,:endTime,:dayOfWeek,:startTimeUnix,:section,:strictTime,:timeShift,:overlapMax,:xtra)" + if entryList['startTime'] != "00:00:00": + table.execute(sql,entryList) + print("INFO: "+str(timediff.seconds)+" to midnight\033[K",end='\n') + else: + endloop = 1 else: for i in range(0,len(schedule)): sql = "INSERT INTO schedule(id,unix,mediaID,title,duration,startTime,endTime,dayOfWeek,startTimeUnix,section,strictTime,timeShift,overlapMax,xtra) \ diff --git a/requirements.txt b/requirements.txt index 010961a..5b35186 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ billiard certifi chardet httplib2 +GitPython idna kombu oauth2client diff --git a/setup.py b/setup.py index 0c8bafd..40bb9cf 100644 --- a/setup.py +++ b/setup.py @@ -41,6 +41,36 @@ def get_channels(channelsDir='.'): do = "nothing" return channelsList +def generate_daily_schedules(channelsList): + #execute PseudoChannel.py -g in specified channel + for channel in channelsList: + os.chdir('./pseudo-channel_'+channel) + print("GENERATING SCHEDULE FOR CHANNEL "+channel) + process = subprocess.Popen(["python", "-u", "PseudoChannel.py", "-g"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) + while True: + output = process.stdout.readline() + if process.poll() is not None: + break + if output: + print(output.strip()) + rc = process.poll() + os.chdir('../') + +def randomize_episodes(channelsList): + #execute PseudoChannel.py -ep in specified channel + for channel in channelsList: + os.chdir('./pseudo-channel_'+channel) + print("RANDOMIZING STARTING EPISODES FOR ALL SHOWS ON CHANNEL "+channel) + process = subprocess.Popen(["python", "-u", "PseudoChannel.py", "-ep"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) + while True: + output = process.stdout.readline() + if process.poll() is not None: + break + if output: + print(output.strip()) + rc = process.poll() + os.chdir('../') + def ps_install(ps_branch='python3', path='./channels'): dirCheck = os.path.isdir(path) if dirCheck == False: @@ -209,6 +239,9 @@ def ps_install(ps_branch='python3', path='./channels'): shutil.copy('./pseudo_config.py', '../') shutil.copy('./plex_token.py', '../') global_database_update('./') #run database scan + channelsList = get_channels() + randomize_episodes(channelsList) + generate_daily_schedules(channelsList) os.remove('../pseudo_config.py') os.remove('../plex_token.py')