Fixed some queries trowing exception when no existing row were found #1425

This commit is contained in:
morpheus65535 2021-06-02 22:29:11 -04:00
parent 57080eda11
commit 8d570fd3b7
3 changed files with 32 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import os
import requests import requests
import logging import logging
from gevent import sleep from gevent import sleep
from peewee import DoesNotExist
from database import get_exclusion_clause, TableEpisodes, TableShows from database import get_exclusion_clause, TableEpisodes, TableShows
from config import settings, url_sonarr from config import settings, url_sonarr
@ -151,10 +152,13 @@ def sync_one_episode(episode_id):
logging.debug('BAZARR syncing this specific episode from Sonarr: {}'.format(episode_id)) logging.debug('BAZARR syncing this specific episode from Sonarr: {}'.format(episode_id))
# Check if there's a row in database for this episode ID # Check if there's a row in database for this episode ID
existing_episode = TableEpisodes.select(TableEpisodes.path)\ try:
.where(TableEpisodes.sonarrEpisodeId == episode_id)\ existing_episode = TableEpisodes.select(TableEpisodes.path)\
.dicts()\ .where(TableEpisodes.sonarrEpisodeId == episode_id)\
.get() .dicts()\
.get()
except DoesNotExist:
existing_episode = None
try: try:
# Get episode data from sonarr api # Get episode data from sonarr api
@ -186,23 +190,23 @@ def sync_one_episode(episode_id):
TableEpisodes.update(episode).where(TableEpisodes.sonarrEpisodeId == episode_id).execute() TableEpisodes.update(episode).where(TableEpisodes.sonarrEpisodeId == episode_id).execute()
event_stream(type='episode', action='update', payload=int(episode_id)) event_stream(type='episode', action='update', payload=int(episode_id))
logging.debug('BAZARR updated this episode into the database:{}'.format(path_mappings.path_replace( logging.debug('BAZARR updated this episode into the database:{}'.format(path_mappings.path_replace(
episode.path))) episode['path'])))
# Insert new episodes in DB # Insert new episodes in DB
elif episode and not existing_episode: elif episode and not existing_episode:
TableEpisodes.insert(episode).on_conflict(action='IGNORE').execute() TableEpisodes.insert(episode).on_conflict(action='IGNORE').execute()
event_stream(type='episode', action='update', payload=int(episode_id)) event_stream(type='episode', action='update', payload=int(episode_id))
logging.debug('BAZARR inserted this episode into the database:{}'.format(path_mappings.path_replace( logging.debug('BAZARR inserted this episode into the database:{}'.format(path_mappings.path_replace(
episode.path))) episode['path'])))
# Storing existing subtitles # Storing existing subtitles
logging.debug('BAZARR storing subtitles for this episode: {}'.format(path_mappings.path_replace( logging.debug('BAZARR storing subtitles for this episode: {}'.format(path_mappings.path_replace(
episode.path))) episode['path'])))
store_subtitles(episode.path, path_mappings.path_replace(episode.path)) store_subtitles(episode['path'], path_mappings.path_replace(episode['path']))
# Downloading missing subtitles # Downloading missing subtitles
logging.debug('BAZARR downloading missing subtitles for this episode: {}'.format(path_mappings.path_replace( logging.debug('BAZARR downloading missing subtitles for this episode: {}'.format(path_mappings.path_replace(
episode.path))) episode['path'])))
episode_download_subtitles(episode_id) episode_download_subtitles(episode_id)

View File

@ -6,6 +6,7 @@ import logging
import operator import operator
from functools import reduce from functools import reduce
from gevent import sleep from gevent import sleep
from peewee import DoesNotExist
from config import settings, url_radarr from config import settings, url_radarr
from helper import path_mappings from helper import path_mappings
@ -172,7 +173,13 @@ def update_one_movie(movie_id, action):
logging.debug('BAZARR syncing this specific movie from Radarr: {}'.format(movie_id)) logging.debug('BAZARR syncing this specific movie from Radarr: {}'.format(movie_id))
# Check if there's a row in database for this movie ID # Check if there's a row in database for this movie ID
existing_movie = TableMovies.get_or_none(TableMovies.radarrId == movie_id) try:
existing_movie = TableMovies.select(TableMovies.path)\
.where(TableMovies.radarrId == movie_id)\
.dicts()\
.get()
except DoesNotExist:
existing_movie = None
# Remove movie from DB # Remove movie from DB
if action == 'deleted': if action == 'deleted':
@ -180,7 +187,7 @@ def update_one_movie(movie_id, action):
TableMovies.delete().where(TableMovies.radarrId == movie_id).execute() TableMovies.delete().where(TableMovies.radarrId == movie_id).execute()
event_stream(type='movie', action='delete', payload=int(movie_id)) event_stream(type='movie', action='delete', payload=int(movie_id))
logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie( logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie(
existing_movie.path))) existing_movie['path'])))
return return
radarr_version = get_radarr_version() radarr_version = get_radarr_version()
@ -225,7 +232,7 @@ def update_one_movie(movie_id, action):
TableMovies.delete().where(TableMovies.radarrId == movie_id).execute() TableMovies.delete().where(TableMovies.radarrId == movie_id).execute()
event_stream(type='movie', action='delete', payload=int(movie_id)) event_stream(type='movie', action='delete', payload=int(movie_id))
logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie( logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie(
existing_movie.path))) existing_movie['path'])))
return return
# Update existing movie in DB # Update existing movie in DB

View File

@ -4,6 +4,7 @@ import os
import requests import requests
import logging import logging
from gevent import sleep from gevent import sleep
from peewee import DoesNotExist
from config import settings, url_sonarr from config import settings, url_sonarr
from list_subtitles import list_missing_subtitles from list_subtitles import list_missing_subtitles
@ -137,7 +138,13 @@ def update_one_series(series_id, action):
logging.debug('BAZARR syncing this specific series from RSonarr: {}'.format(series_id)) logging.debug('BAZARR syncing this specific series from RSonarr: {}'.format(series_id))
# Check if there's a row in database for this series ID # Check if there's a row in database for this series ID
existing_series = TableShows.get_or_none(TableShows.sonarrSeriesId == series_id) try:
existing_series = TableShows.select(TableShows.path)\
.where(TableShows.sonarrSeriesId == series_id)\
.dicts()\
.get()
except DoesNotExist:
existing_series = None
sonarr_version = get_sonarr_version() sonarr_version = get_sonarr_version()
serie_default_enabled = settings.general.getboolean('serie_default_enabled') serie_default_enabled = settings.general.getboolean('serie_default_enabled')
@ -184,7 +191,7 @@ def update_one_series(series_id, action):
TableShows.delete().where(TableShows.sonarrSeriesId == series_id).execute() TableShows.delete().where(TableShows.sonarrSeriesId == series_id).execute()
event_stream(type='series', action='delete', payload=int(series_id)) event_stream(type='series', action='delete', payload=int(series_id))
logging.debug('BAZARR deleted this series from the database:{}'.format(path_mappings.path_replace( logging.debug('BAZARR deleted this series from the database:{}'.format(path_mappings.path_replace(
existing_series.path))) existing_series['path'])))
return return
# Update existing series in DB # Update existing series in DB