added exception handling to database update

Error handling now exists to prevent a corrupted database from being copied across the channel directories in case of an update failure. Script will now exit without copying files if the database update does not complete.
This commit is contained in:
Moe Fwacky
2022-11-27 14:32:03 -08:00
committed by GitHub
parent 75638261b4
commit ed3bd99854

View File

@@ -42,7 +42,7 @@ else:
update_call = "python3 PseudoChannel.py %s" % update_flags
halt = True
# Step ONE: Global database update
os.chdir(os.path.abspath(os.path.dirname(__file__)))
@@ -53,13 +53,20 @@ except OSError:
pass
try:
os.system(update_call)
halt = False
except:
print("ERROR: Global Update Failed!")
print(e)
try:
os.remove("pseudo-channel.db")
halt = True
except:
print("DATABASE FILE NOT FOUND, RESTORING BACKUP")
os.rename("pseudo-channel.bak", "pseudo-channel.db")
sys.exit()
if halt != True:
base_dirA = os.path.dirname(os.path.abspath(__file__))
locations = "pseudo-channel"+channel_dir_increment_symbol
channel_dirs = [ item for item in os.listdir('.') if os.path.isdir(os.path.join('.', item)) ]
@@ -128,8 +135,8 @@ for channel_dir in channel_dirs:
entryList = {}
entryList['id'] = "1"
entryList['unix'] = str(time.time())
entryList['mediaID'] = "23"
rndsql = "SELECT * FROM shows WHERE (customSectionName NOT LIKE 'playlist' AND duration BETWEEN 6000 and 999000000) ORDER BY RANDOM() LIMIT 1"
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)))
@@ -164,10 +171,7 @@ for channel_dir in channel_dirs:
while timediff.seconds > 900 and endloop == 0:
entryList['id'] = str(int(entryList['id']) + 1)
entryList['unix'] = str(time.time())
try:
prevEndTimeUnix = float(entryList['startTimeUnix']) + the_show[4]/1000
except:
prevEndTimeUnix = float(entryList['startTimeUnix'])
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")
@@ -185,7 +189,6 @@ for channel_dir in channel_dirs:
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()
try:
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]
@@ -193,8 +196,6 @@ for channel_dir in channel_dirs:
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,rerun,year,genres,actors,collections,rating,studio,seasonEpisode) \
VALUES(:id,:unix,:mediaID,:title,:duration,:startTime,:endTime,:dayOfWeek,:startTimeUnix,:section,:strictTime,:timeShift,:overlapMax,:xtra,:rerun,:year,:genres,:actors,:collections,:rating,:studio,:seasonEpisode)"
except:
timediff = timediff + datetime.timedelta(0,900)
if entryList['startTime'] != "00:00:00":
table.execute(sql,entryList)
print("INFO: "+str(timediff.seconds)+" to midnight\033[K",end='\n')
@@ -227,14 +228,11 @@ for channel_dir in channel_dirs:
# Step FIVE: Remove any media not in the directories set of commerical archives
print("NOTICE: Trimming database at " + db_path)
os.system('python report_MediaFolders.py')
try:
local_commercials = open('Commercial_Libraries.txt').read().splitlines()
commercial_removal = [x for x in global_commercials["Commercials"] if x not in local_commercials]
except:
print("ERROR: Commercials Not Found")
local_movies = open('Movie_Libraries.txt').read().splitlines()
local_tvs = open('TV_Libraries.txt').read().splitlines()
commercial_removal = [x for x in global_commercials["Commercials"] if x not in local_commercials]
movie_removal = [x for x in global_commercials["Movies"] if x not in local_movies]
tv_removal = [x for x in global_commercials["TV Shows"] if x not in local_tvs]
@@ -263,3 +261,5 @@ for channel_dir in channel_dirs:
print("NOTICE: " + db_path + " complete! Going to next file")
print("NOTICE: Global update COMPLETE")
else:
print("ERROR: Global update FAILED!")