2014-03-10 05:18:05 +00:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +00: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-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2014-03-10 05:18:05 +00: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-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
from __future__ import with_statement
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import threading
|
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
|
2015-03-12 23:23:32 +00:00
|
|
|
from sickbeard import db, scheduler, helpers
|
2014-03-10 05:18:05 +00:00
|
|
|
from sickbeard import search_queue
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard import ui
|
2014-05-16 03:39:46 +00:00
|
|
|
from sickbeard import common
|
2015-09-18 00:06:34 +00:00
|
|
|
from sickbeard.search import wanted_episodes
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
NORMAL_BACKLOG = 0
|
|
|
|
LIMITED_BACKLOG = 10
|
|
|
|
FULL_BACKLOG = 20
|
2014-08-30 08:47:00 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
class BacklogSearchScheduler(scheduler.Scheduler):
|
2015-05-04 19:14:29 +00:00
|
|
|
def forceSearch(self, force_type=NORMAL_BACKLOG):
|
|
|
|
self.force = True
|
|
|
|
self.action.forcetype = force_type
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def nextRun(self):
|
|
|
|
if self.action._lastBacklog <= 1:
|
2014-09-15 13:42:24 +00:00
|
|
|
return datetime.date.today()
|
2015-01-04 02:26:55 +00:00
|
|
|
elif (self.action._lastBacklog + self.action.cycleTime) < datetime.date.today().toordinal():
|
|
|
|
return datetime.date.today()
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2014-09-15 13:42:24 +00:00
|
|
|
return datetime.date.fromordinal(self.action._lastBacklog + self.action.cycleTime)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-08-30 08:47:00 +00:00
|
|
|
|
2014-03-25 05:57:24 +00:00
|
|
|
class BacklogSearcher:
|
2014-03-10 05:18:05 +00:00
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
self._lastBacklog = self._get_lastBacklog()
|
2015-04-09 17:24:35 +00:00
|
|
|
self.cycleTime = sickbeard.BACKLOG_FREQUENCY
|
2014-03-10 05:18:05 +00:00
|
|
|
self.lock = threading.Lock()
|
|
|
|
self.amActive = False
|
|
|
|
self.amPaused = False
|
|
|
|
self.amWaiting = False
|
2015-05-04 19:14:29 +00:00
|
|
|
self.forcetype = NORMAL_BACKLOG
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
self._resetPI()
|
|
|
|
|
|
|
|
def _resetPI(self):
|
|
|
|
self.percentDone = 0
|
|
|
|
self.currentSearchInfo = {'title': 'Initializing'}
|
|
|
|
|
|
|
|
def getProgressIndicator(self):
|
|
|
|
if self.amActive:
|
|
|
|
return ui.ProgressIndicator(self.percentDone, self.currentSearchInfo)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def am_running(self):
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'amWaiting: ' + str(self.amWaiting) + ', amActive: ' + str(self.amActive), logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
return (not self.amWaiting) and self.amActive
|
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def search_backlog(self, which_shows=None, force_type=NORMAL_BACKLOG):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-19 19:52:16 +00:00
|
|
|
if self.amActive:
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'Backlog is still running, not starting it again', logger.DEBUG)
|
2014-07-19 19:52:16 +00:00
|
|
|
return
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if which_shows:
|
|
|
|
show_list = which_shows
|
2015-04-05 18:12:15 +00:00
|
|
|
standard_backlog = False
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
|
|
|
show_list = sickbeard.showList
|
2015-04-05 18:12:15 +00:00
|
|
|
standard_backlog = True
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
self._get_lastBacklog()
|
|
|
|
|
|
|
|
curDate = datetime.date.today().toordinal()
|
|
|
|
fromDate = datetime.date.fromordinal(1)
|
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
limited_backlog = False
|
|
|
|
if (not which_shows and force_type == LIMITED_BACKLOG) or (not which_shows and force_type != FULL_BACKLOG and not curDate - self._lastBacklog >= self.cycleTime):
|
2014-11-16 02:19:15 +00:00
|
|
|
logger.log(u'Running limited backlog for episodes missed during the last %s day(s)' % str(sickbeard.BACKLOG_DAYS))
|
2014-09-19 08:49:37 +00:00
|
|
|
fromDate = datetime.date.today() - datetime.timedelta(days=sickbeard.BACKLOG_DAYS)
|
2015-05-04 19:14:29 +00:00
|
|
|
limited_backlog = True
|
|
|
|
|
|
|
|
forced = False
|
|
|
|
if not which_shows and force_type != NORMAL_BACKLOG:
|
|
|
|
forced = True
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
self.amActive = True
|
|
|
|
self.amPaused = False
|
|
|
|
|
|
|
|
# go through non air-by-date shows and see if they need any episodes
|
|
|
|
for curShow in show_list:
|
|
|
|
|
|
|
|
if curShow.paused:
|
|
|
|
continue
|
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
segments = wanted_episodes(curShow, fromDate, make_dict=True)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-09-07 04:36:23 +00:00
|
|
|
for season, segment in segments.items():
|
2015-05-04 19:14:29 +00:00
|
|
|
self.currentSearchInfo = {'title': curShow.name + ' Season ' + str(season)}
|
2014-09-07 04:36:23 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
backlog_queue_item = search_queue.BacklogQueueItem(curShow, segment, standard_backlog=standard_backlog, limited_backlog=limited_backlog, forced=forced)
|
2014-08-30 08:47:00 +00:00
|
|
|
sickbeard.searchQueueScheduler.action.add_item(backlog_queue_item) # @UndefinedVariable
|
2014-05-16 04:55:56 +00:00
|
|
|
else:
|
2015-03-12 23:23:32 +00:00
|
|
|
logger.log(u'Nothing needs to be downloaded for %s, skipping' % str(curShow.name), logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
# don't consider this an actual backlog search if we only did recent eps
|
|
|
|
# or if we only did certain shows
|
|
|
|
if fromDate == datetime.date.fromordinal(1) and not which_shows:
|
|
|
|
self._set_lastBacklog(curDate)
|
2015-08-10 14:16:22 +00:00
|
|
|
self._get_lastBacklog()
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
self.amActive = False
|
|
|
|
self._resetPI()
|
|
|
|
|
|
|
|
def _get_lastBacklog(self):
|
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'Retrieving the last check time from the DB', logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-06-21 22:46:59 +00:00
|
|
|
myDB = db.DBConnection()
|
2015-05-04 19:14:29 +00:00
|
|
|
sqlResults = myDB.select('SELECT * FROM info')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if len(sqlResults) == 0:
|
|
|
|
lastBacklog = 1
|
2015-05-04 19:14:29 +00:00
|
|
|
elif sqlResults[0]['last_backlog'] == None or sqlResults[0]['last_backlog'] == '':
|
2014-03-10 05:18:05 +00:00
|
|
|
lastBacklog = 1
|
|
|
|
else:
|
2015-05-04 19:14:29 +00:00
|
|
|
lastBacklog = int(sqlResults[0]['last_backlog'])
|
2014-03-11 20:22:00 +00:00
|
|
|
if lastBacklog > datetime.date.today().toordinal():
|
|
|
|
lastBacklog = 1
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
self._lastBacklog = lastBacklog
|
|
|
|
return self._lastBacklog
|
|
|
|
|
|
|
|
def _set_lastBacklog(self, when):
|
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'Setting the last backlog in the DB to ' + str(when), logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-06-21 22:46:59 +00:00
|
|
|
myDB = db.DBConnection()
|
2015-05-04 19:14:29 +00:00
|
|
|
sqlResults = myDB.select('SELECT * FROM info')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-06-21 22:46:59 +00:00
|
|
|
if len(sqlResults) == 0:
|
2015-05-04 19:14:29 +00:00
|
|
|
myDB.action('INSERT INTO info (last_backlog, last_indexer) VALUES (?,?)', [str(when), 0])
|
2014-06-21 22:46:59 +00:00
|
|
|
else:
|
2015-05-04 19:14:29 +00:00
|
|
|
myDB.action('UPDATE info SET last_backlog=' + str(when))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-02-28 16:17:04 +00:00
|
|
|
def run(self):
|
2014-03-10 05:18:05 +00:00
|
|
|
try:
|
2015-05-04 19:14:29 +00:00
|
|
|
force_type = self.forcetype
|
|
|
|
self.forcetype = NORMAL_BACKLOG
|
2015-09-18 00:06:34 +00:00
|
|
|
self.search_backlog(force_type=force_type)
|
2014-03-10 05:18:05 +00:00
|
|
|
except:
|
|
|
|
self.amActive = False
|
|
|
|
raise
|