From 09476224c58bf49ad1f9a8e1602306633b40f1e4 Mon Sep 17 00:00:00 2001 From: echel0n Date: Fri, 2 May 2014 03:09:17 -0700 Subject: [PATCH] Improved code for better performance of application memory and cpu usage. --- sickbeard/failedProcessor.py | 32 ++------------ sickbeard/name_parser/parser.py | 4 +- sickbeard/nzbSplitter.py | 2 +- sickbeard/properFinder.py | 32 ++++++++------ sickbeard/show_name_helpers.py | 8 +++- sickbeard/tv.py | 74 ++++++--------------------------- 6 files changed, 46 insertions(+), 106 deletions(-) diff --git a/sickbeard/failedProcessor.py b/sickbeard/failedProcessor.py index 32a41391..2ab36bee 100644 --- a/sickbeard/failedProcessor.py +++ b/sickbeard/failedProcessor.py @@ -66,15 +66,9 @@ class FailedProcessor(object): logger.log(u" - " + str(parsed.extra_info), logger.DEBUG) logger.log(u" - " + str(parsed.release_group), logger.DEBUG) logger.log(u" - " + str(parsed.air_date), logger.DEBUG) + logger.log(u" - " + str(parsed.sports_event_date), logger.DEBUG) - show_id = self._get_show_id(parsed.series_name) - if show_id is None: - self._log(u"Warning: couldn't find show ID", logger.WARNING) - raise exceptions.FailedProcessingFailed() - - self._log(u"Found show_id: " + str(show_id), logger.DEBUG) - - self._show_obj = helpers.findCertainShow(sickbeard.showList, show_id) + self._show_obj = parsed.show if self._show_obj is None: self._log( u"Could not create show object. Either the show hasn't been added to SickBeard, or it's still loading (if SB was restarted recently)", @@ -90,24 +84,4 @@ class FailedProcessor(object): def _log(self, message, level=logger.MESSAGE): """Log to regular logfile and save for return for PP script log""" logger.log(message, level) - self.log += message + "\n" - - def _get_show_id(self, series_name): - """Find and return show ID by searching exceptions, then DB""" - - show_names = show_name_helpers.sceneToNormalShowNames(series_name) - - logger.log(u"show_names: " + str(show_names), logger.DEBUG) - - for show_name in show_names: - exception = scene_exceptions.get_scene_exception_by_name(show_name) - if exception is not None: - return exception - - for show_name in show_names: - found_info = helpers.get_show_by_name(show_name) - if found_info is not None: - return (found_info.indexerid) - - return None - + self.log += message + "\n" \ No newline at end of file diff --git a/sickbeard/name_parser/parser.py b/sickbeard/name_parser/parser.py index 357dacae..98989c18 100644 --- a/sickbeard/name_parser/parser.py +++ b/sickbeard/name_parser/parser.py @@ -126,7 +126,9 @@ class NameParser(object): result.episode_numbers = [ep_num] if 'sports_event_id' in named_groups: - result.sports_event_id = int(match.group('sports_event_id')) + sports_event_id = match.group('sports_event_id') + if sports_event_id: + result.sports_event_id = int(match.group('sports_event_id')) if 'sports_event_name' in named_groups: result.sports_event_name = match.group('sports_event_name') diff --git a/sickbeard/nzbSplitter.py b/sickbeard/nzbSplitter.py index 24451ec2..35f33ff7 100644 --- a/sickbeard/nzbSplitter.py +++ b/sickbeard/nzbSplitter.py @@ -133,7 +133,7 @@ def splitResult(result): # parse the name try: np = NameParser(False) - parse_result = np.parse(newNZB) + parse_result = np.parse(newNZB).convert() except InvalidNameException: logger.log(u"Unable to parse the filename " + newNZB + " into a valid episode", logger.WARNING) return False diff --git a/sickbeard/properFinder.py b/sickbeard/properFinder.py index 7438182f..dacfc736 100644 --- a/sickbeard/properFinder.py +++ b/sickbeard/properFinder.py @@ -105,6 +105,12 @@ class ProperFinder(): logger.log(u"Unable to parse the filename " + curProper.name + " into a valid episode", logger.DEBUG) continue + # if we can't find the show then there's nothing we can really do + if not parse_result.show: + logger.log(u"This show isn't in your list, skipping ...", + logger.DEBUG) + continue + if not parse_result.episode_numbers: logger.log( u"Ignoring " + curProper.name + " because it's for a full season rather than specific episode", @@ -178,20 +184,20 @@ class ProperFinder(): continue # if we have an air-by-date show then get the real season/episode numbers - if curProper.season == -1 and curProper.indexerid: - indexer_lang = showObj.lang - lINDEXER_API_PARMS = sickbeard.indexerApi(showObj.indexer).api_params.copy() - if indexer_lang and not indexer_lang == 'en': - lINDEXER_API_PARMS['language'] = indexer_lang + if curProper.season == -1 and curProper.indexerid and curProper.indexer: + logger.log( + u"Looks like this is an air-by-date or sports show, attempting to convert the date to season/episode", + logger.DEBUG) + airdate = curProper.episode.toordinal() + myDB = db.DBConnection() + sql_result = myDB.select( + "SELECT season, episode FROM tv_episodes WHERE showid = ? and indexer = ? and airdate = ?", + [curProper.indexerid, curProper.indexer, airdate]) - try: - t = sickbeard.indexerApi(showObj.indexer).indexer(**lINDEXER_API_PARMS) - - epObj = t[curProper.indexerid].airedOn(curProper.episode)[0] - - curProper.season = int(epObj["seasonnumber"]) - curProper.episodes = [int(epObj["episodenumber"])] - except sickbeard.indexer_episodenotfound: + if sql_result: + curProper.season = int(sql_result[0][0]) + curProper.episodes = [int(sql_result[0][1])] + else: logger.log(u"Unable to find episode with date " + str( curProper.episode) + " for show " + parse_result.series_name + ", skipping", logger.WARNING) continue diff --git a/sickbeard/show_name_helpers.py b/sickbeard/show_name_helpers.py index f4f62165..3953936f 100644 --- a/sickbeard/show_name_helpers.py +++ b/sickbeard/show_name_helpers.py @@ -47,11 +47,17 @@ def filterBadReleases(name): try: fp = NameParser() - fp.parse(name) + parse_result = fp.parse(name) except InvalidNameException: logger.log(u"Unable to parse the filename " + name + " into a valid episode", logger.WARNING) return False + # if we can't find the show then there's nothing we can really do + if not parse_result.show: + logger.log(u"This show isn't in your list, skipping ...", + logger.DEBUG) + return False + # if any of the bad strings are in the name then say no if sickbeard.IGNORE_WORDS: resultFilters.extend(sickbeard.IGNORE_WORDS.split(',')) diff --git a/sickbeard/tv.py b/sickbeard/tv.py index 86b8ef73..dca2a75c 100644 --- a/sickbeard/tv.py +++ b/sickbeard/tv.py @@ -312,45 +312,6 @@ class TVShow(object): parse_result = None curEpisode = None - if sceneConvert: - try: - i = 0 - while i < 2: - i+=1 - np = NameParser(False) - parse_result = np.parse(mediaFile, True if i > 1 else False) - if helpers.validateShow(self, parse_result.season_number, parse_result.episode_numbers[0]): - ep = TVEpisode(self, parse_result.season_number, parse_result.episode_numbers[0]) - proper_path = ep.proper_path() - proper_absolute_path = ek.ek(os.path.join, self._location, proper_path) - src_path = ek.ek(os.path.dirname, mediaFile) - dest_path = ek.ek(os.path.dirname, proper_absolute_path) - orig_extension = mediaFile.rpartition('.')[-1] - new_base_name = ek.ek(os.path.basename, proper_path) - - new_file_name = new_base_name + '.' + orig_extension - old_file_name = ek.ek(os.path.basename, mediaFile) - new_mediaFile = os.path.join(dest_path, new_file_name) - if old_file_name == new_file_name or os.path.exists(new_mediaFile):break - - if os.path.exists(os.path.join(src_path, old_file_name)): - old_mediaFile = os.path.join(src_path, old_file_name) - elif os.path.exists(os.path.join(dest_path, old_file_name)): - old_mediaFile = os.path.join(dest_path, old_file_name) - else:break - - logger.log(u"Scene Converting file %s to %s" % (old_file_name, new_file_name), logger.MESSAGE) - if not os.path.exists(dest_path): - shutil.move(src_path, dest_path) - shutil.move(old_mediaFile, new_mediaFile) - mediaFile = new_mediaFile - - # converted - break - - except Exception: - continue - logger.log(str(self.indexerid) + u": Creating episode from " + mediaFile, logger.DEBUG) try: curEpisode = self.makeEpFromFile(ek.ek(os.path.join, self._location, mediaFile)) @@ -562,32 +523,23 @@ class TVShow(object): rootEp = None # if we have an air-by-date show then get the real season/episode numbers - if parse_result.air_by_date or parse_result.sports: - try: - lINDEXER_API_PARMS = sickbeard.indexerApi(self.indexer).api_params.copy() + if parse_result.air_by_date: + logger.log( + u"Looks like this is an air-by-date or sports show, attempting to convert the date to season/episode", + logger.DEBUG) + airdate = parse_result.air_date.toordinal() + myDB = db.DBConnection() + sql_result = myDB.select( + "SELECT season, episode FROM tv_episodes WHERE showid = ? and indexer = ? and airdate = ?", + [self.indexerid, self.indexer, airdate]) - if self.lang: - lINDEXER_API_PARMS['language'] = self.lang - - if self.dvdorder != 0: - lINDEXER_API_PARMS['dvdorder'] = True - - t = sickbeard.indexerApi(self.indexer).indexer(**lINDEXER_API_PARMS) - - epObj = None - if parse_result.air_by_date: - epObj = t[self.indexerid].airedOn(parse_result.air_date)[0] - - season = int(epObj["seasonnumber"]) - episodes = [int(epObj["episodenumber"])] - except sickbeard.indexer_episodenotfound: + if sql_result: + season = int(sql_result[0][0]) + episodes = [int(sql_result[0][1])] + else: logger.log(u"Unable to find episode with date " + str( parse_result.air_date) + " for show " + self.name + ", skipping", logger.WARNING) return None - except sickbeard.indexer_error, e: - logger.log(u"Unable to contact " + sickbeard.indexerApi(self.indexer).name + ": " + ex(e), - logger.WARNING) - return None for curEpNum in episodes: