From cba72477be46e2b6cdc19a05d63b6d06de1bdb6c Mon Sep 17 00:00:00 2001 From: jakewaldron Date: Thu, 23 Feb 2017 17:58:27 -0800 Subject: [PATCH] Bug Fixes * Properly close DB connections when done * Copy DB files before opening (delete copy when done with file) to avoid corruption issues * Fixes an issue with the GUID (used for direct links) --- scripts/plexEmail.py | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/scripts/plexEmail.py b/scripts/plexEmail.py index 1f6e69e..f7c77bd 100644 --- a/scripts/plexEmail.py +++ b/scripts/plexEmail.py @@ -25,7 +25,7 @@ from email.utils import formataddr from xml.etree.ElementTree import XML -SCRIPT_VERSION = 'v0.9.1' +SCRIPT_VERSION = 'v0.9.2' def replaceConfigTokens(): ## The below code is for backwards compatibility @@ -887,7 +887,7 @@ def exceptionHandler(type, value, tb): testMode = False if ('test' in args): - logging.info('Test flag found - setting script instance to test mode.') + logging.info('Test flag is set to ' + str(args['test'])) testMode = args['test'] else: logging.info('Test flag not found.') @@ -909,7 +909,7 @@ def exceptionHandler(type, value, tb): plexWebLink = '' if (config['filter_include_plex_web_link']): - if (config['plex_web_server_guid'] == ''): + if (config['plex_web_server_guid'] != ''): plexWebLink = 'http://plex.tv/web/app#!/server/' + config['plex_web_server_guid'] + '/details/%2Flibrary%2Fmetadata%2F' else: logging.info('Including Plex Web Link - Getting machine identifier from the DLNA DB') @@ -918,12 +918,17 @@ def exceptionHandler(type, value, tb): if (os.path.isfile(DLNA_DB_FILE)): try: - con = sqlite3.connect(DLNA_DB_FILE) + shutil.copyfile(DLNA_DB_FILE, DLNA_DB_FILE + '_plexemail') + con = sqlite3.connect(DLNA_DB_FILE + '_plexemail') cur = con.cursor() cur.execute('SELECT machine_identifier FROM remote_servers WHERE url LIKE "http://127.0.0.1%";') for row in cur: plexWebLink = 'http://plex.tv/web/app#!/server/' + row[0] + '/details/%2Flibrary%2Fmetadata%2F' logging.info('plexWebLink = ' + plexWebLink) + cur.close() + del cur + con.close() + os.remove(DLNA_DB_FILE + '_plexemail') except sqlite3.OperationalError: logging.warning(DLNA_DB_FILE + ' is locked or does not have the correct permissions') else: @@ -936,7 +941,8 @@ def exceptionHandler(type, value, tb): print DATABASE_FILE + ' does not exist. Please make sure the plex_data_folder value is correct.' sys.exit() -con = sqlite3.connect(DATABASE_FILE) +shutil.copyfile(DATABASE_FILE, DATABASE_FILE + '_plexemail') +con = sqlite3.connect(DATABASE_FILE + '_plexemail') con.text_factory = str with con: @@ -956,6 +962,9 @@ def exceptionHandler(type, value, tb): libraryFilter += ') ' logging.debug('libraryFilter = ' + libraryFilter) + cur.close() + del cur + dateSearch = 'datetime(\'now\', \'localtime\', \'-' + str(config['date_days_back_to_search']) + ' days\', \'-' + str(config['date_hours_back_to_search']) + ' hours\', \'-' + str(config['date_minutes_back_to_search']) + ' minutes\')' logging.debug('dateSearch for DB query = ' + dateSearch) @@ -970,6 +979,9 @@ def exceptionHandler(type, value, tb): response[row[0]] = {'id': row[0], 'parent_id': row[1], 'metadata_type': row[2], 'title': row[3], 'title_sort': row[4], 'original_title': row[5], 'rating': row[6], 'tagline': row[7], 'summary': row[8], 'content_rating': row[9], 'duration': row[10], 'user_thumb_url': row[11], 'tags_genre': row[12], 'tags_director': row[13], 'tags_star': row[14], 'year': row[15], 'hash': row[16], 'index': row[17], 'studio': row[18], 'real_duration': row[19], 'air_date': row[20]} logging.debug(response[row[0]]) + cur.close() + del cur + emailNotice = '' htmlNotice = '' if (config['msg_notice']): @@ -1205,6 +1217,9 @@ def exceptionHandler(type, value, tb): tvSeasons[season]['parent_hash'] = row[12] tvSeasons[season]['parent_thumb_url'] = row[13] tvSeasons[season]['studio'] = row[14] + + cur2.close() + del cur2 if ('season_sort_3' in config and config['season_sort_3'] != ''): tvSeasons = OrderedDict(sorted(tvSeasons.iteritems(), key=lambda t: t[1][config['season_sort_3']], reverse=config['season_sort_3_reverse'])) @@ -1304,10 +1319,16 @@ def exceptionHandler(type, value, tb): tvEpisodes[episode]['show_thumb_url'] = row2[8] logging.debug('main: show_thumb_url = ' + row2[8]) tvEpisodes[episode]['studio'] = row2[9] + + cur3.close() + del cur3 else: logging.info('main: tvEpisodes[episode][\'parent_id\'] = None') del modifiedTVEpisodes[episode] + cur2.close() + del cur2 + tvEpisodes = dict(modifiedTVEpisodes) if ('episode_sort_3' in config and config['episode_sort_3'] != ''): @@ -1467,6 +1488,9 @@ def exceptionHandler(type, value, tb): albums[album]['parent_thumb_url'] = row[13] albums[album]['studio'] = row[14] + cur2.close() + del cur2 + cur2 = con.cursor() cur2.execute("SELECT MD.id, MD.title, MD.title_sort, MD.original_title, MD.[index], ME.duration, ME.audio_codec FROM metadata_items MD LEFT OUTER JOIN media_items ME ON MD.id = ME.metadata_item_id WHERE parent_id = " + str(albums[album]['id']) + ";") @@ -1485,7 +1509,9 @@ def exceptionHandler(type, value, tb): except TypeError: duration = 'N/A' albums[album]['tracks'][row[4]] = {'id': row[0], 'title': row[1], 'title_sort': row[2], 'original_title': row[3], 'index': row[4], 'duration': duration, 'codec': row[6]} - + + cur2.close() + del cur2 if ('album_sort_3' in config and config['album_sort_3'] != ''): albums = OrderedDict(sorted(albums.iteritems(), key=lambda t: t[1][config['album_sort_3']], reverse=config['album_sort_3_reverse'])) if ('album_sort_2' in config and config['album_sort_2'] != ''): @@ -1711,3 +1737,6 @@ def exceptionHandler(type, value, tb): print 'Emails were not sent because the option is disabled in the config file.' else: print 'Emails were not sent because there were no new additions in the timeframe specified.' + +con.close() +os.remove(DATABASE_FILE + '_plexemail') \ No newline at end of file