2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
|
|
|
# This file is part of Sick Beard.
|
|
|
|
#
|
|
|
|
# Sick Beard is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Sick Beard is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from __future__ import with_statement
|
|
|
|
|
2014-05-17 05:27:17 -04:00
|
|
|
import time
|
2014-05-08 10:03:50 -04:00
|
|
|
import traceback
|
2014-05-11 08:49:07 -04:00
|
|
|
import threading
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
from sickbeard import db, logger, common, exceptions, helpers
|
2014-05-08 10:03:50 -04:00
|
|
|
from sickbeard import generic_queue, scheduler
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard import search, failed_history, history
|
|
|
|
from sickbeard import ui
|
2014-05-06 07:29:25 -04:00
|
|
|
|
2014-05-11 08:49:07 -04:00
|
|
|
search_queue_lock = threading.Lock()
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
BACKLOG_SEARCH = 10
|
2014-05-04 23:04:46 -04:00
|
|
|
FAILED_SEARCH = 30
|
2014-03-10 01:18:05 -04:00
|
|
|
MANUAL_SEARCH = 30
|
|
|
|
|
|
|
|
class SearchQueue(generic_queue.GenericQueue):
|
|
|
|
def __init__(self):
|
|
|
|
generic_queue.GenericQueue.__init__(self)
|
|
|
|
self.queue_name = "SEARCHQUEUE"
|
|
|
|
|
|
|
|
def is_in_queue(self, show, segment):
|
2014-05-11 11:53:42 -04:00
|
|
|
queue = [x for x in self.queue.queue] + [self.currentItem]
|
|
|
|
for cur_item in queue:
|
2014-05-15 23:39:46 -04:00
|
|
|
if cur_item:
|
|
|
|
if cur_item.show == show and cur_item.segment == segment:
|
2014-05-11 08:49:07 -04:00
|
|
|
return True
|
2014-03-10 01:18:05 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
def pause_backlog(self):
|
|
|
|
self.min_priority = generic_queue.QueuePriorities.HIGH
|
|
|
|
|
|
|
|
def unpause_backlog(self):
|
|
|
|
self.min_priority = 0
|
|
|
|
|
|
|
|
def is_backlog_paused(self):
|
|
|
|
# backlog priorities are NORMAL, this should be done properly somewhere
|
|
|
|
return self.min_priority >= generic_queue.QueuePriorities.NORMAL
|
|
|
|
|
|
|
|
def is_backlog_in_progress(self):
|
2014-05-11 11:53:42 -04:00
|
|
|
queue = [x for x in self.queue.queue] + [self.currentItem]
|
|
|
|
for cur_item in queue:
|
2014-05-11 10:25:46 -04:00
|
|
|
if isinstance(cur_item, BacklogQueueItem):
|
|
|
|
return True
|
2014-03-10 01:18:05 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
def add_item(self, item):
|
2014-05-08 10:03:50 -04:00
|
|
|
|
2014-05-11 08:49:07 -04:00
|
|
|
if isinstance(item, BacklogQueueItem) and not self.is_in_queue(item.show, item.segment):
|
2014-03-10 01:18:05 -04:00
|
|
|
generic_queue.GenericQueue.add_item(self, item)
|
2014-05-15 23:39:46 -04:00
|
|
|
elif isinstance(item, ManualSearchQueueItem) and not self.is_in_queue(item.show, item.segment):
|
2014-03-10 01:18:05 -04:00
|
|
|
generic_queue.GenericQueue.add_item(self, item)
|
2014-05-15 23:39:46 -04:00
|
|
|
elif isinstance(item, FailedQueueItem) and not self.is_in_queue(item.show, item.segment):
|
2014-03-10 01:18:05 -04:00
|
|
|
generic_queue.GenericQueue.add_item(self, item)
|
|
|
|
else:
|
|
|
|
logger.log(u"Not adding item, it's already in the queue", logger.DEBUG)
|
|
|
|
|
2014-05-11 15:04:47 -04:00
|
|
|
def snatch_item(self, item):
|
|
|
|
for result in item.results:
|
|
|
|
# just use the first result for now
|
|
|
|
logger.log(u"Downloading " + result.name + " from " + result.provider.name)
|
2014-05-17 05:27:17 -04:00
|
|
|
item.success = search.snatchEpisode(result)
|
|
|
|
time.sleep(2)
|
2014-05-11 15:04:47 -04:00
|
|
|
generic_queue.QueueItem.finish(item)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
class ManualSearchQueueItem(generic_queue.QueueItem):
|
2014-05-15 23:39:46 -04:00
|
|
|
def __init__(self, show, segment):
|
2014-03-10 01:18:05 -04:00
|
|
|
generic_queue.QueueItem.__init__(self, 'Manual Search', MANUAL_SEARCH)
|
|
|
|
self.priority = generic_queue.QueuePriorities.HIGH
|
2014-05-15 23:39:46 -04:00
|
|
|
self.thread_name = 'MANUAL-' + str(show.indexerid) + '-'
|
2014-03-10 01:18:05 -04:00
|
|
|
self.success = None
|
2014-05-15 23:39:46 -04:00
|
|
|
self.show = show
|
|
|
|
self.segment = segment
|
2014-05-13 17:47:54 -04:00
|
|
|
self.results = []
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
generic_queue.QueueItem.execute(self)
|
2014-05-07 03:50:49 -04:00
|
|
|
|
|
|
|
try:
|
2014-05-15 23:39:46 -04:00
|
|
|
logger.log("Beginning manual search for [" + self.segment.prettyName() + "]")
|
2014-05-17 01:23:11 -04:00
|
|
|
searchResult = search.searchProviders(self, self.show, self.segment.season, [self.segment], True)
|
2014-05-11 08:49:07 -04:00
|
|
|
|
2014-05-11 15:04:47 -04:00
|
|
|
if searchResult:
|
2014-05-13 17:47:54 -04:00
|
|
|
SearchQueue().snatch_item(searchResult)
|
|
|
|
else:
|
|
|
|
ui.notifications.message('No downloads were found',
|
2014-05-15 23:39:46 -04:00
|
|
|
"Couldn't find a download for <i>%s</i>" % self.segment.prettyName())
|
2014-05-13 17:47:54 -04:00
|
|
|
|
2014-05-15 23:39:46 -04:00
|
|
|
logger.log(u"Unable to find a download for " + self.segment.prettyName())
|
2014-05-11 08:49:07 -04:00
|
|
|
|
|
|
|
except Exception:
|
2014-05-08 10:03:50 -04:00
|
|
|
logger.log(traceback.format_exc(), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-11 08:49:07 -04:00
|
|
|
self.finish()
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
class BacklogQueueItem(generic_queue.QueueItem):
|
|
|
|
def __init__(self, show, segment):
|
|
|
|
generic_queue.QueueItem.__init__(self, 'Backlog', BACKLOG_SEARCH)
|
|
|
|
self.priority = generic_queue.QueuePriorities.LOW
|
2014-05-11 11:20:35 -04:00
|
|
|
self.thread_name = 'BACKLOG-' + str(show.indexerid) + '-'
|
2014-05-12 13:49:59 -04:00
|
|
|
self.success = None
|
2014-03-10 01:18:05 -04:00
|
|
|
self.show = show
|
|
|
|
self.segment = segment
|
2014-05-13 17:47:54 -04:00
|
|
|
self.results = []
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
generic_queue.QueueItem.execute(self)
|
2014-05-06 07:29:25 -04:00
|
|
|
|
2014-05-15 23:39:46 -04:00
|
|
|
for season in self.segment:
|
2014-05-16 01:03:37 -04:00
|
|
|
sickbeard.searchBacklog.BacklogSearcher.currentSearchInfo = {'title': self.show.name + " Season " + str(season)}
|
2014-05-16 00:55:56 -04:00
|
|
|
|
2014-05-15 23:39:46 -04:00
|
|
|
wantedEps = self.segment[season]
|
2014-05-08 10:03:50 -04:00
|
|
|
|
2014-05-15 23:39:46 -04:00
|
|
|
try:
|
2014-05-16 05:16:01 -04:00
|
|
|
logger.log("Beginning backlog search for [" + self.show.name + "]")
|
2014-05-17 01:23:11 -04:00
|
|
|
searchResult = search.searchProviders(self, self.show, season, wantedEps, False)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-15 23:39:46 -04:00
|
|
|
if searchResult:
|
|
|
|
SearchQueue().snatch_item(searchResult)
|
|
|
|
else:
|
|
|
|
logger.log(u"No needed episodes found during backlog search")
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-15 23:39:46 -04:00
|
|
|
except Exception:
|
|
|
|
logger.log(traceback.format_exc(), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-15 23:39:46 -04:00
|
|
|
self.finish()
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class FailedQueueItem(generic_queue.QueueItem):
|
2014-05-15 23:39:46 -04:00
|
|
|
def __init__(self, show, segment):
|
2014-05-04 23:04:46 -04:00
|
|
|
generic_queue.QueueItem.__init__(self, 'Retry', FAILED_SEARCH)
|
2014-03-10 01:18:05 -04:00
|
|
|
self.priority = generic_queue.QueuePriorities.HIGH
|
2014-05-11 11:20:35 -04:00
|
|
|
self.thread_name = 'RETRY-' + str(show.indexerid) + '-'
|
2014-03-10 01:18:05 -04:00
|
|
|
self.show = show
|
2014-05-15 23:39:46 -04:00
|
|
|
self.segment = segment
|
2014-03-10 01:18:05 -04:00
|
|
|
self.success = None
|
2014-05-13 17:47:54 -04:00
|
|
|
self.results = []
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
generic_queue.QueueItem.execute(self)
|
|
|
|
|
2014-05-13 07:16:32 -04:00
|
|
|
failed_episodes = []
|
2014-05-15 23:39:46 -04:00
|
|
|
for season in self.segment:
|
|
|
|
epObj = self.segment[season]
|
|
|
|
|
2014-05-13 07:16:32 -04:00
|
|
|
(release, provider) = failed_history.findRelease(epObj)
|
2014-05-13 01:48:00 -04:00
|
|
|
if release:
|
|
|
|
logger.log(u"Marking release as bad: " + release)
|
2014-05-13 07:16:32 -04:00
|
|
|
failed_history.markFailed(epObj)
|
2014-05-13 01:48:00 -04:00
|
|
|
failed_history.logFailed(release)
|
2014-05-13 07:16:32 -04:00
|
|
|
history.logFailed(epObj, release, provider)
|
|
|
|
failed_history.revertEpisode(epObj)
|
|
|
|
failed_episodes.append(epObj)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-13 15:34:57 -04:00
|
|
|
logger.log(
|
2014-05-15 23:39:46 -04:00
|
|
|
"Beginning failed download search for [" + epObj.prettyName() + "]")
|
2014-05-11 08:49:07 -04:00
|
|
|
|
2014-05-15 23:39:46 -04:00
|
|
|
if len(failed_episodes):
|
|
|
|
try:
|
2014-05-17 01:23:11 -04:00
|
|
|
searchResult = search.searchProviders(self, self.show, failed_episodes[0].season, failed_episodes, True)
|
2014-05-11 08:49:07 -04:00
|
|
|
|
2014-05-13 17:47:54 -04:00
|
|
|
if searchResult:
|
|
|
|
SearchQueue().snatch_item(searchResult)
|
|
|
|
else:
|
|
|
|
logger.log(u"No episodes found to retry for failed downloads return from providers!")
|
2014-05-13 15:34:57 -04:00
|
|
|
except Exception, e:
|
|
|
|
logger.log(traceback.format_exc(), logger.DEBUG)
|
2014-05-08 10:03:50 -04:00
|
|
|
|
2014-05-11 08:49:07 -04:00
|
|
|
self.finish()
|