2014-03-10 01:18:05 -04:00
|
|
|
# Author: Tyler Fenby <tylerfenby@gmail.com>
|
|
|
|
# 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
|
|
|
|
|
|
|
import re
|
|
|
|
import urllib
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from sickbeard import db
|
|
|
|
from sickbeard import logger
|
2014-05-13 07:16:32 -04:00
|
|
|
from sickbeard.exceptions import ex, EpisodeNotFoundException
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard.history import dateFormat
|
2014-03-19 04:34:46 -04:00
|
|
|
from sickbeard.common import Quality
|
|
|
|
from sickbeard.common import WANTED, FAILED
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
def prepareFailedName(release):
|
|
|
|
"""Standardizes release name for failed DB"""
|
|
|
|
|
|
|
|
fixed = urllib.unquote(release)
|
2014-03-25 01:57:24 -04:00
|
|
|
if (fixed.endswith(".nzb")):
|
2014-03-10 01:18:05 -04:00
|
|
|
fixed = fixed.rpartition(".")[0]
|
|
|
|
|
|
|
|
fixed = re.sub("[\.\-\+\ ]", "_", fixed)
|
2014-05-13 07:16:32 -04:00
|
|
|
|
|
|
|
if not isinstance(fixed, unicode):
|
2014-07-14 22:00:53 -04:00
|
|
|
fixed = unicode(fixed, 'utf-8', 'replace')
|
2014-05-13 07:16:32 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return fixed
|
|
|
|
|
|
|
|
|
|
|
|
def logFailed(release):
|
|
|
|
log_str = u""
|
|
|
|
size = -1
|
|
|
|
provider = ""
|
|
|
|
|
|
|
|
release = prepareFailedName(release)
|
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection('failed.db')
|
|
|
|
sql_results = myDB.select("SELECT * FROM history WHERE release=?", [release])
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
if len(sql_results) == 0:
|
2014-05-13 07:16:32 -04:00
|
|
|
logger.log(
|
2014-05-19 11:47:01 -04:00
|
|
|
u"Release not found in snatch history.", logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
elif len(sql_results) > 1:
|
2014-05-13 07:16:32 -04:00
|
|
|
logger.log(u"Multiple logged snatches found for release", logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
sizes = len(set(x["size"] for x in sql_results))
|
|
|
|
providers = len(set(x["provider"] for x in sql_results))
|
|
|
|
if sizes == 1:
|
2014-05-13 07:16:32 -04:00
|
|
|
logger.log(u"However, they're all the same size. Continuing with found size.", logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
size = sql_results[0]["size"]
|
|
|
|
else:
|
2014-05-13 07:16:32 -04:00
|
|
|
logger.log(
|
2014-03-25 01:57:24 -04:00
|
|
|
u"They also vary in size. Deleting the logged snatches and recording this release with no size/provider",
|
|
|
|
logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
for result in sql_results:
|
|
|
|
deleteLoggedSnatch(result["release"], result["size"], result["provider"])
|
|
|
|
|
|
|
|
if providers == 1:
|
2014-05-13 07:16:32 -04:00
|
|
|
logger.log(u"They're also from the same provider. Using it as well.")
|
2014-03-10 01:18:05 -04:00
|
|
|
provider = sql_results[0]["provider"]
|
|
|
|
else:
|
|
|
|
size = sql_results[0]["size"]
|
|
|
|
provider = sql_results[0]["provider"]
|
|
|
|
|
|
|
|
if not hasFailed(release, size, provider):
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection('failed.db')
|
|
|
|
myDB.action("INSERT INTO failed (release, size, provider) VALUES (?, ?, ?)", [release, size, provider])
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
deleteLoggedSnatch(release, size, provider)
|
|
|
|
|
|
|
|
return log_str
|
|
|
|
|
|
|
|
|
|
|
|
def logSuccess(release):
|
|
|
|
release = prepareFailedName(release)
|
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection('failed.db')
|
|
|
|
myDB.action("DELETE FROM history WHERE release=?", [release])
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
def hasFailed(release, size, provider="%"):
|
|
|
|
"""
|
|
|
|
Returns True if a release has previously failed.
|
|
|
|
|
|
|
|
If provider is given, return True only if the release is found
|
|
|
|
with that specific provider. Otherwise, return True if the release
|
|
|
|
is found with any provider.
|
|
|
|
"""
|
|
|
|
|
2014-05-13 12:30:25 -04:00
|
|
|
release = prepareFailedName(release)
|
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection('failed.db')
|
|
|
|
sql_results = myDB.select(
|
|
|
|
"SELECT * FROM failed WHERE release=? AND size=? AND provider LIKE ?",
|
|
|
|
[release, size, provider])
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
return (len(sql_results) > 0)
|
|
|
|
|
2014-06-07 17:32:38 -04:00
|
|
|
|
2014-05-13 07:16:32 -04:00
|
|
|
def revertEpisode(epObj):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""Restore the episodes of a failed download to their original state"""
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection('failed.db')
|
|
|
|
sql_results = myDB.select("SELECT * FROM history WHERE showid=? AND season=?",
|
|
|
|
[epObj.show.indexerid, epObj.season])
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
history_eps = dict([(res["episode"], res) for res in sql_results])
|
|
|
|
|
2014-05-13 07:16:32 -04:00
|
|
|
try:
|
|
|
|
logger.log(u"Reverting episode (%s, %s): %s" % (epObj.season, epObj.episode, epObj.name))
|
|
|
|
with epObj.lock:
|
|
|
|
if epObj.episode in history_eps:
|
|
|
|
logger.log(u"Found in history")
|
|
|
|
epObj.status = history_eps[epObj.episode]['old_status']
|
|
|
|
else:
|
|
|
|
logger.log(u"WARNING: Episode not found in history. Setting it back to WANTED",
|
2014-06-07 17:32:38 -04:00
|
|
|
logger.WARNING)
|
2014-05-13 07:16:32 -04:00
|
|
|
epObj.status = WANTED
|
|
|
|
epObj.saveToDB()
|
2014-03-19 10:59:34 -04:00
|
|
|
|
2014-05-13 07:16:32 -04:00
|
|
|
except EpisodeNotFoundException, e:
|
|
|
|
logger.log(u"Unable to create episode, please set its status manually: " + ex(e),
|
2014-06-07 17:32:38 -04:00
|
|
|
logger.WARNING)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-13 07:16:32 -04:00
|
|
|
def markFailed(epObj):
|
|
|
|
log_str = u""
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-13 07:16:32 -04:00
|
|
|
try:
|
|
|
|
with epObj.lock:
|
|
|
|
quality = Quality.splitCompositeStatus(epObj.status)[1]
|
|
|
|
epObj.status = Quality.compositeStatus(FAILED, quality)
|
|
|
|
epObj.saveToDB()
|
2014-03-19 10:59:34 -04:00
|
|
|
|
2014-05-13 07:16:32 -04:00
|
|
|
except EpisodeNotFoundException, e:
|
|
|
|
logger.log(u"Unable to get episode, please set its status manually: " + ex(e), logger.WARNING)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
return log_str
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def logSnatch(searchResult):
|
|
|
|
logDate = datetime.datetime.today().strftime(dateFormat)
|
|
|
|
release = prepareFailedName(searchResult.name)
|
|
|
|
|
|
|
|
providerClass = searchResult.provider
|
|
|
|
if providerClass is not None:
|
|
|
|
provider = providerClass.name
|
|
|
|
else:
|
|
|
|
provider = "unknown"
|
|
|
|
|
|
|
|
show_obj = searchResult.episodes[0].show
|
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection('failed.db')
|
|
|
|
for episode in searchResult.episodes:
|
|
|
|
myDB.action(
|
|
|
|
"INSERT INTO history (date, size, release, provider, showid, season, episode, old_status)"
|
|
|
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
|
|
[logDate, searchResult.size, release, provider, show_obj.indexerid, episode.season, episode.episode,
|
|
|
|
episode.status])
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
def deleteLoggedSnatch(release, size, provider):
|
|
|
|
release = prepareFailedName(release)
|
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection('failed.db')
|
|
|
|
myDB.action("DELETE FROM history WHERE release=? AND size=? AND provider=?",
|
|
|
|
[release, size, provider])
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
def trimHistory():
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection('failed.db')
|
|
|
|
myDB.action("DELETE FROM history WHERE date < " + str(
|
|
|
|
(datetime.datetime.today() - datetime.timedelta(days=30)).strftime(dateFormat)))
|
2014-06-07 17:32:38 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-13 07:16:32 -04:00
|
|
|
def findRelease(epObj):
|
2014-03-10 01:18:05 -04:00
|
|
|
"""
|
|
|
|
Find releases in history by show ID and season.
|
|
|
|
Return None for release if multiple found or no release found.
|
|
|
|
"""
|
|
|
|
|
|
|
|
release = None
|
|
|
|
provider = None
|
|
|
|
|
|
|
|
|
|
|
|
# Clear old snatches for this release if any exist
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = db.DBConnection('failed.db')
|
|
|
|
myDB.action("DELETE FROM history WHERE showid=" + str(epObj.show.indexerid) + " AND season=" + str(
|
|
|
|
epObj.season) + " AND episode=" + str(
|
|
|
|
epObj.episode) + " AND date < (SELECT max(date) FROM history WHERE showid=" + str(
|
|
|
|
epObj.show.indexerid) + " AND season=" + str(epObj.season) + " AND episode=" + str(epObj.episode) + ")")
|
|
|
|
|
|
|
|
# Search for release in snatch history
|
|
|
|
results = myDB.select("SELECT release, provider, date FROM history WHERE showid=? AND season=? AND episode=?",
|
|
|
|
[epObj.show.indexerid, epObj.season, epObj.episode])
|
|
|
|
|
|
|
|
for result in results:
|
|
|
|
release = str(result["release"])
|
|
|
|
provider = str(result["provider"])
|
|
|
|
date = result["date"]
|
|
|
|
|
|
|
|
# Clear any incomplete snatch records for this release if any exist
|
|
|
|
myDB.action("DELETE FROM history WHERE release=? AND date!=?", [release, date])
|
|
|
|
|
|
|
|
# Found a previously failed release
|
|
|
|
logger.log(u"Failed release found for season (%s): (%s)" % (epObj.season, result["release"]), logger.DEBUG)
|
|
|
|
return (release, provider)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# Release was not found
|
2014-05-13 07:16:32 -04:00
|
|
|
logger.log(u"No releases found for season (%s) of (%s)" % (epObj.season, epObj.show.indexerid), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
return (release, provider)
|