2014-03-10 05:18:05 +00:00
|
|
|
# Author: Nic Wolfe <nic@wolfeden.ca>
|
|
|
|
# URL: http://code.google.com/p/sickbeard/
|
|
|
|
#
|
|
|
|
# This file is part of Sick Beard.
|
|
|
|
#
|
|
|
|
# Sick Beard 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.
|
|
|
|
#
|
|
|
|
# Sick Beard 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 Sick Beard. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import urllib
|
2014-05-17 09:27:17 +00:00
|
|
|
import time
|
2014-03-10 05:18:05 +00:00
|
|
|
import datetime
|
|
|
|
import os
|
|
|
|
|
|
|
|
try:
|
|
|
|
import xml.etree.cElementTree as etree
|
|
|
|
except ImportError:
|
|
|
|
import elementtree.ElementTree as etree
|
|
|
|
|
|
|
|
import sickbeard
|
|
|
|
import generic
|
|
|
|
|
|
|
|
from sickbeard import classes
|
|
|
|
from sickbeard import helpers
|
|
|
|
from sickbeard import scene_exceptions
|
|
|
|
from sickbeard import encodingKludge as ek
|
2014-05-17 11:40:26 +00:00
|
|
|
from sickbeard.common import cpu_presets
|
2014-03-10 05:18:05 +00:00
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard import tvcache
|
|
|
|
from sickbeard.exceptions import ex, AuthException
|
|
|
|
|
|
|
|
|
|
|
|
class NewznabProvider(generic.NZBProvider):
|
2014-05-17 05:23:11 +00:00
|
|
|
def __init__(self, name, url, key='', catIDs='5030,5040,5060', search_mode='eponly', search_fallback=False):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
generic.NZBProvider.__init__(self, name)
|
|
|
|
|
|
|
|
self.cache = NewznabCache(self)
|
|
|
|
|
|
|
|
self.url = url
|
|
|
|
|
|
|
|
self.key = key
|
|
|
|
|
2014-05-17 05:23:11 +00:00
|
|
|
self.search_mode = search_mode
|
|
|
|
self.search_fallback = search_fallback
|
|
|
|
|
|
|
|
if self.search_fallback == '0':
|
|
|
|
self.search_fallback = False
|
|
|
|
else:
|
|
|
|
self.search_fallback = True
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
# a 0 in the key spot indicates that no key is needed
|
|
|
|
if self.key == '0':
|
|
|
|
self.needs_auth = False
|
|
|
|
else:
|
|
|
|
self.needs_auth = True
|
|
|
|
|
|
|
|
if catIDs:
|
|
|
|
self.catIDs = catIDs
|
|
|
|
else:
|
|
|
|
self.catIDs = '5030,5040, 5060'
|
|
|
|
|
|
|
|
self.enabled = True
|
|
|
|
self.supportsBacklog = True
|
|
|
|
|
|
|
|
self.default = False
|
|
|
|
|
|
|
|
def configStr(self):
|
2014-05-17 05:23:11 +00:00
|
|
|
return self.name + '|' + self.url + '|' + self.key + '|' + self.catIDs + '|' + str(int(self.enabled)) + '|' + self.search_mode + '|' + str(int(self.search_fallback))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def imageName(self):
|
2014-03-25 05:57:24 +00:00
|
|
|
if ek.ek(os.path.isfile,
|
2014-05-17 23:17:16 +00:00
|
|
|
ek.ek(os.path.join, sickbeard.PROG_DIR, 'gui', 'slick', 'images', 'providers', self.getID() + '.png')):
|
2014-03-10 05:18:05 +00:00
|
|
|
return self.getID() + '.png'
|
|
|
|
return 'newznab.png'
|
|
|
|
|
|
|
|
def isEnabled(self):
|
|
|
|
return self.enabled
|
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
def _get_season_search_strings(self, ep_obj):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
to_return = []
|
|
|
|
|
|
|
|
# add new query strings for exceptions
|
2014-04-29 13:14:19 +00:00
|
|
|
name_exceptions = scene_exceptions.get_scene_exceptions(self.show.indexerid) + [self.show.name]
|
2014-03-10 05:18:05 +00:00
|
|
|
for cur_exception in name_exceptions:
|
|
|
|
|
|
|
|
cur_params = {}
|
|
|
|
|
|
|
|
# search
|
|
|
|
cur_params['q'] = helpers.sanitizeSceneName(cur_exception)
|
|
|
|
|
2014-04-27 10:31:54 +00:00
|
|
|
# season
|
2014-05-14 08:01:36 +00:00
|
|
|
if ep_obj.show.air_by_date or ep_obj.show.sports:
|
|
|
|
cur_params['season'] = str(ep_obj.airdate)[:7]
|
|
|
|
else:
|
2014-05-04 03:16:26 +00:00
|
|
|
cur_params['season'] = str(ep_obj.scene_season)
|
2014-04-27 12:13:46 +00:00
|
|
|
|
|
|
|
to_return.append(cur_params)
|
2014-05-04 03:16:26 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return to_return
|
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
def _get_episode_search_strings(self, ep_obj, add_string=''):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
params = {}
|
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
if not ep_obj:
|
2014-03-10 05:18:05 +00:00
|
|
|
return [params]
|
|
|
|
|
|
|
|
# search
|
2014-04-29 13:14:19 +00:00
|
|
|
params['q'] = helpers.sanitizeSceneName(self.show.name)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-04-29 13:14:19 +00:00
|
|
|
if self.show.air_by_date:
|
2014-05-03 09:23:26 +00:00
|
|
|
date_str = str(ep_obj.airdate)
|
2014-04-28 09:15:29 +00:00
|
|
|
params['season'] = date_str.partition('-')[0]
|
|
|
|
params['ep'] = date_str.partition('-')[2].replace('-', '/')
|
2014-04-30 13:49:50 +00:00
|
|
|
elif self.show.sports:
|
2014-05-03 09:23:26 +00:00
|
|
|
date_str = str(ep_obj.airdate)
|
2014-04-30 13:49:50 +00:00
|
|
|
params['season'] = date_str.partition('-')[0]
|
2014-05-03 09:23:26 +00:00
|
|
|
params['ep'] = date_str.partition('-')[2].replace('-', '/')
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
2014-04-30 13:49:50 +00:00
|
|
|
params['season'] = ep_obj.scene_season
|
|
|
|
params['ep'] = ep_obj.scene_episode
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
to_return = [params]
|
|
|
|
|
|
|
|
# only do exceptions if we are searching by name
|
|
|
|
if 'q' in params:
|
|
|
|
|
|
|
|
# add new query strings for exceptions
|
2014-04-29 13:14:19 +00:00
|
|
|
name_exceptions = scene_exceptions.get_scene_exceptions(self.show.indexerid)
|
2014-03-10 05:18:05 +00:00
|
|
|
for cur_exception in name_exceptions:
|
|
|
|
|
|
|
|
# don't add duplicates
|
2014-04-29 13:14:19 +00:00
|
|
|
if cur_exception == self.show.name:
|
2014-03-10 05:18:05 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
cur_return = params.copy()
|
|
|
|
cur_return['q'] = helpers.sanitizeSceneName(cur_exception)
|
|
|
|
to_return.append(cur_return)
|
|
|
|
|
|
|
|
return to_return
|
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
def _doGeneralSearch(self, search_string):
|
|
|
|
return self._doSearch({'q': search_string})
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def _checkAuth(self):
|
|
|
|
|
|
|
|
if self.needs_auth and not self.key:
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.log(u"Incorrect authentication credentials for " + self.name + " : " + "API key is missing",
|
|
|
|
logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
raise AuthException("Your authentication credentials for " + self.name + " are missing, check your config.")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-04-26 01:49:38 +00:00
|
|
|
def _checkAuthFromData(self, data):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-04-26 01:49:38 +00:00
|
|
|
if data is None:
|
2014-03-10 05:18:05 +00:00
|
|
|
return self._checkAuth()
|
|
|
|
|
2014-04-28 10:57:30 +00:00
|
|
|
if 'error' in data.feed:
|
|
|
|
logger.log(u"Newznab ERROR:[%s] CODE:[%s]" % (data.feed['error']['description'], data.feed['error']['code']), logger.DEBUG)
|
|
|
|
raise AuthException("%s" % data.feed['error']['description'])
|
2014-04-28 00:08:12 +00:00
|
|
|
|
2014-04-26 11:23:00 +00:00
|
|
|
return True
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-05-08 14:03:50 +00:00
|
|
|
def _doSearch(self, search_params, epcount=0, age=0):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
self._checkAuth()
|
|
|
|
|
|
|
|
params = {"t": "tvsearch",
|
|
|
|
"maxage": sickbeard.USENET_RETENTION,
|
|
|
|
"limit": 100,
|
|
|
|
"cat": self.catIDs}
|
|
|
|
|
|
|
|
# if max_age is set, use it, don't allow it to be missing
|
2014-05-08 14:03:50 +00:00
|
|
|
if age or not params['maxage']:
|
|
|
|
params['maxage'] = age
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if search_params:
|
|
|
|
params.update(search_params)
|
|
|
|
|
|
|
|
if self.needs_auth and self.key:
|
|
|
|
params['apikey'] = self.key
|
|
|
|
|
|
|
|
search_url = self.url + 'api?' + urllib.urlencode(params)
|
|
|
|
|
|
|
|
logger.log(u"Search url: " + search_url, logger.DEBUG)
|
|
|
|
|
2014-05-04 12:08:45 +00:00
|
|
|
data = self.cache.getRSSFeed(search_url)
|
2014-03-10 05:18:05 +00:00
|
|
|
if not data:
|
|
|
|
return []
|
|
|
|
|
2014-04-26 01:39:43 +00:00
|
|
|
if self._checkAuthFromData(data):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-04-26 01:39:43 +00:00
|
|
|
items = data.entries
|
2014-03-10 05:18:05 +00:00
|
|
|
results = []
|
|
|
|
|
|
|
|
for curItem in items:
|
|
|
|
(title, url) = self._get_title_and_url(curItem)
|
|
|
|
|
|
|
|
if title and url:
|
|
|
|
results.append(curItem)
|
|
|
|
else:
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.log(
|
2014-05-16 05:16:35 +00:00
|
|
|
u"The data returned from the " + self.name + " is incomplete, this result is unusable",
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.DEBUG)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
def findPropers(self, search_date=None):
|
|
|
|
|
|
|
|
search_terms = ['.proper.', '.repack.']
|
|
|
|
|
|
|
|
cache_results = self.cache.listPropers(search_date)
|
2014-03-25 05:57:24 +00:00
|
|
|
results = [classes.Proper(x['name'], x['url'], datetime.datetime.fromtimestamp(x['time'])) for x in
|
|
|
|
cache_results]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-05-17 17:59:33 +00:00
|
|
|
index = 0
|
|
|
|
alt_search = ('nzbs_org' == self.getID())
|
|
|
|
term_items_found = False
|
|
|
|
do_search_alt = False
|
|
|
|
|
|
|
|
while index < len(search_terms):
|
|
|
|
search_params = {'q': search_terms[index]}
|
|
|
|
if alt_search:
|
|
|
|
|
|
|
|
if do_search_alt:
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
if term_items_found:
|
|
|
|
do_search_alt = True
|
|
|
|
term_items_found = False
|
|
|
|
else:
|
|
|
|
if do_search_alt:
|
|
|
|
search_params["t"] = "search"
|
|
|
|
|
|
|
|
do_search_alt = (True, False)[do_search_alt]
|
|
|
|
|
|
|
|
else:
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
for item in self._doSearch(search_params, age=4):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
(title, url) = self._get_title_and_url(item)
|
|
|
|
|
2014-05-01 05:17:54 +00:00
|
|
|
if item.has_key('published_parsed') and item['published_parsed']:
|
2014-04-26 03:42:35 +00:00
|
|
|
result_date = item.published_parsed
|
2014-03-10 05:18:05 +00:00
|
|
|
if result_date:
|
|
|
|
result_date = datetime.datetime(*result_date[0:6])
|
2014-05-01 05:17:54 +00:00
|
|
|
else:
|
|
|
|
logger.log(u"Unable to figure out the date for entry " + title + ", skipping it")
|
|
|
|
continue
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
if not search_date or result_date > search_date:
|
|
|
|
search_result = classes.Proper(title, url, result_date)
|
|
|
|
results.append(search_result)
|
2014-05-17 17:59:33 +00:00
|
|
|
term_items_found = True
|
|
|
|
do_search_alt = False
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
class NewznabCache(tvcache.TVCache):
|
|
|
|
def __init__(self, provider):
|
|
|
|
|
|
|
|
tvcache.TVCache.__init__(self, provider)
|
|
|
|
|
|
|
|
# only poll newznab providers every 15 minutes max
|
|
|
|
self.minTime = 15
|
|
|
|
|
|
|
|
def _getRSSData(self):
|
|
|
|
|
|
|
|
params = {"t": "tvsearch",
|
|
|
|
"cat": self.provider.catIDs}
|
|
|
|
|
|
|
|
if self.provider.needs_auth and self.provider.key:
|
|
|
|
params['apikey'] = self.provider.key
|
|
|
|
|
|
|
|
rss_url = self.provider.url + 'api?' + urllib.urlencode(params)
|
|
|
|
|
|
|
|
logger.log(self.provider.name + " cache update URL: " + rss_url, logger.DEBUG)
|
|
|
|
|
2014-05-04 12:05:27 +00:00
|
|
|
return self.getRSSFeed(rss_url)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-04-26 01:49:38 +00:00
|
|
|
def _checkAuth(self, data):
|
|
|
|
return self.provider._checkAuthFromData(data)
|
2014-05-11 12:49:07 +00:00
|
|
|
|
|
|
|
def updateCache(self):
|
2014-05-18 15:33:31 +00:00
|
|
|
|
|
|
|
# delete anything older then 7 days
|
|
|
|
logger.log(u"Clearing " + self.provider.name + " cache")
|
|
|
|
self._clearCache()
|
|
|
|
|
2014-05-11 12:49:07 +00:00
|
|
|
if not self.shouldUpdate():
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._checkAuth(None):
|
|
|
|
data = self._getRSSData()
|
|
|
|
|
|
|
|
# as long as the http request worked we count this as an update
|
|
|
|
if data:
|
|
|
|
self.setLastUpdate()
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
if self._checkAuth(data):
|
|
|
|
items = data.entries
|
|
|
|
ql = []
|
|
|
|
for item in items:
|
|
|
|
ci = self._parseItem(item)
|
|
|
|
if ci is not None:
|
|
|
|
ql.append(ci)
|
|
|
|
|
2014-05-17 11:40:26 +00:00
|
|
|
time.sleep(cpu_presets[sickbeard.CPU_PRESET])
|
2014-05-17 09:27:17 +00:00
|
|
|
|
2014-05-11 12:49:07 +00:00
|
|
|
myDB = self._getDB()
|
|
|
|
myDB.mass_action(ql)
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise AuthException(
|
|
|
|
u"Your authentication credentials for " + self.provider.name + " are incorrect, check your config")
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
# overwrite method with that parses the rageid from the newznab feed
|
|
|
|
def _parseItem(self, item):
|
|
|
|
title = item.title
|
|
|
|
url = item.link
|
|
|
|
|
|
|
|
self._checkItemAuth(title, url)
|
|
|
|
|
|
|
|
if not title or not url:
|
|
|
|
logger.log(
|
|
|
|
u"The data returned from the " + self.provider.name + " feed is incomplete, this result is unusable",
|
|
|
|
logger.DEBUG)
|
|
|
|
return None
|
|
|
|
|
|
|
|
url = self._translateLinkURL(url)
|
|
|
|
|
2014-05-12 08:52:14 +00:00
|
|
|
logger.log(u"Attempting to add item from RSS to cache: " + title, logger.DEBUG)
|
2014-05-11 12:49:07 +00:00
|
|
|
|
|
|
|
return self._addCacheEntry(title, url)
|