Added backlog and proper searchers to HDBits

This commit is contained in:
echel0n 2014-05-05 00:39:20 -07:00
parent afdbc44a47
commit c11c61e8cf

View file

@ -13,12 +13,15 @@
# 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 datetime
import urllib
import generic
import sickbeard
from sickbeard import classes
from sickbeard import logger, tvcache, exceptions
from sickbeard.common import Quality, Overview
from sickbeard import helpers
from sickbeard.common import Quality
from sickbeard.exceptions import ex, AuthException
from sickbeard.name_parser.parser import NameParser, InvalidNameException
@ -29,11 +32,12 @@ except ImportError:
class HDBitsProvider(generic.TorrentProvider):
def __init__(self):
generic.TorrentProvider.__init__(self, "HDBits")
self.supportsBacklog = False
self.supportsBacklog = True
self.cache = HDBitsCache(self)
@ -59,53 +63,49 @@ class HDBitsProvider(generic.TorrentProvider):
if 'status' in parsedJSON and 'message' in parsedJSON:
if parsedJSON.get('status') == 5:
logger.log(u"Incorrect authentication credentials for " + self.name + " : " + parsedJSON['message'],
logger.DEBUG)
raise AuthException(
"Your authentication credentials for " + self.name + " are incorrect, check your config.")
logger.log(u"Incorrect authentication credentials for " + self.name + " : " + parsedJSON['message'], logger.DEBUG)
raise AuthException("Your authentication credentials for " + self.name + " are incorrect, check your config.")
return True
def searchProviders(self, show, season, episode=None, manualSearch=False):
itemList = []
results = {}
def _get_season_search_strings(self, ep_obj):
season_search_string = [self._make_post_data_JSON(show=ep_obj.show, season=ep_obj.scene_season)]
return season_search_string
logger.log(u"Searching for stuff we need from " + show.name + " season " + str(season))
def _get_episode_search_strings(self, ep_obj, add_string=''):
episode_search_string = [self._make_post_data_JSON(show=ep_obj.show, episode=ep_obj)]
return episode_search_string
# gather all episodes for season and then pick out the wanted episodes and compare to determin if we want whole season or just a few episodes
if episode is None:
seasonEps = self.show.getAllEpisodes(season)
wantedEps = [x for x in seasonEps if self.show.getOverview(x.status) in (Overview.WANTED, Overview.QUAL)]
else:
wantedEps = [show.getEpisode(season, episode)]
def _get_title_and_url(self, item):
for ep_obj in wantedEps:
season = ep_obj.scene_season
episode = ep_obj.scene_episode
title = item['name']
if title:
title = title.replace(' ', '.')
if manualSearch:
self.cache.updateCache()
url = self.download_url + urllib.urlencode({'id': item['id'], 'passkey': sickbeard.HDBITS_PASSKEY})
results = self.cache.searchCache(episode, manualSearch)
logger.log(u"Cache results: " + str(results), logger.DEBUG)
return (title, url)
# if we got some results then use them no matter what.
# OR
# return anyway unless we're doing a manual search
if results or not manualSearch:
return results
def _doSearch(self, search_params, show=None, age=None):
results = []
itemList += self.getURL(self.search_url, post_data=self._make_post_data_JSON(show=show, season=season, episode=episode), json=True)
self._checkAuth()
logger.log(u"Search url: " + self.search_url + " search_params: " + search_params, logger.DEBUG)
for parsedJSON in itemList:
if not parsedJSON:
data = self.getURL(self.search_url, post_data=search_params)
if not data:
logger.log(u"No data returned from " + self.search_url, logger.ERROR)
return []
if self._checkAuthFromData(parsedJSON):
results = []
parsedJSON = helpers.parse_json(data)
if parsedJSON is None:
logger.log(u"Error trying to load " + self.name + " JSON data", logger.ERROR)
return []
if self._checkAuthFromData(parsedJSON):
if parsedJSON and 'data' in parsedJSON:
items = parsedJSON['data']
else:
@ -113,53 +113,31 @@ class HDBitsProvider(generic.TorrentProvider):
items = []
for item in items:
(title, url) = self._get_title_and_url(item)
# parse the file name
try:
myParser = NameParser()
parse_result = myParser.parse(title)
except InvalidNameException:
logger.log(u"Unable to parse the filename " + title + " into a valid episode", logger.WARNING)
continue
if episode.show.air_by_date or episode.sports:
if parse_result.air_date != episode.airdate:
logger.log(u"Episode " + title + " didn't air on " + str(episode.airdate) + ", skipping it",
logger.DEBUG)
continue
elif parse_result.season_number != episode.season or episode.episode not in parse_result.episode_numbers:
logger.log(u"Episode " + title + " isn't " + str(episode.season) + "x" + str(
episode.episode) + ", skipping it", logger.DEBUG)
continue
quality = self.getQuality(item)
if not episode.show.wantEpisode(episode.season, episode.episode, quality, manualSearch):
logger.log(u"Ignoring result " + title + " because we don't want an episode that is " +
Quality.qualityStrings[quality], logger.DEBUG)
continue
logger.log(u"Found result " + title + " at " + url, logger.DEBUG)
result = self.getResult([episode])
result.url = url
result.name = title
result.quality = quality
results.append(result)
results.append(item)
return results
def _get_title_and_url(self, item):
def findPropers(self, search_date=None):
results = []
title = item['name']
url = self.download_url + urllib.urlencode({'id': item['id'], 'passkey': sickbeard.HDBITS_PASSKEY})
search_terms = [' proper ', ' repack ']
return (title, url)
for term in search_terms:
for item in self._doSearch(self._make_post_data_JSON(search_term=term)):
if item['utadded']:
try:
result_date = datetime.datetime.fromtimestamp(int(item['utadded']))
except:
result_date = None
def _make_post_data_JSON(self, show=None, episode=None, season=None):
if result_date:
if not search_date or result_date > search_date:
title, url = self._get_title_and_url(item)
results.append(classes.Proper(title, url, result_date))
return results
def _make_post_data_JSON(self, show=None, episode=None, season=None, search_term=None):
post_data = {
'username': sickbeard.HDBITS_USERNAME,
@ -169,9 +147,9 @@ class HDBitsProvider(generic.TorrentProvider):
if episode:
post_data['tvdb'] = {
'id': show.indexerid,
'season': season,
'episode': episode
'id': show.tvdbid,
'season': episode.scene_season,
'episode': episode.scene_episode
}
if season:
@ -180,10 +158,14 @@ class HDBitsProvider(generic.TorrentProvider):
'season': season,
}
if search_term:
post_data['search'] = search_term
return json.dumps(post_data)
class HDBitsCache(tvcache.TVCache):
def __init__(self, provider):
tvcache.TVCache.__init__(self, provider)
@ -198,10 +180,10 @@ class HDBitsCache(tvcache.TVCache):
if self._checkAuth(None):
parsedJSON = self._getRSSData()
data = self._getRSSData()
# As long as we got something from the provider we count it as an update
if parsedJSON:
if data:
self.setLastUpdate()
else:
return []
@ -209,6 +191,8 @@ class HDBitsCache(tvcache.TVCache):
logger.log(u"Clearing " + self.provider.name + " cache and updating with new information")
self._clearCache()
parsedJSON = helpers.parse_json(data)
if parsedJSON is None:
logger.log(u"Error trying to load " + self.provider.name + " JSON feed", logger.ERROR)
return []
@ -217,29 +201,26 @@ class HDBitsCache(tvcache.TVCache):
if parsedJSON and 'data' in parsedJSON:
items = parsedJSON['data']
else:
logger.log(u"Resulting JSON from " + self.provider.name + " isn't correct, not parsing it",
logger.ERROR)
logger.log(u"Resulting JSON from " + self.provider.name + " isn't correct, not parsing it", logger.ERROR)
return []
cl = []
ql = []
for item in items:
ci = self._parseItem(item)
if ci is not None:
cl.append(ci)
ql.append(ci)
if len(cl) > 0:
myDB = self._getDB()
myDB.mass_action(cl)
myDB.mass_action(ql)
else:
raise exceptions.AuthException(
"Your authentication info for " + self.provider.name + " is incorrect, check your config")
raise exceptions.AuthException("Your authentication info for " + self.provider.name + " is incorrect, check your config")
else:
return []
def _getRSSData(self):
return self.provider.getURL(self.provider.rss_url, post_data=self.provider._make_post_data_JSON(), json=True)
return self.provider.getURL(self.provider.rss_url, post_data=self.provider._make_post_data_JSON())
def _parseItem(self, item):
@ -249,12 +230,10 @@ class HDBitsCache(tvcache.TVCache):
logger.log(u"Adding item to results: " + title, logger.DEBUG)
return self._addCacheEntry(title, url)
else:
logger.log(u"The data returned from the " + self.provider.name + " is incomplete, this result is unusable",
logger.ERROR)
logger.log(u"The data returned from the " + self.provider.name + " is incomplete, this result is unusable", logger.ERROR)
return None
def _checkAuth(self, data):
return self.provider._checkAuthFromData(data)
provider = HDBitsProvider()