diff --git a/video_metadata/views/tmdb.py b/video_metadata/views/tmdb.py index 8fbafde..ab008ff 100644 --- a/video_metadata/views/tmdb.py +++ b/video_metadata/views/tmdb.py @@ -404,7 +404,9 @@ def storeTMDBQueryResults(query_obj, result_list): cur = conn.cursor() query_id = 0 - current_app.logger.debug('adding query string "{}" into local cache'.format(query_obj.parsed_query_str)) + current_app.logger.debug(f'{util.getFunctionName()}, adding query string ' + f'"{query_obj.parsed_query_str}" into local cache') + #current_app.logger.debug(f'{util.getFunctionName()}, result_list:\n{result_list} ') try: cur.execute('BEGIN TRANSACTION') @@ -416,7 +418,18 @@ def storeTMDBQueryResults(query_obj, result_list): query_obj.errors.append(e) for result in result_list: + # NOTE: check stored details before trying to add to local db to avoid unique key + # constraint error + cur.execute('SELECT COUNT(*) FROM movie_details WHERE movie_id=?', (result.id,)) + count = cur.fetchone()[0] + if count > 0: + current_app.logger.debug(f'{util.getFunctionName()}, ' + f'skipping result that is already saved. id: {result.id}, title: {result.title}' + ) + break + try: + current_app.logger.debug(f'{util.getFunctionName()}, adding movie details for "{result.title}"') cur.execute("INSERT INTO movie_details(" + "movie_id, title, overview, release_date, original_language, poster_path)" + " VALUES (?,?,?,?,?,?)", @@ -428,7 +441,7 @@ def storeTMDBQueryResults(query_obj, result_list): + " VALUES (?,?)", (query_id, result.id) ) except DatabaseError as e: - query_obj.errors.append(e) + query_obj.errors.append(f'{util.getFunctionName()}, database error \'{e}\'') if len(query_obj.errors) == 0: conn.commit()