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
|
2014-05-26 06:42:34 -04:00
|
|
|
# GNU General Public License for more details.
|
2014-03-10 01:18:05 -04:00
|
|
|
#
|
|
|
|
# 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-05-11 08:49:07 -04:00
|
|
|
|
|
|
|
from __future__ import with_statement
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
import sickbeard
|
|
|
|
|
|
|
|
from sickbeard import db
|
|
|
|
from sickbeard import logger
|
2014-07-31 00:31:54 -04:00
|
|
|
from sickbeard.common import Quality
|
2014-07-31 00:47:17 -04:00
|
|
|
|
|
|
|
from sickbeard import helpers, show_name_helpers
|
2014-05-11 08:49:07 -04:00
|
|
|
from sickbeard.exceptions import MultipleShowObjectsException
|
2014-05-04 08:05:27 -04:00
|
|
|
from sickbeard.exceptions import AuthException
|
2014-07-31 00:31:54 -04:00
|
|
|
from name_parser.parser import NameParser, InvalidNameException, InvalidShowException
|
2014-07-31 00:47:17 -04:00
|
|
|
from sickbeard.rssfeeds import RSSFeeds
|
|
|
|
from sickbeard import clients
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
class CacheDBConnection(db.DBConnection):
|
|
|
|
def __init__(self, providerName):
|
|
|
|
db.DBConnection.__init__(self, "cache.db")
|
|
|
|
|
2014-06-07 19:16:01 -04:00
|
|
|
# Create the table if it's not already there
|
|
|
|
try:
|
|
|
|
if not self.hasTable(providerName):
|
2014-06-26 00:39:34 -04:00
|
|
|
self.action(
|
2014-07-06 07:10:25 -04:00
|
|
|
"CREATE TABLE [" + providerName + "] (name TEXT, season NUMERIC, episodes TEXT, indexerid NUMERIC, url TEXT, time NUMERIC, quality TEXT, release_group TEXT)")
|
2014-06-26 06:29:05 -04:00
|
|
|
else:
|
2014-06-27 23:33:31 -04:00
|
|
|
sqlResults = self.select(
|
|
|
|
"SELECT url, COUNT(url) as count FROM [" + providerName + "] GROUP BY url HAVING count > 1")
|
|
|
|
|
|
|
|
for cur_dupe in sqlResults:
|
|
|
|
self.action("DELETE FROM [" + providerName + "] WHERE url = ?", [cur_dupe["url"]])
|
|
|
|
|
2014-06-29 01:54:29 -04:00
|
|
|
# add unique index to prevent further dupes from happening if one does not exist
|
|
|
|
self.action("CREATE UNIQUE INDEX IF NOT EXISTS idx_url ON " + providerName + " (url)")
|
2014-07-06 07:10:25 -04:00
|
|
|
|
|
|
|
# add release_group column to table if missing
|
|
|
|
if not self.hasColumn(providerName, 'release_group'):
|
|
|
|
self.addColumn(providerName, 'release_group', "TEXT", "")
|
2014-07-14 22:00:53 -04:00
|
|
|
|
2014-07-22 00:53:32 -04:00
|
|
|
# add version column to table if missing
|
|
|
|
if not self.hasColumn(providerName, 'version'):
|
|
|
|
self.addColumn(providerName, 'version', "NUMERIC", "-1")
|
|
|
|
|
2014-06-07 19:16:01 -04:00
|
|
|
except Exception, e:
|
|
|
|
if str(e) != "table [" + providerName + "] already exists":
|
|
|
|
raise
|
|
|
|
|
|
|
|
# Create the table if it's not already there
|
|
|
|
try:
|
|
|
|
if not self.hasTable('lastUpdate'):
|
|
|
|
self.action("CREATE TABLE lastUpdate (provider TEXT, time NUMERIC)")
|
|
|
|
except Exception, e:
|
|
|
|
if str(e) != "table lastUpdate already exists":
|
|
|
|
raise
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
class TVCache():
|
2014-03-10 01:18:05 -04:00
|
|
|
def __init__(self, provider):
|
|
|
|
|
|
|
|
self.provider = provider
|
|
|
|
self.providerID = self.provider.getID()
|
2014-07-19 17:16:05 -04:00
|
|
|
self.providerDB = None
|
2014-03-10 01:18:05 -04:00
|
|
|
self.minTime = 10
|
|
|
|
|
|
|
|
def _getDB(self):
|
2014-07-19 17:16:05 -04:00
|
|
|
# init provider database if not done already
|
|
|
|
if not self.providerDB:
|
|
|
|
self.providerDB = CacheDBConnection(self.providerID)
|
|
|
|
|
|
|
|
return self.providerDB
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def _clearCache(self):
|
2014-06-29 06:05:33 -04:00
|
|
|
if self.shouldClearCache():
|
|
|
|
myDB = self._getDB()
|
2014-08-30 04:47:00 -04:00
|
|
|
myDB.action("DELETE FROM [" + self.providerID + "] WHERE 1")
|
2014-05-18 08:59:42 -04:00
|
|
|
|
2014-08-14 23:43:11 -04:00
|
|
|
def _get_title_and_url(self, item):
|
|
|
|
# override this in the provider if daily search has a different data layout to backlog searches
|
|
|
|
return self.provider._get_title_and_url(item)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def _getRSSData(self):
|
|
|
|
data = None
|
|
|
|
return data
|
|
|
|
|
2014-08-12 06:09:11 -04:00
|
|
|
def _checkAuth(self):
|
|
|
|
return self.provider._checkAuth()
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def _checkItemAuth(self, title, url):
|
|
|
|
return True
|
|
|
|
|
2014-05-11 08:49:07 -04:00
|
|
|
def updateCache(self):
|
2014-08-12 06:09:11 -04:00
|
|
|
if self.shouldUpdate() and self._checkAuth():
|
2014-05-11 08:49:07 -04:00
|
|
|
# as long as the http request worked we count this as an update
|
2014-08-30 04:47:00 -04:00
|
|
|
data = self._getRSSData()
|
2014-07-27 06:59:21 -04:00
|
|
|
if not data:
|
2014-05-11 08:49:07 -04:00
|
|
|
return []
|
|
|
|
|
2014-07-27 06:59:21 -04:00
|
|
|
# clear cache
|
|
|
|
self._clearCache()
|
|
|
|
|
|
|
|
# set updated
|
|
|
|
self.setLastUpdate()
|
|
|
|
|
|
|
|
# parse data
|
2014-08-12 06:09:11 -04:00
|
|
|
cl = []
|
|
|
|
for item in data:
|
2014-08-14 23:43:11 -04:00
|
|
|
title, url = self._get_title_and_url(item)
|
2014-08-12 06:09:11 -04:00
|
|
|
ci = self._parseItem(title, url)
|
|
|
|
if ci is not None:
|
|
|
|
cl.append(ci)
|
|
|
|
|
|
|
|
if len(cl) > 0:
|
|
|
|
myDB = self._getDB()
|
|
|
|
myDB.mass_action(cl)
|
2014-05-11 08:49:07 -04:00
|
|
|
|
|
|
|
return []
|
|
|
|
|
2014-06-29 23:50:12 -04:00
|
|
|
def getRSSFeed(self, url, post_data=None, request_headers=None):
|
2014-06-30 13:48:18 -04:00
|
|
|
return RSSFeeds(self.providerID).getFeed(url, post_data, request_headers)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def _translateTitle(self, title):
|
2014-07-21 09:29:07 -04:00
|
|
|
return u'' + title.replace(' ', '.')
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def _translateLinkURL(self, url):
|
|
|
|
return url.replace('&', '&')
|
|
|
|
|
2014-08-05 12:19:54 -04:00
|
|
|
def _parseItem(self, title, url):
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
self._checkItemAuth(title, url)
|
|
|
|
|
|
|
|
if title and url:
|
|
|
|
title = self._translateTitle(title)
|
|
|
|
url = self._translateLinkURL(url)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-08-05 12:19:54 -04:00
|
|
|
logger.log(u"Attempting to add item to cache: " + title, logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
return self._addCacheEntry(title, url)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(
|
2014-04-26 00:08:27 -04:00
|
|
|
u"The data returned from the " + self.provider.name + " feed is incomplete, this result is unusable",
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.DEBUG)
|
|
|
|
return None
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _getLastUpdate(self):
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = self._getDB()
|
|
|
|
sqlResults = myDB.select("SELECT time FROM lastUpdate WHERE provider = ?", [self.providerID])
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
if sqlResults:
|
|
|
|
lastTime = int(sqlResults[0]["time"])
|
2014-03-11 16:22:00 -04:00
|
|
|
if lastTime > int(time.mktime(datetime.datetime.today().timetuple())):
|
|
|
|
lastTime = 0
|
2014-03-10 01:18:05 -04:00
|
|
|
else:
|
|
|
|
lastTime = 0
|
|
|
|
|
|
|
|
return datetime.datetime.fromtimestamp(lastTime)
|
|
|
|
|
2014-05-15 03:20:00 -04:00
|
|
|
def _getLastSearch(self):
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = self._getDB()
|
|
|
|
sqlResults = myDB.select("SELECT time FROM lastSearch WHERE provider = ?", [self.providerID])
|
2014-05-15 03:20:00 -04:00
|
|
|
|
|
|
|
if sqlResults:
|
|
|
|
lastTime = int(sqlResults[0]["time"])
|
|
|
|
if lastTime > int(time.mktime(datetime.datetime.today().timetuple())):
|
|
|
|
lastTime = 0
|
|
|
|
else:
|
|
|
|
lastTime = 0
|
|
|
|
|
|
|
|
return datetime.datetime.fromtimestamp(lastTime)
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-11 08:49:07 -04:00
|
|
|
def setLastUpdate(self, toDate=None):
|
2014-03-10 01:18:05 -04:00
|
|
|
if not toDate:
|
|
|
|
toDate = datetime.datetime.today()
|
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = self._getDB()
|
|
|
|
myDB.upsert("lastUpdate",
|
|
|
|
{'time': int(time.mktime(toDate.timetuple()))},
|
|
|
|
{'provider': self.providerID})
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-15 03:20:00 -04:00
|
|
|
def setLastSearch(self, toDate=None):
|
|
|
|
if not toDate:
|
|
|
|
toDate = datetime.datetime.today()
|
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = self._getDB()
|
|
|
|
myDB.upsert("lastSearch",
|
|
|
|
{'time': int(time.mktime(toDate.timetuple()))},
|
|
|
|
{'provider': self.providerID})
|
2014-05-11 08:49:07 -04:00
|
|
|
|
2014-05-15 03:20:00 -04:00
|
|
|
lastUpdate = property(_getLastUpdate)
|
|
|
|
lastSearch = property(_getLastSearch)
|
2014-05-11 08:49:07 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
def shouldUpdate(self):
|
|
|
|
# if we've updated recently then skip the update
|
2014-06-30 11:57:32 -04:00
|
|
|
if datetime.datetime.today() - self.lastUpdate < datetime.timedelta(minutes=self.minTime):
|
|
|
|
logger.log(u"Last update was too soon, using old cache: today()-" + str(self.lastUpdate) + "<" + str(
|
|
|
|
datetime.timedelta(minutes=self.minTime)), logger.DEBUG)
|
|
|
|
return False
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-05-15 03:20:00 -04:00
|
|
|
def shouldClearCache(self):
|
|
|
|
# if daily search hasn't used our previous results yet then don't clear the cache
|
|
|
|
if self.lastUpdate > self.lastSearch:
|
|
|
|
logger.log(
|
2014-06-30 11:57:32 -04:00
|
|
|
u"Daily search has not yet used our last cache results, not clearing cache ...", logger.DEBUG)
|
2014-05-15 03:20:00 -04:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-07-22 02:00:58 -04:00
|
|
|
def _addCacheEntry(self, name, url, parse_result=None, indexer_id=0):
|
2014-07-14 22:00:53 -04:00
|
|
|
|
2014-07-22 02:00:58 -04:00
|
|
|
# check if we passed in a parsed result or should we try and create one
|
|
|
|
if not parse_result:
|
2014-05-26 16:16:07 -04:00
|
|
|
|
2014-07-22 02:00:58 -04:00
|
|
|
# create showObj from indexer_id if available
|
|
|
|
showObj=None
|
|
|
|
if indexer_id:
|
|
|
|
showObj = helpers.findCertainShow(sickbeard.showList, indexer_id)
|
2014-04-30 18:07:18 -04:00
|
|
|
|
2014-07-22 02:00:58 -04:00
|
|
|
try:
|
|
|
|
myParser = NameParser(showObj=showObj, convert=True)
|
|
|
|
parse_result = myParser.parse(name)
|
|
|
|
except InvalidNameException:
|
|
|
|
logger.log(u"Unable to parse the filename " + name + " into a valid episode", logger.DEBUG)
|
|
|
|
return None
|
|
|
|
except InvalidShowException:
|
|
|
|
logger.log(u"Unable to parse the filename " + name + " into a valid show", logger.DEBUG)
|
|
|
|
return None
|
|
|
|
|
|
|
|
if not parse_result or not parse_result.series_name:
|
|
|
|
return None
|
2014-04-28 19:03:49 -04:00
|
|
|
|
2014-07-22 02:00:58 -04:00
|
|
|
# if we made it this far then lets add the parsed result to cache for usager later on
|
2014-07-24 14:16:59 -04:00
|
|
|
season = parse_result.season_number if parse_result.season_number else 1
|
|
|
|
episodes = parse_result.episode_numbers
|
2014-04-26 01:42:40 -04:00
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
if season and episodes:
|
|
|
|
# store episodes as a seperated string
|
|
|
|
episodeText = "|" + "|".join(map(str, episodes)) + "|"
|
2014-04-29 09:14:19 -04:00
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
# get the current timestamp
|
|
|
|
curTimestamp = int(time.mktime(datetime.datetime.today().timetuple()))
|
2014-04-30 18:07:18 -04:00
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
# get quality of release
|
2014-07-22 02:00:58 -04:00
|
|
|
quality = parse_result.quality
|
2014-04-30 18:07:18 -04:00
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
if not isinstance(name, unicode):
|
2014-07-14 22:00:53 -04:00
|
|
|
name = unicode(name, 'utf-8', 'replace')
|
2014-05-03 05:23:26 -04:00
|
|
|
|
2014-07-06 07:10:25 -04:00
|
|
|
# get release group
|
|
|
|
release_group = parse_result.release_group
|
|
|
|
|
2014-07-22 00:53:32 -04:00
|
|
|
# get version
|
|
|
|
version = parse_result.version
|
|
|
|
|
2014-05-03 05:23:26 -04:00
|
|
|
logger.log(u"Added RSS item: [" + name + "] to cache: [" + self.providerID + "]", logger.DEBUG)
|
2014-05-11 08:49:07 -04:00
|
|
|
|
2014-05-04 08:05:27 -04:00
|
|
|
return [
|
2014-07-22 00:53:32 -04:00
|
|
|
"INSERT OR IGNORE INTO [" + self.providerID + "] (name, season, episodes, indexerid, url, time, quality, release_group, version) VALUES (?,?,?,?,?,?,?,?,?)",
|
|
|
|
[name, season, episodeText, parse_result.show.indexerid, url, curTimestamp, quality, release_group, version]]
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-04 08:05:27 -04:00
|
|
|
|
2014-08-30 04:47:00 -04:00
|
|
|
def searchCache(self, episode, manualSearch=False):
|
|
|
|
neededEps = self.findNeededEpisodes(episode, manualSearch)
|
|
|
|
return neededEps[episode]
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-11 08:49:07 -04:00
|
|
|
def listPropers(self, date=None, delimiter="."):
|
2014-06-21 18:46:59 -04:00
|
|
|
myDB = self._getDB()
|
|
|
|
sql = "SELECT * FROM [" + self.providerID + "] WHERE name LIKE '%.PROPER.%' OR name LIKE '%.REPACK.%'"
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
if date != None:
|
|
|
|
sql += " AND time >= " + str(int(time.mktime(date.timetuple())))
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-06-21 18:46:59 -04:00
|
|
|
return filter(lambda x: x['indexerid'] != 0, myDB.select(sql))
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-11 08:49:07 -04:00
|
|
|
|
2014-08-30 04:47:00 -04:00
|
|
|
def findNeededEpisodes(self, episode=None, manualSearch=False):
|
2014-03-10 01:18:05 -04:00
|
|
|
neededEps = {}
|
|
|
|
|
2014-08-30 04:47:00 -04:00
|
|
|
if episode:
|
|
|
|
neededEps[episode] = []
|
|
|
|
|
|
|
|
myDB = self._getDB()
|
|
|
|
if not episode:
|
|
|
|
sqlResults = myDB.select("SELECT * FROM [" + self.providerID + "]")
|
|
|
|
else:
|
2014-06-21 18:46:59 -04:00
|
|
|
sqlResults = myDB.select(
|
|
|
|
"SELECT * FROM [" + self.providerID + "] WHERE indexerid = ? AND season = ? AND episodes LIKE ?",
|
2014-08-30 04:47:00 -04:00
|
|
|
[episode.show.indexerid, episode.season, "%|" + str(episode.episode) + "|%"])
|
|
|
|
|
|
|
|
# for each cache entry
|
|
|
|
for curResult in sqlResults:
|
|
|
|
|
|
|
|
# skip non-tv crap
|
|
|
|
if not show_name_helpers.filterBadReleases(curResult["name"]):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# get the show object, or if it's not one of our shows then ignore it
|
|
|
|
showObj = helpers.findCertainShow(sickbeard.showList, int(curResult["indexerid"]))
|
|
|
|
if not showObj:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# skip if provider is anime only and show is not anime
|
|
|
|
if self.provider.anime_only and not showObj.is_anime:
|
|
|
|
logger.log(u"" + str(showObj.name) + " is not an anime, skiping", logger.DEBUG)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# get season and ep data (ignoring multi-eps for now)
|
|
|
|
curSeason = int(curResult["season"])
|
|
|
|
if curSeason == -1:
|
|
|
|
continue
|
|
|
|
curEp = curResult["episodes"].split("|")[1]
|
|
|
|
if not curEp:
|
|
|
|
continue
|
|
|
|
curEp = int(curEp)
|
|
|
|
|
|
|
|
curQuality = int(curResult["quality"])
|
|
|
|
curReleaseGroup = curResult["release_group"]
|
|
|
|
curVersion = curResult["version"]
|
|
|
|
|
|
|
|
# if the show says we want that episode then add it to the list
|
|
|
|
if not showObj.wantEpisode(curSeason, curEp, curQuality, manualSearch):
|
|
|
|
logger.log(u"Skipping " + curResult["name"] + " because we don't want an episode that's " +
|
|
|
|
Quality.qualityStrings[curQuality], logger.DEBUG)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# build name cache for show
|
|
|
|
sickbeard.name_cache.buildNameCache(showObj)
|
|
|
|
|
|
|
|
if episode:
|
|
|
|
epObj = episode
|
|
|
|
else:
|
|
|
|
epObj = showObj.getEpisode(curSeason, curEp)
|
|
|
|
|
|
|
|
# build a result object
|
|
|
|
title = curResult["name"]
|
|
|
|
url = curResult["url"]
|
|
|
|
|
|
|
|
logger.log(u"Found result " + title + " at " + url)
|
|
|
|
|
|
|
|
result = self.provider.getResult([epObj])
|
|
|
|
result.show = showObj
|
|
|
|
result.url = url
|
|
|
|
result.name = title
|
|
|
|
result.quality = curQuality
|
|
|
|
result.release_group = curReleaseGroup
|
|
|
|
result.version = curVersion
|
|
|
|
result.content = None
|
|
|
|
|
|
|
|
# add it to the list
|
|
|
|
if epObj not in neededEps:
|
|
|
|
neededEps[epObj] = [result]
|
|
|
|
else:
|
|
|
|
neededEps[epObj].append(result)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-15 03:20:00 -04:00
|
|
|
# datetime stamp this search so cache gets cleared
|
|
|
|
self.setLastSearch()
|
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
return neededEps
|
2014-05-11 08:49:07 -04:00
|
|
|
|