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 urllib
|
2015-04-18 04:55:04 +00:00
|
|
|
|
2015-06-12 10:18:04 +00:00
|
|
|
from . import generic
|
2015-09-18 00:06:34 +00:00
|
|
|
from sickbeard import logger, tvcache
|
2015-04-18 04:55:04 +00:00
|
|
|
from sickbeard.exceptions import AuthException
|
2015-09-18 00:06:34 +00:00
|
|
|
from sickbeard.helpers import tryInt
|
|
|
|
from sickbeard.indexers import indexer_config
|
2015-04-18 04:55:04 +00:00
|
|
|
|
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-09-18 00:06:34 +00:00
|
|
|
self.categories = [3, 5, 2]
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
self.proper_search_terms = [' proper ', ' repack ']
|
2015-06-12 10:18:04 +00:00
|
|
|
self.url = self.urls['config_provider_home_uri']
|
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
self.username, self.passkey, self.minseed, self.minleech = 4 * [None]
|
|
|
|
self.freeleech = False
|
2015-06-12 10:18:04 +00:00
|
|
|
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-09-18 00:06:34 +00:00
|
|
|
def _season_strings(self, ep_obj, **kwargs):
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-12-02 15:19:28 +00:00
|
|
|
params = super(HDBitsProvider, self)._season_strings(ep_obj, scene=False)
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
show = ep_obj.show
|
|
|
|
if indexer_config.INDEXER_TVDB == show.indexer and show.indexerid:
|
|
|
|
params[0]['Season'].insert(0, dict(tvdb=dict(
|
|
|
|
id=show.indexerid,
|
|
|
|
season=(show.air_by_date or show.is_sports) and str(ep_obj.airdate)[:7] or
|
|
|
|
(show.is_anime and ('%d' % ep_obj.scene_absolute_number) or
|
|
|
|
(ep_obj.season, ep_obj.scene_season)[bool(show.is_scene)]))))
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
return params
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def _episode_strings(self, ep_obj, **kwargs):
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-12-02 15:19:28 +00:00
|
|
|
params = super(HDBitsProvider, self)._episode_strings(ep_obj, scene=False, sep_date='|')
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
show = ep_obj.show
|
|
|
|
if indexer_config.INDEXER_TVDB == show.indexer and show.indexerid:
|
|
|
|
id_param = dict(
|
|
|
|
id=show.indexerid,
|
|
|
|
episode=show.air_by_date and str(ep_obj.airdate).replace('-', ' ') or
|
|
|
|
(show.is_sports and ep_obj.airdate.strftime('%b') or
|
|
|
|
(show.is_anime and ('%i' % int(ep_obj.scene_absolute_number)) or
|
|
|
|
(ep_obj.episode, ep_obj.scene_episode)[bool(show.is_scene)])))
|
|
|
|
if not(show.air_by_date and show.is_sports and show.is_anime):
|
|
|
|
id_param['season'] = (ep_obj.season, ep_obj.scene_season)[bool(show.is_scene)]
|
|
|
|
params[0]['Episode'].insert(0, dict(tvdb=id_param))
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
return params
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def _search_provider(self, search_params, **kwargs):
|
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-09-18 00:06:34 +00:00
|
|
|
results = []
|
|
|
|
api_data = {'username': self.username, 'passkey': self.passkey, 'category': self.categories}
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
items = {'Cache': [], 'Season': [], 'Episode': [], 'Propers': []}
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
for mode in search_params.keys():
|
2015-12-02 15:19:28 +00:00
|
|
|
for search_param in search_params[mode]:
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
post_data = api_data.copy()
|
2015-12-02 15:19:28 +00:00
|
|
|
if isinstance(search_param, dict):
|
|
|
|
post_data.update(search_param)
|
2015-09-18 00:06:34 +00:00
|
|
|
id_search = True
|
|
|
|
else:
|
2015-12-02 15:19:28 +00:00
|
|
|
post_data['search'] = search_param
|
2015-09-18 00:06:34 +00:00
|
|
|
id_search = False
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
post_data = json.dumps(post_data)
|
|
|
|
search_url = self.urls['search']
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
json_resp = self.get_url(search_url, post_data=post_data, json=True)
|
2015-12-02 15:19:28 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
if not (json_resp and 'data' in json_resp and self.check_auth_from_data(json_resp)):
|
|
|
|
logger.log(u'Response from %s does not contain any json data, abort' % self.name, logger.ERROR)
|
|
|
|
return results
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
cnt = len(items[mode])
|
|
|
|
for item in json_resp['data']:
|
2014-05-17 13:55:58 +00:00
|
|
|
try:
|
2015-12-02 15:19:28 +00:00
|
|
|
seeders, leechers, size = [tryInt(n, n) for n in [item.get(x) for x in 'seeders', 'leechers', 'size']]
|
2015-09-18 00:06:34 +00:00
|
|
|
if self._peers_fail(mode, seeders, leechers)\
|
|
|
|
or self.freeleech and re.search('(?i)no', item.get('freeleech', 'no')):
|
2015-06-12 10:18:04 +00:00
|
|
|
continue
|
2015-09-18 00:06:34 +00:00
|
|
|
title = item['name']
|
|
|
|
download_url = self.urls['get'] % urllib.urlencode({'id': item['id'], 'passkey': self.passkey})
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
except (AttributeError, TypeError, ValueError):
|
|
|
|
continue
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
if title and download_url:
|
|
|
|
items[mode].append((title, download_url, item.get('seeders', 0), self._bytesizer(size)))
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
self._log_search(mode, len(items[mode]) - cnt,
|
2015-12-02 15:19:28 +00:00
|
|
|
('search_param: ' + str(search_param), self.name)['Cache' == mode])
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
self._sort_seeders(mode, items)
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
if id_search and len(items[mode]):
|
|
|
|
return items[mode]
|
2014-05-14 08:01:36 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
results = list(set(results + items[mode]))
|
2015-06-12 10:18:04 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
return results
|
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-09-18 00:06:34 +00:00
|
|
|
self.update_freq = 15 # cache update frequency
|
2014-05-17 13:55:58 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def _cache_data(self):
|
2014-08-12 10:09:11 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
return self.provider.cache_data()
|
2014-05-17 13:55:58 +00:00
|
|
|
|
|
|
|
|
2015-04-15 06:16:27 +00:00
|
|
|
provider = HDBitsProvider()
|