2014-03-10 01:18:05 -04:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is free software: you can redistribute it and/or modify
|
2014-03-10 01:18:05 -04:00
|
|
|
# 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.
|
|
|
|
#
|
2014-05-23 08:37:22 -04:00
|
|
|
# SickRage is distributed in the hope that it will be useful,
|
2014-03-10 01:18:05 -04:00
|
|
|
# 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
|
2014-05-23 08:37:22 -04:00
|
|
|
# along with SickRage. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
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-18 08:59:42 -04:00
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
from sickbeard.search import pickBestResult
|
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-18 08:59:42 -04:00
|
|
|
DAILY_SEARCH = 20
|
2014-05-04 23:04:46 -04:00
|
|
|
FAILED_SEARCH = 30
|
2014-07-27 06:59:21 -04:00
|
|
|
MANUAL_SEARCH = 40
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-09-15 03:23:55 -04:00
|
|
|
MANUAL_SEARCH_HISTORY = []
|
|
|
|
MANUAL_SEARCH_HISTORY_SIZE = 100
|
2014-05-26 16:16:07 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
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-19 21:04:23 -04:00
|
|
|
for cur_item in self.queue:
|
|
|
|
if isinstance(cur_item, BacklogQueueItem) and cur_item.show == show and cur_item.segment == segment:
|
|
|
|
return True
|
2014-03-10 01:18:05 -04:00
|
|
|
return False
|
|
|
|
|
2014-09-14 20:40:59 -04:00
|
|
|
def is_ep_in_queue(self, segment):
|
2014-09-07 00:36:23 -04:00
|
|
|
for cur_item in self.queue:
|
2014-09-14 20:40:59 -04:00
|
|
|
if isinstance(cur_item, (ManualSearchQueueItem, FailedQueueItem)) and cur_item.segment == segment:
|
2014-09-07 00:36:23 -04:00
|
|
|
return True
|
|
|
|
return False
|
2014-09-15 03:23:55 -04:00
|
|
|
|
|
|
|
def is_show_in_queue(self, show):
|
|
|
|
for cur_item in self.queue:
|
|
|
|
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, FailedQueueItem)) and str(cur_item.show.indexerid) == show:
|
|
|
|
ep_obj_list.append(cur_item)
|
|
|
|
|
|
|
|
if ep_obj_list:
|
|
|
|
return ep_obj_list
|
|
|
|
return False
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
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
|
|
|
|
|
2014-09-15 03:23:55 -04:00
|
|
|
def is_manualsearch_in_progress(self):
|
2014-09-23 05:06:02 -04:00
|
|
|
# Only referenced in webserve.py, only current running manualsearch or failedsearch is needed!!
|
|
|
|
if isinstance(self.currentItem, (ManualSearchQueueItem, FailedQueueItem)):
|
|
|
|
return True
|
2014-09-15 03:23:55 -04:00
|
|
|
return False
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def is_backlog_in_progress(self):
|
2014-05-19 21:04:23 -04:00
|
|
|
for cur_item in self.queue + [self.currentItem]:
|
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
|
|
|
|
|
2014-05-19 13:40:25 -04:00
|
|
|
def is_dailysearch_in_progress(self):
|
2014-05-19 21:04:23 -04:00
|
|
|
for cur_item in self.queue + [self.currentItem]:
|
2014-05-19 13:40:25 -04:00
|
|
|
if isinstance(cur_item, DailySearchQueueItem):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2014-09-07 22:48:48 -04:00
|
|
|
def queue_length(self):
|
|
|
|
length = {'backlog': 0, 'daily': 0, 'manual': 0, 'failed': 0}
|
|
|
|
for cur_item in self.queue:
|
|
|
|
if isinstance(cur_item, DailySearchQueueItem):
|
|
|
|
length['daily'] += 1
|
2014-09-08 05:45:02 -04:00
|
|
|
elif isinstance(cur_item, BacklogQueueItem):
|
|
|
|
length['backlog'] += 1
|
2014-09-07 22:48:48 -04:00
|
|
|
elif isinstance(cur_item, ManualSearchQueueItem):
|
|
|
|
length['manual'] += 1
|
|
|
|
elif isinstance(cur_item, FailedQueueItem):
|
|
|
|
length['failed'] += 1
|
|
|
|
return length
|
|
|
|
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def add_item(self, item):
|
2014-09-20 08:03:48 -04:00
|
|
|
if isinstance(item, DailySearchQueueItem):
|
|
|
|
# daily searches
|
|
|
|
generic_queue.GenericQueue.add_item(self, item)
|
|
|
|
elif isinstance(item, BacklogQueueItem) and not self.is_in_queue(item.show, item.segment):
|
|
|
|
# backlog searches
|
2014-09-07 00:36:23 -04:00
|
|
|
generic_queue.GenericQueue.add_item(self, item)
|
|
|
|
elif isinstance(item, (ManualSearchQueueItem, FailedQueueItem)) and not self.is_ep_in_queue(item.segment):
|
|
|
|
# manual and failed searches
|
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-07-18 01:57:35 -04:00
|
|
|
|
2014-05-18 08:59:42 -04:00
|
|
|
class DailySearchQueueItem(generic_queue.QueueItem):
|
2014-09-20 08:03:48 -04:00
|
|
|
def __init__(self):
|
2014-09-21 16:42:01 -04:00
|
|
|
self.success = None
|
2014-05-18 08:59:42 -04:00
|
|
|
generic_queue.QueueItem.__init__(self, 'Daily Search', DAILY_SEARCH)
|
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
def run(self):
|
|
|
|
generic_queue.QueueItem.run(self)
|
2014-05-18 08:59:42 -04:00
|
|
|
|
2014-07-17 21:06:42 -04:00
|
|
|
try:
|
2014-09-20 08:03:48 -04:00
|
|
|
logger.log("Beginning daily search for new episodes")
|
|
|
|
foundResults = search.searchForNeededEpisodes()
|
2014-05-18 08:59:42 -04:00
|
|
|
|
2014-07-17 21:06:42 -04:00
|
|
|
if not len(foundResults):
|
2014-09-20 08:03:48 -04:00
|
|
|
logger.log(u"No needed episodes found")
|
2014-07-17 21:06:42 -04:00
|
|
|
else:
|
|
|
|
for result in foundResults:
|
|
|
|
# just use the first result for now
|
|
|
|
logger.log(u"Downloading " + result.name + " from " + result.provider.name)
|
2014-09-21 16:42:01 -04:00
|
|
|
self.success = search.snatchEpisode(result)
|
2014-05-18 08:59:42 -04:00
|
|
|
|
2014-07-17 21:06:42 -04:00
|
|
|
# give the CPU a break
|
|
|
|
time.sleep(common.cpu_presets[sickbeard.CPU_PRESET])
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-07-17 21:06:42 -04:00
|
|
|
generic_queue.QueueItem.finish(self)
|
|
|
|
except Exception:
|
|
|
|
logger.log(traceback.format_exc(), logger.DEBUG)
|
2014-05-26 16:16:07 -04:00
|
|
|
|
2014-09-21 16:42:01 -04:00
|
|
|
if self.success is None:
|
|
|
|
self.success = False
|
|
|
|
|
2014-07-18 01:57:35 -04:00
|
|
|
self.finish()
|
|
|
|
|
2014-08-30 04:47:00 -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-07-14 22:00:53 -04:00
|
|
|
self.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-09-15 03:23:55 -04:00
|
|
|
self.started = None
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
def run(self):
|
|
|
|
generic_queue.QueueItem.run(self)
|
2014-05-07 03:50:49 -04:00
|
|
|
|
|
|
|
try:
|
2014-09-15 04:28:11 -04:00
|
|
|
logger.log("Beginning manual search for: [" + self.segment.prettyName() + "]")
|
2014-09-15 03:23:55 -04:00
|
|
|
self.started = True
|
|
|
|
|
2014-09-07 00:36:23 -04:00
|
|
|
searchResult = search.searchProviders(self.show, [self.segment], True)
|
2014-05-18 11:33:31 -04:00
|
|
|
|
2014-05-11 15:04:47 -04:00
|
|
|
if searchResult:
|
2014-05-18 14:21:18 -04:00
|
|
|
# just use the first result for now
|
|
|
|
logger.log(u"Downloading " + searchResult[0].name + " from " + searchResult[0].provider.name)
|
|
|
|
self.success = search.snatchEpisode(searchResult[0])
|
|
|
|
|
|
|
|
# give the CPU a break
|
2014-05-19 21:04:23 -04:00
|
|
|
time.sleep(common.cpu_presets[sickbeard.CPU_PRESET])
|
2014-05-18 14:21:18 -04:00
|
|
|
|
2014-05-13 17:47:54 -04:00
|
|
|
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-09-15 04:28:11 -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-09-15 03:23:55 -04:00
|
|
|
|
|
|
|
### Keep a list with the 100 last executed searches
|
|
|
|
fifo(MANUAL_SEARCH_HISTORY, self, MANUAL_SEARCH_HISTORY_SIZE)
|
|
|
|
|
2014-07-18 01:57:35 -04:00
|
|
|
if self.success is None:
|
2014-05-18 14:21:18 -04:00
|
|
|
self.success = False
|
2014-07-18 01:57:35 -04:00
|
|
|
|
2014-07-19 07:52:55 -04:00
|
|
|
self.finish()
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-18 01:57:35 -04:00
|
|
|
|
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-07-14 22:00:53 -04:00
|
|
|
self.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-07-14 22:00:53 -04:00
|
|
|
def run(self):
|
|
|
|
generic_queue.QueueItem.run(self)
|
2014-05-06 07:29:25 -04:00
|
|
|
|
2014-07-18 01:57:35 -04:00
|
|
|
try:
|
2014-09-15 04:28:11 -04:00
|
|
|
logger.log("Beginning backlog search for: [" + self.show.name + "]")
|
2014-09-07 00:36:23 -04:00
|
|
|
searchResult = search.searchProviders(self.show, self.segment, False)
|
2014-05-08 10:03:50 -04:00
|
|
|
|
2014-09-07 00:36:23 -04:00
|
|
|
if searchResult:
|
|
|
|
for result in searchResult:
|
|
|
|
# just use the first result for now
|
|
|
|
logger.log(u"Downloading " + result.name + " from " + result.provider.name)
|
|
|
|
search.snatchEpisode(result)
|
2014-05-18 14:21:18 -04:00
|
|
|
|
2014-09-07 00:36:23 -04:00
|
|
|
# give the CPU a break
|
|
|
|
time.sleep(common.cpu_presets[sickbeard.CPU_PRESET])
|
|
|
|
else:
|
2014-09-15 04:28:11 -04:00
|
|
|
logger.log(u"No needed episodes found during backlog search for: [" + self.show.name + "]")
|
2014-07-18 01:57:35 -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-05-26 16:16:07 -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-07-14 22:00:53 -04:00
|
|
|
self.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-09-15 09:01:18 -04:00
|
|
|
self.started = None
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-14 22:00:53 -04:00
|
|
|
def run(self):
|
|
|
|
generic_queue.QueueItem.run(self)
|
2014-09-17 06:24:54 -04:00
|
|
|
self.started = True
|
|
|
|
|
2014-07-17 21:06:42 -04:00
|
|
|
try:
|
2014-09-17 06:24:54 -04:00
|
|
|
for epObj in self.segment:
|
|
|
|
|
|
|
|
logger.log(u"Marking episode as bad: [" + epObj.prettyName() + "]")
|
|
|
|
|
|
|
|
failed_history.markFailed(epObj)
|
|
|
|
|
|
|
|
(release, provider) = failed_history.findRelease(epObj)
|
|
|
|
if release:
|
|
|
|
failed_history.logFailed(release)
|
|
|
|
history.logFailed(epObj, release, provider)
|
|
|
|
|
|
|
|
failed_history.revertEpisode(epObj)
|
|
|
|
logger.log("Beginning failed download search for: [" + epObj.prettyName() + "]")
|
2014-09-07 00:36:23 -04:00
|
|
|
|
2014-09-17 06:24:54 -04:00
|
|
|
searchResult = search.searchProviders(self.show, self.segment, True)
|
2014-09-07 00:36:23 -04:00
|
|
|
|
|
|
|
if searchResult:
|
|
|
|
for result in searchResult:
|
|
|
|
# just use the first result for now
|
|
|
|
logger.log(u"Downloading " + result.name + " from " + result.provider.name)
|
|
|
|
search.snatchEpisode(result)
|
|
|
|
|
|
|
|
# give the CPU a break
|
|
|
|
time.sleep(common.cpu_presets[sickbeard.CPU_PRESET])
|
|
|
|
else:
|
2014-09-17 06:24:54 -04:00
|
|
|
pass
|
|
|
|
#logger.log(u"No valid episode found to retry for: [" + self.segment.prettyName() + "]")
|
2014-07-17 21:06:42 -04:00
|
|
|
except Exception:
|
|
|
|
logger.log(traceback.format_exc(), logger.DEBUG)
|
2014-09-15 09:01:18 -04:00
|
|
|
|
|
|
|
### Keep a list with the 100 last executed searches
|
|
|
|
fifo(MANUAL_SEARCH_HISTORY, self, MANUAL_SEARCH_HISTORY_SIZE)
|
2014-05-08 10:03:50 -04:00
|
|
|
|
2014-07-19 21:06:04 -04:00
|
|
|
if self.success is None:
|
|
|
|
self.success = False
|
|
|
|
|
2014-09-15 03:23:55 -04:00
|
|
|
self.finish()
|
|
|
|
|
|
|
|
def fifo(myList, item, maxSize = 100):
|
|
|
|
if len(myList) >= maxSize:
|
|
|
|
myList.pop(0)
|
2014-09-21 16:42:01 -04:00
|
|
|
myList.append(item)
|