2014-03-10 01:18:05 -04:00
|
|
|
# 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
|
|
|
|
import generic
|
|
|
|
import sickbeard
|
|
|
|
|
|
|
|
from sickbeard import logger, tvcache, exceptions
|
2014-04-27 06:31:54 -04:00
|
|
|
from sickbeard.common import Quality, Overview
|
2014-03-10 01:18:05 -04:00
|
|
|
from sickbeard.exceptions import ex, AuthException
|
|
|
|
from sickbeard.name_parser.parser import NameParser, InvalidNameException
|
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
from lib import simplejson as json
|
|
|
|
|
|
|
|
|
|
|
|
class HDBitsProvider(generic.TorrentProvider):
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
generic.TorrentProvider.__init__(self, "HDBits")
|
|
|
|
|
|
|
|
self.supportsBacklog = False
|
|
|
|
|
|
|
|
self.cache = HDBitsCache(self)
|
|
|
|
|
|
|
|
self.url = 'https://hdbits.org'
|
|
|
|
self.search_url = 'https://hdbits.org/api/torrents'
|
|
|
|
self.rss_url = 'https://hdbits.org/api/torrents'
|
|
|
|
self.download_url = 'http://hdbits.org/download.php?'
|
|
|
|
|
|
|
|
def isEnabled(self):
|
|
|
|
return sickbeard.HDBITS
|
|
|
|
|
|
|
|
def _checkAuth(self):
|
|
|
|
|
2014-03-25 01:57:24 -04:00
|
|
|
if not sickbeard.HDBITS_USERNAME or not sickbeard.HDBITS_PASSKEY:
|
2014-03-10 01:18:05 -04:00
|
|
|
raise AuthException("Your authentication credentials for " + self.name + " are missing, check your config.")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-04-25 23:58:24 -04:00
|
|
|
def _checkAuthFromData(self, parsedJSON):
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-25 23:58:24 -04:00
|
|
|
if parsedJSON is None:
|
2014-03-10 01:18:05 -04:00
|
|
|
return self._checkAuth()
|
|
|
|
|
2014-04-25 23:58:24 -04:00
|
|
|
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.")
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
def searchProviders(self, show, season, episode=None, manualSearch=False):
|
|
|
|
itemList = []
|
|
|
|
results = {}
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
logger.log(u"Searching for stuff we need from " + show.name + " season " + str(season))
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
# 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:
|
2014-04-29 09:14:19 -04:00
|
|
|
seasonEps = self.show.getAllEpisodes(season)
|
|
|
|
wantedEps = [x for x in seasonEps if self.show.getOverview(x.status) in (Overview.WANTED, Overview.QUAL)]
|
2014-04-27 06:31:54 -04:00
|
|
|
else:
|
|
|
|
wantedEps = [show.getEpisode(season, episode)]
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
for ep_obj in wantedEps:
|
|
|
|
season = ep_obj.scene_season
|
|
|
|
episode = ep_obj.scene_episode
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-05-04 23:04:46 -04:00
|
|
|
if manualSearch:
|
|
|
|
self.cache.updateCache()
|
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
results = self.cache.searchCache(episode, manualSearch)
|
|
|
|
logger.log(u"Cache results: " + str(results), logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
# 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
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
itemList += self.getURL(self.search_url, post_data=self._make_post_data_JSON(show=show, season=season, episode=episode), json=True)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
for parsedJSON in itemList:
|
|
|
|
if not parsedJSON:
|
|
|
|
logger.log(u"No data returned from " + self.search_url, logger.ERROR)
|
|
|
|
return []
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
if self._checkAuthFromData(parsedJSON):
|
|
|
|
results = []
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
if parsedJSON and 'data' in parsedJSON:
|
|
|
|
items = parsedJSON['data']
|
|
|
|
else:
|
|
|
|
logger.log(u"Resulting JSON from " + self.name + " isn't correct, not parsing it", logger.ERROR)
|
|
|
|
items = []
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
for item in items:
|
|
|
|
|
|
|
|
(title, url) = self._get_title_and_url(item)
|
|
|
|
|
|
|
|
# parse the file name
|
|
|
|
try:
|
|
|
|
myParser = NameParser()
|
2014-05-03 05:23:26 -04:00
|
|
|
parse_result = myParser.parse(title)
|
2014-04-27 06:31:54 -04:00
|
|
|
except InvalidNameException:
|
|
|
|
logger.log(u"Unable to parse the filename " + title + " into a valid episode", logger.WARNING)
|
|
|
|
continue
|
|
|
|
|
2014-04-28 05:15:29 -04:00
|
|
|
if episode.show.air_by_date or episode.sports:
|
2014-04-27 06:31:54 -04:00
|
|
|
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)
|
2014-03-10 01:18:05 -04:00
|
|
|
continue
|
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
quality = self.getQuality(item)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
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
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
logger.log(u"Found result " + title + " at " + url, logger.DEBUG)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
result = self.getResult([episode])
|
|
|
|
result.url = url
|
|
|
|
result.name = title
|
|
|
|
result.quality = quality
|
2014-03-10 01:18:05 -04:00
|
|
|
|
2014-04-27 06:31:54 -04:00
|
|
|
results.append(result)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
def _get_title_and_url(self, item):
|
|
|
|
|
2014-04-25 23:58:24 -04:00
|
|
|
title = item['name']
|
|
|
|
url = self.download_url + urllib.urlencode({'id': item['id'], 'passkey': sickbeard.HDBITS_PASSKEY})
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
return (title, url)
|
|
|
|
|
|
|
|
def _make_post_data_JSON(self, show=None, episode=None, season=None):
|
|
|
|
|
|
|
|
post_data = {
|
|
|
|
'username': sickbeard.HDBITS_USERNAME,
|
|
|
|
'passkey': sickbeard.HDBITS_PASSKEY,
|
|
|
|
'category': [2], # TV Category
|
|
|
|
}
|
|
|
|
|
|
|
|
if episode:
|
|
|
|
post_data['tvdb'] = {
|
|
|
|
'id': show.indexerid,
|
2014-04-27 06:31:54 -04:00
|
|
|
'season': season,
|
|
|
|
'episode': episode
|
2014-03-10 01:18:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if season:
|
|
|
|
post_data['tvdb'] = {
|
|
|
|
'id': show.indexerid,
|
|
|
|
'season': season,
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.dumps(post_data)
|
|
|
|
|
|
|
|
|
|
|
|
class HDBitsCache(tvcache.TVCache):
|
|
|
|
def __init__(self, provider):
|
|
|
|
|
|
|
|
tvcache.TVCache.__init__(self, provider)
|
|
|
|
|
|
|
|
# only poll HDBits every 15 minutes max
|
|
|
|
self.minTime = 15
|
|
|
|
|
|
|
|
def updateCache(self):
|
|
|
|
|
|
|
|
if not self.shouldUpdate():
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._checkAuth(None):
|
|
|
|
|
2014-04-25 23:58:24 -04:00
|
|
|
parsedJSON = self._getRSSData()
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
# As long as we got something from the provider we count it as an update
|
2014-04-25 23:58:24 -04:00
|
|
|
if parsedJSON:
|
2014-03-10 01:18:05 -04:00
|
|
|
self.setLastUpdate()
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
logger.log(u"Clearing " + self.provider.name + " cache and updating with new information")
|
|
|
|
self._clearCache()
|
|
|
|
|
2014-04-25 23:58:24 -04:00
|
|
|
if parsedJSON is None:
|
|
|
|
logger.log(u"Error trying to load " + self.provider.name + " JSON feed", logger.ERROR)
|
|
|
|
return []
|
|
|
|
|
|
|
|
if self._checkAuth(parsedJSON):
|
|
|
|
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)
|
2014-03-10 01:18:05 -04:00
|
|
|
return []
|
|
|
|
|
|
|
|
cl = []
|
|
|
|
for item in items:
|
|
|
|
ci = self._parseItem(item)
|
|
|
|
if ci is not None:
|
|
|
|
cl.append(ci)
|
2014-03-25 01:57:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
if len(cl) > 0:
|
|
|
|
myDB = self._getDB()
|
|
|
|
myDB.mass_action(cl)
|
|
|
|
|
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
raise exceptions.AuthException(
|
|
|
|
"Your authentication info for " + self.provider.name + " is incorrect, check your config")
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def _getRSSData(self):
|
2014-04-25 23:58:24 -04:00
|
|
|
return self.provider.getURL(self.provider.rss_url, post_data=self.provider._make_post_data_JSON(), json=True)
|
2014-03-10 01:18:05 -04:00
|
|
|
|
|
|
|
def _parseItem(self, item):
|
|
|
|
|
|
|
|
(title, url) = self.provider._get_title_and_url(item)
|
|
|
|
|
|
|
|
if title and url:
|
|
|
|
logger.log(u"Adding item to results: " + title, logger.DEBUG)
|
|
|
|
return self._addCacheEntry(title, url)
|
|
|
|
else:
|
2014-03-25 01:57:24 -04:00
|
|
|
logger.log(u"The data returned from the " + self.provider.name + " is incomplete, this result is unusable",
|
|
|
|
logger.ERROR)
|
2014-03-10 01:18:05 -04:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _checkAuth(self, data):
|
|
|
|
return self.provider._checkAuthFromData(data)
|
|
|
|
|
2014-04-25 23:58:24 -04:00
|
|
|
|
2014-03-10 01:18:05 -04:00
|
|
|
provider = HDBitsProvider()
|