2015-06-12 10:18:04 +00:00
|
|
|
# coding=utf-8
|
|
|
|
#
|
|
|
|
# This file is part of SickGear.
|
2015-04-15 06:16:27 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2015-04-15 06:16:27 +00: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-11-12 16:43:14 +00:00
|
|
|
# SickGear is distributed in the hope that it will be useful,
|
2015-04-15 06:16:27 +00: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-11-12 16:43:14 +00:00
|
|
|
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
2014-05-07 07:50:49 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
import re
|
2014-05-17 13:55:58 +00:00
|
|
|
import datetime
|
|
|
|
import urllib
|
2015-04-18 04:55:04 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
from . import generic
|
2015-04-18 04:55:04 +00:00
|
|
|
from sickbeard import classes, logger, tvcache
|
|
|
|
from sickbeard.exceptions import AuthException
|
|
|
|
|
2014-05-17 13:55:58 +00:00
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
from lib import simplejson as json
|
|
|
|
|
|
|
|
|
|
|
|
class HDBitsProvider(generic.TorrentProvider):
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2014-05-17 13:55:58 +00:00
|
|
|
def __init__(self):
|
2015-06-12 10:18:04 +00:00
|
|
|
generic.TorrentProvider.__init__(self, 'HDBits')
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
# api_spec: https://hdbits.org/wiki/API
|
|
|
|
self.url_base = 'https://hdbits.org/'
|
|
|
|
self.urls = {'config_provider_home_uri': self.url_base,
|
|
|
|
'search': self.url_base + 'api/torrents',
|
|
|
|
'get': self.url_base + 'download.php?%s'}
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
self.categories = 2 # TV
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
self.url = self.urls['config_provider_home_uri']
|
|
|
|
|
|
|
|
self.username, self.passkey = 2 * [None]
|
|
|
|
self.cache = HDBitsCache(self)
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
def check_auth_from_data(self, parsed_json):
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
if 'status' in parsed_json and 5 == parsed_json.get('status') and 'message' in parsed_json:
|
|
|
|
logger.log(u'Incorrect username or password for %s : %s' % (self.name, parsed_json['message']), logger.DEBUG)
|
|
|
|
raise AuthException('Your username or password for %s is incorrect, check your config.' % self.name)
|
2014-05-17 13:55:58 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
def _get_season_search_strings(self, ep_obj, **kwargs):
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
return [self._build_search_strings(show=ep_obj.show, season=ep_obj)]
|
|
|
|
|
|
|
|
def _get_episode_search_strings(self, ep_obj, add_string='', **kwargs):
|
|
|
|
|
|
|
|
return [self._build_search_strings(show=ep_obj.show, episode=ep_obj)]
|
2014-05-17 13:55:58 +00:00
|
|
|
|
|
|
|
def _get_title_and_url(self, item):
|
|
|
|
|
|
|
|
title = item['name']
|
|
|
|
if title:
|
2015-06-12 10:18:04 +00:00
|
|
|
title = u'' + title.replace(' ', '.')
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
url = self.urls['get'] % urllib.urlencode({'id': item['id'], 'passkey': self.passkey})
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
return title, url
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def _do_search(self, search_params, search_mode='eponly', epcount=0, age=0):
|
2014-05-05 07:39:20 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
self._check_auth()
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
logger.log(u'Search url: %s search_params: %s' % (self.urls['search'], search_params), logger.DEBUG)
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
response_json = self.get_url(self.urls['search'], post_data=search_params, json=True)
|
2015-06-12 10:18:04 +00:00
|
|
|
if response_json and 'data' in response_json and self.check_auth_from_data(response_json):
|
|
|
|
return response_json['data']
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
logger.log(u'Resulting JSON from %s isn\'t correct, not parsing it' % self.name, logger.ERROR)
|
|
|
|
return []
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
def find_propers(self, search_date=None):
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2014-05-17 13:55:58 +00:00
|
|
|
results = []
|
|
|
|
|
|
|
|
search_terms = [' proper ', ' repack ']
|
|
|
|
|
|
|
|
for term in search_terms:
|
2015-07-13 09:39:20 +00:00
|
|
|
for item in self._do_search(self._build_search_strings(search_term=term)):
|
2014-05-17 13:55:58 +00:00
|
|
|
if item['utadded']:
|
|
|
|
try:
|
|
|
|
result_date = datetime.datetime.fromtimestamp(int(item['utadded']))
|
|
|
|
except:
|
|
|
|
result_date = None
|
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
if result_date and (not search_date or result_date > search_date):
|
|
|
|
title, url = self._get_title_and_url(item)
|
|
|
|
if not re.search('(?i)(?:%s)' % term.strip(), title):
|
|
|
|
continue
|
|
|
|
results.append(classes.Proper(title, url, result_date, self.show))
|
2014-05-17 13:55:58 +00:00
|
|
|
return results
|
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
def _build_search_strings(self, show=None, episode=None, season=None, search_term=None):
|
|
|
|
|
|
|
|
request_params = {'username': self.username, 'passkey': self.passkey, 'category': [self.categories]}
|
|
|
|
|
|
|
|
if episode or season:
|
|
|
|
param = {'id': show.indexerid}
|
|
|
|
|
|
|
|
if episode:
|
|
|
|
if show.air_by_date:
|
|
|
|
param['episode'] = str(episode.airdate).replace('-', '|')
|
2015-08-14 23:02:05 +00:00
|
|
|
elif show.is_sports:
|
2015-06-12 10:18:04 +00:00
|
|
|
param['episode'] = episode.airdate.strftime('%b')
|
2015-08-14 23:02:05 +00:00
|
|
|
elif show.is_anime:
|
2015-06-12 10:18:04 +00:00
|
|
|
param['episode'] = '%i' % int(episode.scene_absolute_number)
|
|
|
|
else:
|
|
|
|
param['season'] = episode.scene_season
|
|
|
|
param['episode'] = episode.scene_episode
|
|
|
|
|
|
|
|
if season:
|
2015-08-14 23:02:05 +00:00
|
|
|
if show.air_by_date or show.is_sports:
|
2015-06-12 10:18:04 +00:00
|
|
|
param['season'] = str(season.airdate)[:7]
|
2015-08-14 23:02:05 +00:00
|
|
|
elif show.is_anime:
|
2015-06-12 10:18:04 +00:00
|
|
|
param['season'] = '%d' % season.scene_absolute_number
|
|
|
|
else:
|
|
|
|
param['season'] = season.scene_season
|
|
|
|
|
|
|
|
request_params['tvdb'] = param
|
2014-05-14 08:01:36 +00:00
|
|
|
|
2014-05-17 13:55:58 +00:00
|
|
|
if search_term:
|
2015-06-12 10:18:04 +00:00
|
|
|
request_params['search'] = search_term
|
|
|
|
|
|
|
|
return json.dumps(request_params)
|
|
|
|
|
|
|
|
def get_cache_data(self):
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
self._check_auth()
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
response_json = self.get_url(self.urls['search'], post_data=self._build_search_strings(), json=True)
|
2015-06-12 10:18:04 +00:00
|
|
|
if response_json and 'data' in response_json and self.check_auth_from_data(response_json):
|
|
|
|
return response_json['data']
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
return []
|
2014-05-17 13:55:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HDBitsCache(tvcache.TVCache):
|
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
def __init__(self, this_provider):
|
|
|
|
tvcache.TVCache.__init__(self, this_provider)
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
self.minTime = 15 # cache update frequency
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2014-08-30 08:47:00 +00:00
|
|
|
def _getRSSData(self):
|
2014-08-12 10:09:11 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
return self.provider.get_cache_data()
|
2014-05-17 13:55:58 +00:00
|
|
|
|
|
|
|
|
2015-04-15 06:16:27 +00:00
|
|
|
provider = HDBitsProvider()
|