diff --git a/sickbeard/manual_snatch.py b/sickbeard/manual_snatch.py index 1b79c92529..e5c6023250 100644 --- a/sickbeard/manual_snatch.py +++ b/sickbeard/manual_snatch.py @@ -90,7 +90,7 @@ def getEpisodes(searchThread, searchstatus): logger.log(u'No Show Object found for show with indexerID: {}'.format(searchThread.show.indexerid), logger.ERROR) return results - if isinstance(searchThread, (sickbeard.search_queue.ManualSearchQueueItem, sickbeard.search_queue.ManualSnatchQueueItem)): + if isinstance(searchThread, sickbeard.search_queue.ManualSearchQueueItem): results.append({ 'show': searchThread.show.indexerid, 'episode': searchThread.segment.episode, @@ -140,11 +140,11 @@ def collectEpisodesFromSearchThread(show): # Finished Searches searchstatus = SEARCH_STATUS_FINISHED for searchThread in sickbeard.search_queue.MANUAL_SEARCH_HISTORY: - if show is not None: + if show: if not str(searchThread.show.indexerid) == show: continue - if isinstance(searchThread, (sickbeard.search_queue.ManualSearchQueueItem, sickbeard.search_queue.ManualSnatchQueueItem)): + if isinstance(searchThread, sickbeard.search_queue.ManualSearchQueueItem): if not [x for x in episodes if x['episodeindexid'] == searchThread.segment.indexerid]: episodes += getEpisodes(searchThread, searchstatus) else: diff --git a/sickbeard/search_queue.py b/sickbeard/search_queue.py index 424947d6fb..d48336042e 100644 --- a/sickbeard/search_queue.py +++ b/sickbeard/search_queue.py @@ -56,20 +56,20 @@ def is_in_queue(self, show, segment): def is_ep_in_queue(self, segment): for cur_item in self.queue: - if isinstance(cur_item, (ManualSearchQueueItem, ManualSnatchQueueItem, FailedQueueItem)) and cur_item.segment == segment: + if isinstance(cur_item, (ManualSearchQueueItem, FailedQueueItem)) and cur_item.segment == segment: return True return False def is_show_in_queue(self, show): for cur_item in self.queue: - if isinstance(cur_item, (ManualSearchQueueItem, ManualSnatchQueueItem, FailedQueueItem)) and cur_item.show.indexerid == show: + if isinstance(cur_item, (ManualSearchQueueItem, FailedQueueItem)) and cur_item.show.indexerid == show: return True return False def get_all_ep_from_queue(self, show): ep_obj_list = [] for cur_item in self.queue: - if isinstance(cur_item, (ManualSearchQueueItem, ManualSnatchQueueItem, FailedQueueItem)) and str(cur_item.show.indexerid) == show: + if isinstance(cur_item, (ManualSearchQueueItem, FailedQueueItem)) and str(cur_item.show.indexerid) == show: ep_obj_list.append(cur_item) return ep_obj_list @@ -85,7 +85,7 @@ def is_backlog_paused(self): def is_manualsearch_in_progress(self): # Only referenced in webserve.py, only current running manualsearch or failedsearch is needed!! - if isinstance(self.currentItem, (ManualSearchQueueItem, ManualSnatchQueueItem, FailedQueueItem)): + if isinstance(self.currentItem, (ManualSearchQueueItem, FailedQueueItem)): return True return False @@ -123,7 +123,10 @@ def add_item(self, item): elif isinstance(item, BacklogQueueItem) and not self.is_in_queue(item.show, item.segment): # backlog searches generic_queue.GenericQueue.add_item(self, item) - elif isinstance(item, (ManualSearchQueueItem, ManualSnatchQueueItem, FailedQueueItem)) and not self.is_ep_in_queue(item.segment): + elif isinstance(item, (ManualSearchQueueItem, FailedQueueItem)) and not self.is_ep_in_queue(item.segment): + # manual and failed searches + generic_queue.GenericQueue.add_item(self, item) + elif isinstance(item, ManualSnatchQueueItem): # manual and failed searches generic_queue.GenericQueue.add_item(self, item) else: @@ -137,7 +140,6 @@ def __init__(self): self.success = None self.started = None - def run(self): generic_queue.QueueItem.run(self) self.started = True @@ -188,7 +190,6 @@ def __init__(self, show, segment, downCurQuality=False, manual_snatch=False): self.downCurQuality = downCurQuality self.manual_snatch = manual_snatch - def run(self): generic_queue.QueueItem.run(self) self.started = True @@ -201,10 +202,11 @@ def run(self): if not self.manual_snatch and searchResult: # just use the first result for now if searchResult[0].seeders not in (-1, None) and searchResult[0].leechers not in (-1, None): - logger.log(u"Downloading {0} with {1} seeders and {2} leechers from {3}".format(searchResult[0].name, - searchResult[0].seeders, searchResult[0].leechers, searchResult[0].provider.name)) + logger.log(u"Downloading {0} with {1} seeders and {2} leechers from {3}". + format(searchResult[0].name, + searchResult[0].seeders, searchResult[0].leechers, searchResult[0].provider.name)) else: - logger.log(u"Downloading {0} from {1}".format(searchResult[0].name, searchResult[0].provider.name)) + logger.log(u"Downloading {0} from {1}".format(searchResult[0].name, searchResult[0].provider.name)) self.success = search.snatchEpisode(searchResult[0]) # give the CPU a break @@ -230,57 +232,34 @@ def run(self): self.finish() + class ManualSnatchQueueItem(generic_queue.QueueItem): - def __init__(self, show, segment, season, episode, url, quality, provider, search_name, seeders, leechers): + def __init__(self, searchResult): generic_queue.QueueItem.__init__(self, u'Manual Snatch', MANUAL_SNATCH) self.priority = generic_queue.QueuePriorities.HIGH self.success = None self.started = None self.results = None - - self.show = show - self.segment = segment - self.season = season - self.episode = episode - self.url = url - self.quality = int(quality) - self.provider = providers.getProviderClass(GenericProvider.make_id(provider)) - self.search_name = search_name - self.seeders = seeders - self.leechers = leechers - + self.searchResult = searchResult def run(self): generic_queue.QueueItem.run(self) self.started = True try: - logger.log(u"Beginning custom manual search for: [" + self.segment.prettyName() + "]") - - # Build a valid result - # get the episode object - epObj = self.show.getEpisode(self.season, self.episode) - - # make the result object - result = self.provider.get_result([epObj]) - result.show = self.show - result.url = self.url - result.name = self.search_name - result.quality = self.quality - result.seeders = self.seeders - result.leechers = self.leechers - result.content = None - - if result: - if result.seeders not in (-1, None) and result.leechers not in (-1, None): - logger.log(u"Downloading {0} with {1} seeders and {2} leechers from {3}".format(result.name, - result.seeders, result.leechers, result.provider.name)) + logger.log(u"Beginning to manual snatch release: {}".format(self.searchResult.name)) + + if self.searchResult: + if self.searchResult.seeders not in (-1, None) and self.searchResult.leechers not in (-1, None): + logger.log(u"Downloading {0} with {1} seeders and {2} leechers from {3}". + format(self.searchResult.name, + self.searchResult.seeders, self.searchResult.seeders, self.searchResult.provider.name)) else: - logger.log(u"Downloading {0} from {1}".format(result.name, result.provider.name)) - self.success = search.snatchEpisode(result) + logger.log(u"Downloading {0} from {1}".format(self.searchResult.name, self.searchResult.provider.name)) + self.success = search.snatchEpisode(self.searchResult) else: - logger.log(u"Unable to find a download for: [" + self.segment.prettyName() + "]") + logger.log(u"Unable to snatch release: {}".format(self.searchResult.name)) # give the CPU a break time.sleep(common.cpu_presets[sickbeard.CPU_PRESET]) @@ -288,10 +267,7 @@ def run(self): except Exception: self.success = False logger.log(traceback.format_exc(), logger.DEBUG) - ui.notifications.message('Error while snatching selected result', "Couldn't snatch the result for %s" % self.segment.prettyName()) - - ## Keep a list with the 100 last executed searches - fifo(MANUAL_SEARCH_HISTORY, self, MANUAL_SEARCH_HISTORY_SIZE) + ui.notifications.message('Error while snatching selected result', "Couldn't snatch the result for %s".format(self.searchResult.name)) if self.success is None: self.success = False diff --git a/sickbeard/webserve.py b/sickbeard/webserve.py index 1efdb1c998..f682d036f4 100644 --- a/sickbeard/webserve.py +++ b/sickbeard/webserve.py @@ -98,6 +98,8 @@ from sickrage.show.Show import Show from sickrage.system.Restart import Restart from sickrage.system.Shutdown import Shutdown +from sickbeard.tv import TVEpisode +from sickbeard.classes import SearchResult # Conditional imports try: @@ -1411,27 +1413,38 @@ def pickManualSnatch(self, show=None, season=None, episode=None, provider=None, except Exception as e: return self._genericMessage("Error", "Couldn't read cached results. Error: {}".format(e)) - try: - show = int(show) # fails if show id ends in a period SickRage/sickrage-issues#65 - showObj = Show.find(sickbeard.showList, show) - except (ValueError, TypeError): - return self._genericMessage("Error", "Invalid show ID: %s" % str(show)) - - if not showObj: - return self._genericMessage("Error", "Show is not in your library") - if not (sql_return['url'] or sql_return['quality'] or sql_return['name'] or provider or episode): return self._genericMessage("Error", "Cached result doesn't have all needed info to snatch episode") - # retrieve the episode object and fail if we can't get one - ep_obj = getEpisode(show, season, episode) - if isinstance(ep_obj, str): - return json.dumps({'result': 'failure'}) - - # make a queue item for it and put it on the queue - ep_queue_item = search_queue.ManualSnatchQueueItem(ep_obj.show, ep_obj, season, episode, - sql_return['url'], sql_return['quality'], - provider, sql_return['name'], sql_return['seeders'], sql_return['leechers']) + try: + show = int(show) # fails if show id ends in a period SickRage/sickrage-issues#65 + show_obj = Show.find(sickbeard.showList, show) + except (ValueError, TypeError): + return self._genericMessage("Error", "Invalid show ID: {}".format(show)) + + # Create a list of episode object(s) + # if multi-episode: |1|2| + # if single-episode: |1| + # TODO: Handle Season Packs: || (no episode) + episodes = sql_return['episodes'].strip("|").split("|") + ep_objs = [] + for episode in episodes: + if episode: + ep_objs.append(TVEpisode(show_obj, int(season), int(episode))) + + # TODO: Can this be moved to the ManualSnatchQueueItem? + search_result = sickbeard.providers.getProviderClass(provider).get_result(ep_objs) + search_result.show = show_obj + search_result.url = sql_return['url'] + search_result.quality = int(sql_return['quality']) + search_result.name = sql_return['name'] + search_result.size = int(sql_return['size']) + search_result.seeders = int(sql_return['seeders']) + search_result.leechers = int(sql_return['leechers']) + search_result.release_group = sql_return['release_group'] + search_result.version = int(sql_return['version']) + + ep_queue_item = search_queue.ManualSnatchQueueItem(search_result) sickbeard.searchQueueScheduler.action.add_item(ep_queue_item)