diff --git a/sickbeard/databases/mainDB.py b/sickbeard/databases/mainDB.py index d8fa2f441cf1be191db289e4b4774faad0764098..68464f6936c418534367e9bbda16f5df5ab5cd88 100644 --- a/sickbeard/databases/mainDB.py +++ b/sickbeard/databases/mainDB.py @@ -365,19 +365,46 @@ class AddSizeAndSceneNameFields(SetNzbTorrentSettings): if not self.hasColumn("tv_episodes", "file_size"): self.addColumn("tv_episodes", "file_size") - if not self.hasColumn("tv_episodes", "original_name"): - self.addColumn("tv_episodes", "original_name", "TEXT", "") + if not self.hasColumn("tv_episodes", "release_name"): + self.addColumn("tv_episodes", "release_name", "TEXT", "") - ep_results = self.connection.select("SELECT episode_id, location FROM tv_episodes") + ep_results = self.connection.select("SELECT episode_id, location, file_size FROM tv_episodes") for cur_ep in ep_results: - if ek.ek(os.path.isfile, cur_ep["location"]): + # if there is no size yet then populate it for us + if not int(cur_ep["file_size"]) and ek.ek(os.path.isfile, cur_ep["location"]): cur_size = ek.ek(os.path.getsize, cur_ep["location"]) self.connection.action("UPDATE tv_episodes SET file_size = ? WHERE episode_id = ?", [cur_size, int(cur_ep["episode_id"])]) - self.incDBVersion() + history_results = self.connection.select("SELECT * FROM history WHERE provider = -1") -class SetNzbTorrentSettings(PopulateRootDirs): + for cur_result in history_results: + # find the associated snatch + snatch_results = self.connection.select("SELECT resource, quality FROM history WHERE provider != -1 AND showid = ? AND season = ? AND episode = ? AND date < ?", + [cur_result["showid"], cur_result["season"], cur_result["episode"], cur_result["date"]]) + if not snatch_results: + continue + + # find the associated episode on disk + ep_results = self.connection.select("SELECT episode_id, status FROM tv_episodes WHERE showid = ? AND season = ? AND episode = ?", + [cur_result["showid"], cur_result["season"], cur_result["episode"]]) + if not ep_results: + continue + + # get the status/quality of the existing ep and make sure it's what we expect + ep_status, ep_quality = common.Quality.splitCompositeStatus(int(ep_results[0]["status"])) + if ep_status != common.DOWNLOADED: + continue + + if ep_quality != int(snatch_results[0]["quality"]): + continue + + # if all is well by here we'll just put the release name into the database + self.connection.action("UPDATE tv_episodes SET release_name = ? WHERE episode_id = ?", [snatch_results[0]["resource"], ep_results[0]["episode_id"]]) + + + #self.incDBVersion() + class SetNzbTorrentSettings(PopulateRootDirs): def test(self): return self.checkDBVersion() >= 8 diff --git a/sickbeard/postProcessor.py b/sickbeard/postProcessor.py index a02ac041144b96be9343c1e4f266b2a82c437d12..0f708a6ce28666a7d37305bed7e4681f90136e2b 100755 --- a/sickbeard/postProcessor.py +++ b/sickbeard/postProcessor.py @@ -70,6 +70,7 @@ class PostProcessor(object): self.in_history = False self.release_group = None self.is_proper = False + self.good_folder = False self.log = '' @@ -339,8 +340,15 @@ class PostProcessor(object): def _finalize(parse_result): self.release_group = parse_result.release_group + + # remember whether it's a proper if parse_result.extra_info: self.is_proper = re.search('(^|[\. _-])(proper|repack)([\. _-]|$)', parse_result.extra_info, re.I) != None + + # if we are doing the folder name and the folder is good then remember that for later + if name == self.folder_name: + if parse_result.series_name and parse_result.season_number != None and parse_result.episode_numbers and parse_result.release_group: + self.good_folder = True # for each possible interpretation of that scene name for cur_name in name_list: @@ -695,7 +703,13 @@ class PostProcessor(object): # put the new location in the database for cur_ep in [ep_obj] + ep_obj.relatedEps: with cur_ep.lock: - cur_ep.original_name = ek.ek(os.path.join, self.folder_name, self.file_name) + # use the best possible representation of the release name + if self.nzb_name: + cur_ep.release_name = self.nzb_name + elif self.good_folder: + cur_ep.release_name = self.folder_name + else: + cur_ep.release_name = self.file_name cur_ep.location = ek.ek(os.path.join, dest_path, new_file_name) cur_ep.saveToDB() diff --git a/sickbeard/tv.py b/sickbeard/tv.py index 2536fa1fcd8cb839fdc1f5af0b46666a6dd9fa5d..a2f08116f64dc4a2d5397631135717cc90fbc5b3 100644 --- a/sickbeard/tv.py +++ b/sickbeard/tv.py @@ -452,7 +452,7 @@ class TVShow(object): # if it's a new file then if not same_file: - curEp.original_name = '' + curEp.release_name = '' # if they replace a file on me I'll make some attempt at re-checking the quality unless I know it's the same file if checkQualityAgain and not same_file: @@ -959,7 +959,7 @@ class TVEpisode(object): self._status = UNKNOWN self._tvdbid = 0 self._file_size = 0 - self._original_name = 0 + self._release_name = '' # setting any of the above sets the dirty flag self.dirty = True @@ -985,19 +985,19 @@ class TVEpisode(object): status = property(lambda self: self._status, dirty_setter("_status")) tvdbid = property(lambda self: self._tvdbid, dirty_setter("_tvdbid")) #location = property(lambda self: self._location, dirty_setter("_location")) - file_size = property(lambda self: self._size, dirty_setter("_file_size")) - original_name = property(lambda self: self._size, dirty_setter("_original_name")) + file_size = property(lambda self: self._file_size, dirty_setter("_file_size")) + release_name = property(lambda self: self._release_name, dirty_setter("_release_name")) def _set_location(self, new_location): logger.log(u"Setter sets location to " + new_location, logger.DEBUG) #self._location = newLocation - dirty_setter("_location")(new_location) + dirty_setter("_location")(self, new_location) if new_location and ek.ek(os.path.isfile, new_location): - self.size = ek.ek(os.path.getsize, new_location) + self.file_size = ek.ek(os.path.getsize, new_location) else: - self.size = 0 + self.file_size = 0 location = property(lambda self: self._location, _set_location) @@ -1086,7 +1086,7 @@ class TVEpisode(object): # don't overwrite my location if sqlResults[0]["location"] != "" and sqlResults[0]["location"] != None: self.location = os.path.normpath(sqlResults[0]["location"]) - self.size = int(sqlResults[0]["size"]) + self.file_size = int(sqlResults[0]["file_size"]) self.tvdbid = int(sqlResults[0]["tvdbid"]) @@ -1366,7 +1366,7 @@ class TVEpisode(object): "hastbn": self.hastbn, "status": self.status, "location": self.location, - "size": self.size} + "file_size": self.file_size} controlValueDict = {"showid": self.show.tvdbid, "season": self.season, "episode": self.episode}