Commit 69fda28a authored by echel0n's avatar echel0n

Fixed IndexError when deleting episode from show episode cache.

Added `no_create` arg to TVShow class method `get_episode`, prevents TVShow class method `wanted_episode` from creating TVEpisode objects.
Added cleanup code for cache database scene names table to remove dupes.
parent 42caea54
......@@ -49,7 +49,20 @@ class CacheDB(SRDatabase):
else:
found.append(x.provider)
def remove_duplicates_from_scene_name_table():
found = []
session = self.session()
for x in session.query(CacheDB.SceneName).all():
if (x.indexer_id, x.name) in found:
x.delete()
session.commit()
else:
found.append((x.indexer_id, x.name))
remove_duplicates_from_last_search_table()
remove_duplicates_from_scene_name_table()
class LastUpdate(CacheDBBase):
__tablename__ = 'last_update'
......
......@@ -22,6 +22,7 @@
import datetime
import os
import re
import threading
import traceback
from collections import OrderedDict
from xml.etree.ElementTree import ElementTree
......@@ -48,6 +49,8 @@ from sickrage.subtitles import Subtitles
class TVEpisode(object):
def __init__(self, showid, indexer, season, episode, location=''):
self.lock = threading.Lock()
with sickrage.app.main_db.session() as session:
try:
query = session.query(MainDB.TVEpisode).filter_by(showid=showid, indexer=indexer, season=season, episode=episode).one()
......@@ -61,7 +64,10 @@ class TVEpisode(object):
'location': location
}))
session.commit()
try:
session.commit()
except Exception as e:
pass
query = session.query(MainDB.TVEpisode).filter_by(showid=showid, indexer=indexer, season=season, episode=episode).one()
self._data_local = query.as_dict()
......@@ -665,10 +671,9 @@ class TVEpisode(object):
# delete myself from show episode cache
try:
ep_index = self.show.episodes.index(self)
sickrage.app.log.debug("Deleting %s S%02dE%02d from the shows episode cache" % (self.show.name, self.season or 0, self.episode or 0))
del self.show.episodes[ep_index]
except ValueError:
del self.show.episodes[self.show.episodes.index(self)]
except (IndexError, ValueError) as e:
pass
# delete myself from the database
......
......@@ -25,6 +25,7 @@ import os
import re
import shutil
import stat
import threading
import traceback
import send2trash
......@@ -48,6 +49,7 @@ from sickrage.indexers.exceptions import indexer_attributenotfound
class TVShow(object):
def __init__(self, indexer_id, indexer, lang='en', location=''):
self.lock = threading.Lock()
self._episodes = []
with sickrage.app.main_db.session() as session:
......@@ -630,7 +632,7 @@ class TVShow(object):
return scanned_eps
def get_episode(self, season=None, episode=None, absolute_number=None):
def get_episode(self, season=None, episode=None, absolute_number=None, no_create=False):
try:
if season is None and episode is None and absolute_number is not None:
with sickrage.app.main_db.session() as session:
......@@ -643,6 +645,8 @@ class TVShow(object):
if tv_episode.season == season and tv_episode.episode == episode:
return tv_episode
else:
if no_create:
raise EpisodeNotFoundException
tv_episode = TVEpisode(showid=self.indexer_id, indexer=self.indexer, season=season, episode=episode)
self._episodes.append(tv_episode)
return tv_episode
......@@ -1118,7 +1122,7 @@ class TVShow(object):
def want_episode(self, season, episode, quality, manualSearch=False, downCurQuality=False):
try:
episode_object = self.get_episode(season, episode)
episode_object = self.get_episode(season, episode, no_create=True)
except EpisodeNotFoundException:
sickrage.app.log.debug("Unable to find a matching episode in database, ignoring found episode")
return False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment