SickGear/sickbeard/search_backlog.py
Prinz23 d3a7f0ff5e Add smart logic to reduce api hits to newznab server types and improve how nzbs are downloaded.
Add newznab smart logic to avoid missing releases when there are a great many recent releases.
Change improve performance by using newznab server advertised capabilities.
Change config/providers newznab to display only non-default categories.
Change use scene season for wanted segment in backlog if show is scene numbering.
Change combine Manage Searches / Backlog Search / Limited and Full to Force.
Change consolidate limited and full backlog.
Change config / Search / Backlog search frequency to instead spread backlog searches over a number of days.
Change migrate minimum used value for search frequency into new minimum 7 for search spread.
Change restrict nzb providers to 1 backlog batch run per day.
Add to Config/Search/Unaired episodes/Allow episodes that are released early.
Add to Config/Search/Unaired episodes/Use specific api requests to search for early episode releases.
Add use related ids for newznab searches to increase search efficiency.
Add periodic update of related show ids.
Change terminology Edit Show/"Post processing" tab name to "Other".
Add advanced feature "Related show IDs" to Edit Show/Other used for finding episodes and TV info.
Add search info source image links to those that have zero id under Edit Show/Other/"Related show IDs".
Add "set master" button to Edit Show/Other/"Related show IDs" for info source that can be changed.
Change terminology displayShow "Indexers" to "Links" to cover internal and web links.
Change add related show info sources on displayShow page.
Change don't display "temporarily" defunct TVRage image link on displayShow pages unless it is master info source.
Change if a defunct info source is the master of a show then present a link on displayShow to edit related show IDs.
Change simplify the next backlog search run time display in the page footer.
Change try ssl when fetching data thetvdb, imdb, trakt, scene exception.
Change improve reliability to Trakt notifier by using show related id support.
Change improve config/providers newznab categories layout.
Change show loaded log message at start up and include info source.
Change if episode has no airdate then set status to unaired (was skipped).

Technical
Change move scene_exceptions table from cache.db to sickbeard.db.
Add related ids to show obj.
Add use of mapped indexer ids for newznab.
Add indexer to sql in wanted_eps.
Add aired in (scene) season for wanted episodes.
Add need_anime, need_sports, need_sd, need_hd, need_uhd to wanted episodes and added as parameter to update_providers.
Add fix for lib lockfile/mkdirlockfile.
Add set master TV info source logic.
Change harden ui input validation.
Add per action dialog confirmation.
Change to reload page under more events.
Change implement "Mark all added episodes Wanted to search for releases" when setting new info source.
2016-09-21 17:50:27 +01:00

327 lines
13 KiB
Python

# Author: Nic Wolfe <nic@wolfeden.ca>
# URL: http://code.google.com/p/sickbeard/
#
# This file is part of SickGear.
#
# SickGear 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.
#
# SickGear 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 SickGear. If not, see <http://www.gnu.org/licenses/>.
from __future__ import with_statement
import datetime
import threading
import sickbeard
from sickbeard import db, scheduler, helpers
from sickbeard import search_queue
from sickbeard import logger
from sickbeard import ui
from sickbeard.providers.generic import GenericProvider
from sickbeard.search import wanted_episodes
from sickbeard.helpers import find_show_by_id
from sickbeard.sbdatetime import sbdatetime
NORMAL_BACKLOG = 0
LIMITED_BACKLOG = 10
FULL_BACKLOG = 20
FORCED_BACKLOG = 30
class BacklogSearchScheduler(scheduler.Scheduler):
def force_search(self, force_type=NORMAL_BACKLOG):
self.action.forcetype = force_type
self.action.force = True
self.force = True
def next_run(self):
if 1 >= self.action._lastBacklog:
return datetime.date.today()
elif (self.action._lastBacklog + self.action.cycleTime) < datetime.date.today().toordinal():
return datetime.date.today()
else:
return datetime.date.fromordinal(self.action._lastBacklog + self.action.cycleTime)
def next_backlog_timeleft(self):
now = datetime.datetime.now()
torrent_enabled = 0 < len([x for x in sickbeard.providers.sortedProviderList() if x.is_active() and
x.enable_backlog and x.providerType == GenericProvider.TORRENT])
if now > self.action.nextBacklog or self.action.nextCyleTime != self.cycleTime:
nextruntime = now + self.timeLeft()
if not torrent_enabled:
nextpossibleruntime = (datetime.datetime.fromtimestamp(self.action.last_runtime) +
datetime.timedelta(hours=23))
for _ in xrange(5):
if nextruntime > nextpossibleruntime:
self.action.nextBacklog = nextruntime
self.action.nextCyleTime = self.cycleTime
break
nextruntime += self.cycleTime
else:
self.action.nextCyleTime = self.cycleTime
self.action.nextBacklog = nextruntime
return self.action.nextBacklog - now if self.action.nextBacklog > now else datetime.timedelta(seconds=0)
class BacklogSearcher:
def __init__(self):
self._lastBacklog = self._get_last_backlog()
self.cycleTime = sickbeard.BACKLOG_FREQUENCY
self.lock = threading.Lock()
self.amActive = False
self.amPaused = False
self.amWaiting = False
self.forcetype = NORMAL_BACKLOG
self.force = False
self.nextBacklog = datetime.datetime.fromtimestamp(1)
self.nextCyleTime = None
self.currentSearchInfo = None
self._reset_progress_indicator()
@property
def last_runtime(self):
return self._get_last_runtime()
def _reset_progress_indicator(self):
self.percentDone = 0
self.currentSearchInfo = {'title': 'Initializing'}
def get_progress_indicator(self):
if self.amActive:
return ui.ProgressIndicator(self.percentDone, self.currentSearchInfo)
else:
return None
def am_running(self):
logger.log(u'amWaiting: ' + str(self.amWaiting) + ', amActive: ' + str(self.amActive), logger.DEBUG)
return (not self.amWaiting) and self.amActive
def add_backlog_item(self, items, standard_backlog, limited_backlog, forced, torrent_only):
for segments in items:
if len(segments):
for season, segment in segments.items():
self.currentSearchInfo = {'title': segment[0].show.name + ' Season ' + str(season)}
backlog_queue_item = search_queue.BacklogQueueItem(
segment[0].show, segment, standard_backlog=standard_backlog, limited_backlog=limited_backlog,
forced=forced, torrent_only=torrent_only)
sickbeard.searchQueueScheduler.action.add_item(backlog_queue_item)
def search_backlog(self, which_shows=None, force_type=NORMAL_BACKLOG, force=False):
if self.amActive:
logger.log(u'Backlog is still running, not starting it again', logger.DEBUG)
return
if which_shows:
show_list = which_shows
standard_backlog = False
else:
show_list = sickbeard.showList
standard_backlog = True
now = datetime.datetime.now()
torrent_only = continued_backlog = False
if not force and standard_backlog and (datetime.datetime.now() - datetime.datetime.fromtimestamp(
self._get_last_runtime())) < datetime.timedelta(hours=23):
if [x for x in sickbeard.providers.sortedProviderList() if x.is_active() and x.enable_backlog and
x.providerType == GenericProvider.TORRENT]:
torrent_only = True
else:
logger.log('Last scheduled Backlog run was within the last day, skipping this run.', logger.DEBUG)
return
self._get_last_backlog()
self.amActive = True
self.amPaused = False
cur_date = datetime.date.today().toordinal()
from_date = datetime.date.fromordinal(1)
limited_from_date = datetime.date.today() - datetime.timedelta(days=sickbeard.BACKLOG_DAYS)
limited_backlog = False
if not which_shows and torrent_only:
logger.log(u'Running limited backlog for episodes missed during the last %s day(s)' %
str(sickbeard.BACKLOG_DAYS))
from_date = limited_from_date
limited_backlog = True
runparts = []
if standard_backlog and not torrent_only:
my_db = db.DBConnection('cache.db')
sql_result = my_db.select('SELECT * FROM backlogparts WHERE part in (SELECT MIN(part) FROM backlogparts)')
if sql_result:
sl = []
part_nr = int(sql_result[0]['part'])
for s in sql_result:
show_obj = find_show_by_id(sickbeard.showList, {int(s['indexer']): int(s['indexerid'])})
if show_obj:
sl.append(show_obj)
runparts.append([int(s['indexerid']), int(s['indexer'])])
show_list = sl
continued_backlog = True
my_db.action('DELETE FROM backlogparts WHERE part = ?', [part_nr])
forced = False
if not which_shows and force_type != NORMAL_BACKLOG:
forced = True
wanted_list = []
for curShow in show_list:
if not curShow.paused:
w = wanted_episodes(curShow, from_date, make_dict=True,
unaired=(sickbeard.SEARCH_UNAIRED and not sickbeard.UNAIRED_RECENT_SEARCH_ONLY))
if w:
wanted_list.append(w)
parts = []
if standard_backlog and not torrent_only and not continued_backlog:
fullbacklogparts = sum([len(w) for w in wanted_list if w]) / sickbeard.BACKLOG_FREQUENCY
h_part = []
counter = 0
for w in wanted_list:
f = False
for season, segment in w.iteritems():
counter += 1
if not f:
h_part.append([segment[0].show.indexerid, segment[0].show.indexer])
f = True
if counter > fullbacklogparts:
counter = 0
parts.append(h_part)
h_part = []
if h_part:
parts.append(h_part)
def in_showlist(show, showlist):
return 0 < len([item for item in showlist if item[1] == show.indexer and item[0] == show.indexerid])
if not runparts and parts:
runparts = parts[0]
wanted_list = [w for w in wanted_list if w and in_showlist(w.itervalues().next()[0].show, runparts)]
limited_wanted_list = []
if standard_backlog and not torrent_only and runparts:
for curShow in sickbeard.showList:
if not curShow.paused and not in_showlist(curShow, runparts):
w = wanted_episodes(curShow, limited_from_date, make_dict=True,
unaired=(sickbeard.SEARCH_UNAIRED and not sickbeard.UNAIRED_RECENT_SEARCH_ONLY))
if w:
limited_wanted_list.append(w)
self.add_backlog_item(wanted_list, standard_backlog, limited_backlog, forced, torrent_only)
if standard_backlog and not torrent_only and limited_wanted_list:
self.add_backlog_item(limited_wanted_list, standard_backlog, True, forced, torrent_only)
if standard_backlog and not torrent_only and not continued_backlog:
cl = ([], [['DELETE FROM backlogparts']])[len(parts) > 1]
for i, l in enumerate(parts):
if 0 == i:
continue
for m in l:
cl.append(['INSERT INTO backlogparts (part, indexerid, indexer) VALUES (?,?,?)',
[i + 1, m[0], m[1]]])
if 0 < len(cl):
my_db.mass_action(cl)
# don't consider this an actual backlog search if we only did recent eps
# or if we only did certain shows
if from_date == datetime.date.fromordinal(1) and not which_shows:
self._set_last_backlog(cur_date)
self._get_last_backlog()
if standard_backlog and not torrent_only:
self._set_last_runtime(now)
self.amActive = False
self._reset_progress_indicator()
@staticmethod
def _get_last_runtime():
logger.log('Retrieving the last runtime of Backlog from the DB', logger.DEBUG)
my_db = db.DBConnection()
sql_results = my_db.select('SELECT * FROM info')
if 0 == len(sql_results):
last_run_time = 1
elif None is sql_results[0]['last_run_backlog'] or '' == sql_results[0]['last_run_backlog']:
last_run_time = 1
else:
last_run_time = int(sql_results[0]['last_run_backlog'])
if last_run_time > sbdatetime.now().totimestamp(default=0):
last_run_time = 1
return last_run_time
def _set_last_runtime(self, when):
logger.log('Setting the last backlog runtime in the DB to %s' % when, logger.DEBUG)
my_db = db.DBConnection()
sql_results = my_db.select('SELECT * FROM info')
if len(sql_results) == 0:
my_db.action('INSERT INTO info (last_backlog, last_indexer, last_run_backlog) VALUES (?,?,?)',
[1, 0, sbdatetime.totimestamp(when, default=0)])
else:
my_db.action('UPDATE info SET last_run_backlog=%s' % sbdatetime.totimestamp(when, default=0))
self.nextBacklog = datetime.datetime.fromtimestamp(1)
def _get_last_backlog(self):
logger.log('Retrieving the last check time from the DB', logger.DEBUG)
my_db = db.DBConnection()
sql_results = my_db.select('SELECT * FROM info')
if 0 == len(sql_results):
last_backlog = 1
elif None is sql_results[0]['last_backlog'] or '' == sql_results[0]['last_backlog']:
last_backlog = 1
else:
last_backlog = int(sql_results[0]['last_backlog'])
if last_backlog > datetime.date.today().toordinal():
last_backlog = 1
self._lastBacklog = last_backlog
return self._lastBacklog
@staticmethod
def _set_last_backlog(when):
logger.log('Setting the last backlog in the DB to %s' % when, logger.DEBUG)
my_db = db.DBConnection()
sql_results = my_db.select('SELECT * FROM info')
if len(sql_results) == 0:
my_db.action('INSERT INTO info (last_backlog, last_indexer, last_run_backlog) VALUES (?,?,?)',
[str(when), 0, 1])
else:
my_db.action('UPDATE info SET last_backlog=%s' % when)
def run(self):
try:
force_type = self.forcetype
force = self.force
self.forcetype = NORMAL_BACKLOG
self.force = False
self.search_backlog(force_type=force_type, force=force)
except:
self.amActive = False
raise