Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Manual Search] Log multi-epi as snatched #240

Merged
merged 2 commits into from
Apr 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sickbeard/manual_snatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
76 changes: 26 additions & 50 deletions sickbeard/search_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -137,7 +140,6 @@ def __init__(self):
self.success = None
self.started = None


def run(self):
generic_queue.QueueItem.run(self)
self.started = True
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -230,68 +232,42 @@ 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])

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 <i>%s</i>" % 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 <i>%s</i>".format(self.searchResult.name))

if self.success is None:
self.success = False
Expand Down
49 changes: 31 additions & 18 deletions sickbeard/webserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand Down