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
|
|
|
|
|
|
|
import datetime
|
|
|
|
import operator
|
2014-05-19 17:40:25 +00:00
|
|
|
import threading
|
2014-07-24 16:12:29 +00:00
|
|
|
import traceback
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
|
|
|
|
from sickbeard import db
|
|
|
|
from sickbeard import exceptions
|
|
|
|
from sickbeard.exceptions import ex
|
|
|
|
from sickbeard import helpers, logger, show_name_helpers
|
|
|
|
from sickbeard import search
|
|
|
|
from sickbeard import history
|
|
|
|
|
|
|
|
from sickbeard.common import DOWNLOADED, SNATCHED, SNATCHED_PROPER, Quality
|
|
|
|
|
2014-07-06 13:11:04 +00:00
|
|
|
from name_parser.parser import NameParser, InvalidNameException, InvalidShowException
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
def searchPropers():
|
2014-05-14 22:23:59 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
if not sickbeard.DOWNLOAD_PROPERS:
|
|
|
|
return
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'Beginning the search for new propers')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
propers = _getProperList()
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
if propers:
|
|
|
|
_downloadPropers(propers)
|
2014-05-19 17:40:25 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
_set_lastProperSearch(datetime.datetime.today().toordinal())
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
run_at = ''
|
|
|
|
if None is sickbeard.properFinderScheduler.start_time:
|
|
|
|
run_in = sickbeard.properFinderScheduler.lastRun + sickbeard.properFinderScheduler.cycleTime - datetime.datetime.now()
|
|
|
|
hours, remainder = divmod(run_in.seconds, 3600)
|
|
|
|
minutes, seconds = divmod(remainder, 60)
|
|
|
|
run_at = u', next check in approx. ' + (
|
|
|
|
'%dh, %dm' % (hours, minutes) if 0 < hours else '%dm, %ds' % (minutes, seconds))
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'Completed the search for new propers%s' % run_at)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
def _getProperList():
|
|
|
|
propers = {}
|
2014-06-19 07:12:30 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
search_date = datetime.datetime.today() - datetime.timedelta(days=2)
|
2014-05-14 22:23:59 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# for each provider get a list of the
|
|
|
|
origThreadName = threading.currentThread().name
|
2015-07-13 09:39:20 +00:00
|
|
|
providers = [x for x in sickbeard.providers.sortedProviderList() if x.is_active()]
|
2015-05-04 19:14:29 +00:00
|
|
|
for curProvider in providers:
|
|
|
|
threading.currentThread().name = origThreadName + ' :: [' + curProvider.name + ']'
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'Searching for any new PROPER releases from ' + curProvider.name)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
try:
|
2015-07-13 09:39:20 +00:00
|
|
|
curPropers = curProvider.find_propers(search_date)
|
2015-06-08 12:47:01 +00:00
|
|
|
except exceptions.AuthException as e:
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'Authentication error: ' + ex(e), logger.ERROR)
|
|
|
|
continue
|
2015-06-08 12:47:01 +00:00
|
|
|
except Exception as e:
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'Error while searching ' + curProvider.name + ', skipping: ' + ex(e), logger.ERROR)
|
|
|
|
logger.log(traceback.format_exc(), logger.DEBUG)
|
|
|
|
continue
|
|
|
|
finally:
|
|
|
|
threading.currentThread().name = origThreadName
|
|
|
|
|
|
|
|
# if they haven't been added by a different provider than add the proper to the list
|
|
|
|
for x in curPropers:
|
|
|
|
name = _genericName(x.name)
|
|
|
|
if not name in propers:
|
|
|
|
logger.log(u'Found new proper: ' + x.name, logger.DEBUG)
|
|
|
|
x.provider = curProvider
|
|
|
|
propers[name] = x
|
|
|
|
|
|
|
|
# take the list of unique propers and get it sorted by
|
|
|
|
sortedPropers = sorted(propers.values(), key=operator.attrgetter('date'), reverse=True)
|
|
|
|
finalPropers = []
|
|
|
|
|
|
|
|
for curProper in sortedPropers:
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
try:
|
|
|
|
myParser = NameParser(False)
|
|
|
|
parse_result = myParser.parse(curProper.name)
|
|
|
|
except InvalidNameException:
|
|
|
|
logger.log(u'Unable to parse the filename ' + curProper.name + ' into a valid episode', logger.DEBUG)
|
|
|
|
continue
|
|
|
|
except InvalidShowException:
|
|
|
|
logger.log(u'Unable to parse the filename ' + curProper.name + ' into a valid show', logger.DEBUG)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not parse_result.series_name:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not parse_result.episode_numbers:
|
2014-05-30 07:36:47 +00:00
|
|
|
logger.log(
|
2015-05-04 19:14:29 +00:00
|
|
|
u'Ignoring ' + curProper.name + ' because it\'s for a full season rather than specific episode',
|
2014-05-30 07:36:47 +00:00
|
|
|
logger.DEBUG)
|
2015-05-04 19:14:29 +00:00
|
|
|
continue
|
2014-05-30 07:36:47 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(
|
|
|
|
u'Successful match! Result ' + parse_result.original_name + ' matched to show ' + parse_result.show.name,
|
|
|
|
logger.DEBUG)
|
2014-05-30 07:36:47 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# set the indexerid in the db to the show's indexerid
|
|
|
|
curProper.indexerid = parse_result.show.indexerid
|
2014-05-30 07:36:47 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# set the indexer in the db to the show's indexer
|
|
|
|
curProper.indexer = parse_result.show.indexer
|
2014-08-01 09:40:46 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# populate our Proper instance
|
|
|
|
curProper.season = parse_result.season_number if parse_result.season_number != None else 1
|
|
|
|
curProper.episode = parse_result.episode_numbers[0]
|
|
|
|
curProper.release_group = parse_result.release_group
|
|
|
|
curProper.version = parse_result.version
|
|
|
|
curProper.quality = Quality.nameQuality(curProper.name, parse_result.is_anime)
|
2014-05-26 06:29:22 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# only get anime proper if it has release group and version
|
|
|
|
if parse_result.is_anime:
|
|
|
|
if not curProper.release_group and curProper.version == -1:
|
|
|
|
logger.log(u'Proper ' + curProper.name + ' doesn\'t have a release group and version, ignoring it',
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
continue
|
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
if not show_name_helpers.filterBadReleases(curProper.name, parse=False):
|
|
|
|
logger.log(u'Proper ' + curProper.name + ' isn\'t a valid scene release that we want, ignoring it',
|
|
|
|
logger.DEBUG)
|
|
|
|
continue
|
2014-04-24 05:18:16 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
if parse_result.show.rls_ignore_words and search.filter_release_name(curProper.name,
|
|
|
|
parse_result.show.rls_ignore_words):
|
|
|
|
logger.log(
|
|
|
|
u'Ignoring ' + curProper.name + ' based on ignored words filter: ' + parse_result.show.rls_ignore_words,
|
|
|
|
logger.MESSAGE)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if parse_result.show.rls_require_words and not search.filter_release_name(curProper.name,
|
|
|
|
parse_result.show.rls_require_words):
|
|
|
|
logger.log(
|
|
|
|
u'Ignoring ' + curProper.name + ' based on required words filter: ' + parse_result.show.rls_require_words,
|
|
|
|
logger.MESSAGE)
|
|
|
|
continue
|
2014-04-24 05:18:16 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# check if we actually want this proper (if it's the right quality)
|
|
|
|
myDB = db.DBConnection()
|
|
|
|
sqlResults = myDB.select('SELECT status FROM tv_episodes WHERE showid = ? AND season = ? AND episode = ?',
|
|
|
|
[curProper.indexerid, curProper.season, curProper.episode])
|
|
|
|
if not sqlResults:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# only keep the proper if we have already retrieved the same quality ep (don't get better/worse ones)
|
|
|
|
oldStatus, oldQuality = Quality.splitCompositeStatus(int(sqlResults[0]['status']))
|
|
|
|
if oldStatus not in (DOWNLOADED, SNATCHED) or oldQuality != curProper.quality:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# check if we actually want this proper (if it's the right release group and a higher version)
|
|
|
|
if parse_result.is_anime:
|
2014-07-27 14:04:37 +00:00
|
|
|
myDB = db.DBConnection()
|
2015-05-04 19:14:29 +00:00
|
|
|
sqlResults = myDB.select(
|
|
|
|
'SELECT release_group, version FROM tv_episodes WHERE showid = ? AND season = ? AND episode = ?',
|
|
|
|
[curProper.indexerid, curProper.season, curProper.episode])
|
|
|
|
|
|
|
|
oldVersion = int(sqlResults[0]['version'])
|
|
|
|
oldRelease_group = (sqlResults[0]['release_group'])
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
if oldVersion > -1 and oldVersion < curProper.version:
|
|
|
|
logger.log('Found new anime v' + str(curProper.version) + ' to replace existing v' + str(oldVersion))
|
|
|
|
else:
|
2014-03-10 05:18:05 +00:00
|
|
|
continue
|
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
if oldRelease_group != curProper.release_group:
|
|
|
|
logger.log('Skipping proper from release group: ' + curProper.release_group + ', does not match existing release group: ' + oldRelease_group)
|
|
|
|
continue
|
2014-07-22 04:53:32 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# if the show is in our list and there hasn't been a proper already added for that particular episode then add it to our list of propers
|
|
|
|
if curProper.indexerid != -1 and (curProper.indexerid, curProper.season, curProper.episode) not in map(
|
|
|
|
operator.attrgetter('indexerid', 'season', 'episode'), finalPropers):
|
|
|
|
logger.log(u'Found a proper that we need: ' + str(curProper.name))
|
|
|
|
finalPropers.append(curProper)
|
2014-07-22 04:53:32 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
return finalPropers
|
2014-07-22 04:53:32 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
def _downloadPropers(properList):
|
2014-07-22 04:53:32 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
for curProper in properList:
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
historyLimit = datetime.datetime.today() - datetime.timedelta(days=30)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# make sure the episode has been downloaded before
|
|
|
|
myDB = db.DBConnection()
|
|
|
|
historyResults = myDB.select(
|
|
|
|
'SELECT resource FROM history '
|
|
|
|
'WHERE showid = ? AND season = ? AND episode = ? AND quality = ? AND date >= ? '
|
|
|
|
'AND action IN (' + ','.join([str(x) for x in Quality.SNATCHED]) + ')',
|
|
|
|
[curProper.indexerid, curProper.season, curProper.episode, curProper.quality,
|
|
|
|
historyLimit.strftime(history.dateFormat)])
|
|
|
|
|
|
|
|
# if we didn't download this episode in the first place we don't know what quality to use for the proper so we can't do it
|
|
|
|
if len(historyResults) == 0:
|
|
|
|
logger.log(
|
|
|
|
u'Unable to find an original history entry for proper ' + curProper.name + ' so I\'m not downloading it.')
|
|
|
|
continue
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
else:
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-11-25 12:12:40 +00:00
|
|
|
# get the show object
|
|
|
|
showObj = helpers.findCertainShow(sickbeard.showList, curProper.indexerid)
|
|
|
|
if showObj == None:
|
|
|
|
logger.log(u'Unable to find the show with indexerid ' + str(
|
|
|
|
curProper.indexerid) + ' so unable to download the proper', logger.ERROR)
|
|
|
|
continue
|
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# make sure that none of the existing history downloads are the same proper we're trying to download
|
2015-11-25 12:12:40 +00:00
|
|
|
clean_proper_name = _genericName(helpers.remove_non_release_groups(curProper.name, showObj.anime))
|
2015-05-04 19:14:29 +00:00
|
|
|
isSame = False
|
|
|
|
for curResult in historyResults:
|
|
|
|
# if the result exists in history already we need to skip it
|
|
|
|
if _genericName(helpers.remove_non_release_groups(curResult['resource'])) == clean_proper_name:
|
|
|
|
isSame = True
|
|
|
|
break
|
|
|
|
if isSame:
|
|
|
|
logger.log(u'This proper is already in history, skipping it', logger.DEBUG)
|
|
|
|
continue
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-11-25 12:12:40 +00:00
|
|
|
|
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
epObj = showObj.getEpisode(curProper.season, curProper.episode)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# make the result object
|
2015-07-13 09:39:20 +00:00
|
|
|
result = curProper.provider.get_result([epObj], curProper.url)
|
|
|
|
if None is result:
|
|
|
|
continue
|
2015-05-04 19:14:29 +00:00
|
|
|
result.name = curProper.name
|
|
|
|
result.quality = curProper.quality
|
|
|
|
result.version = curProper.version
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
# snatch it
|
|
|
|
search.snatchEpisode(result, SNATCHED_PROPER)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
def _genericName(name):
|
|
|
|
return name.replace('.', ' ').replace('-', ' ').replace('_', ' ').lower()
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
def _set_lastProperSearch(when):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
logger.log(u'Setting the last Proper search in the DB to ' + str(when), logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
myDB = db.DBConnection()
|
|
|
|
sqlResults = myDB.select('SELECT * FROM info')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
if len(sqlResults) == 0:
|
|
|
|
myDB.action('INSERT INTO info (last_backlog, last_indexer, last_proper_search) VALUES (?,?,?)',
|
|
|
|
[0, 0, str(when)])
|
|
|
|
else:
|
|
|
|
myDB.action('UPDATE info SET last_proper_search=' + str(when))
|
|
|
|
|
|
|
|
def _get_lastProperSearch():
|
|
|
|
|
|
|
|
myDB = db.DBConnection()
|
|
|
|
sqlResults = myDB.select('SELECT * FROM info')
|
|
|
|
|
|
|
|
try:
|
|
|
|
last_proper_search = datetime.date.fromordinal(int(sqlResults[0]['last_proper_search']))
|
|
|
|
except:
|
|
|
|
return datetime.date.fromordinal(1)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-05-04 19:14:29 +00:00
|
|
|
return last_proper_search
|