Change provider HDB code simplify and PEP8.

This commit is contained in:
JackDandy 2015-06-12 11:18:04 +01:00
parent 50978cfcd3
commit a771dcdf0d
2 changed files with 83 additions and 116 deletions

View file

@ -18,6 +18,7 @@
* Change provider SCC remove 1 of 3 requests per search to save 30% time * Change provider SCC remove 1 of 3 requests per search to save 30% time
* Change provider SCC login process to use General Config/Advanced/Proxy host setting * Change provider SCC login process to use General Config/Advanced/Proxy host setting
* Change provider SCD PEP8 and code convention cleanup * Change provider SCD PEP8 and code convention cleanup
* Change provider HDB code simplify and PEP8
* Change provider IPT only decode unicode search strings * Change provider IPT only decode unicode search strings
* Change provider IPT login process to use General Config/Advanced/Proxy host setting * Change provider IPT login process to use General Config/Advanced/Proxy host setting
* Change provider TB PEP8 and code convention cleanup * Change provider TB PEP8 and code convention cleanup

View file

@ -1,3 +1,5 @@
# coding=utf-8
#
# This file is part of SickGear. # This file is part of SickGear.
# #
# SickGear is free software: you can redistribute it and/or modify # SickGear is free software: you can redistribute it and/or modify
@ -13,14 +15,14 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with SickGear. If not, see <http://www.gnu.org/licenses/>. # along with SickGear. If not, see <http://www.gnu.org/licenses/>.
import re
import datetime import datetime
import urllib import urllib
import generic from . import generic
from sickbeard import classes, logger, tvcache from sickbeard import classes, logger, tvcache
from sickbeard.exceptions import AuthException from sickbeard.exceptions import AuthException
try: try:
import json import json
except ImportError: except ImportError:
@ -28,173 +30,137 @@ except ImportError:
class HDBitsProvider(generic.TorrentProvider): class HDBitsProvider(generic.TorrentProvider):
def __init__(self): def __init__(self):
generic.TorrentProvider.__init__(self, 'HDBits', True, False) generic.TorrentProvider.__init__(self, 'HDBits')
self.username = None
self.passkey = None # api_spec: https://hdbits.org/wiki/API
self.ratio = None 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'}
self.categories = 2 # TV
self.url = self.urls['config_provider_home_uri']
self.username, self.passkey = 2 * [None]
self.cache = HDBitsCache(self) 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 = 'https://hdbits.org/download.php?'
def _checkAuth(self): def check_auth_from_data(self, parsed_json):
if not self.username or not self.passkey: if 'status' in parsed_json and 5 == parsed_json.get('status') and 'message' in parsed_json:
raise AuthException("Your authentication credentials for " + self.name + " are missing, check your config.") 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)
return True return True
def _checkAuthFromData(self, parsedJSON): def _get_season_search_strings(self, ep_obj, **kwargs):
if 'status' in parsedJSON and 'message' in parsedJSON: return [self._build_search_strings(show=ep_obj.show, season=ep_obj)]
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.")
return True def _get_episode_search_strings(self, ep_obj, add_string='', **kwargs):
def _get_season_search_strings(self, ep_obj): return [self._build_search_strings(show=ep_obj.show, episode=ep_obj)]
season_search_string = [self._make_post_data_JSON(show=ep_obj.show, season=ep_obj)]
return season_search_string
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
def _get_title_and_url(self, item): def _get_title_and_url(self, item):
title = item['name'] title = item['name']
if title: if title:
title = u'' + title title = u'' + title.replace(' ', '.')
title = title.replace(' ', '.')
url = self.download_url + urllib.urlencode({'id': item['id'], 'passkey': self.passkey}) url = self.urls['get'] % urllib.urlencode({'id': item['id'], 'passkey': self.passkey})
return (title, url) return title, url
def _doSearch(self, search_params, search_mode='eponly', epcount=0, age=0): def _doSearch(self, search_params, search_mode='eponly', epcount=0, age=0):
results = []
self._checkAuth() self._checkAuth()
logger.log(u"Search url: " + self.search_url + " search_params: " + search_params, logger.DEBUG) logger.log(u'Search url: %s search_params: %s' % (self.urls['search'], search_params), logger.DEBUG)
parsedJSON = self.getURL(self.search_url, post_data=search_params, json=True) response_json = self.getURL(self.urls['search'], post_data=search_params, json=True)
if not parsedJSON: if response_json and 'data' in response_json and self.check_auth_from_data(response_json):
return response_json['data']
logger.log(u'Resulting JSON from %s isn\'t correct, not parsing it' % self.name, logger.ERROR)
return [] return []
if self._checkAuthFromData(parsedJSON):
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 = []
for item in items:
results.append(item)
return results
def findPropers(self, search_date=None): def findPropers(self, search_date=None):
results = [] results = []
search_terms = [' proper ', ' repack '] search_terms = [' proper ', ' repack ']
for term in search_terms: for term in search_terms:
for item in self._doSearch(self._make_post_data_JSON(search_term=term)): for item in self._doSearch(self._build_search_strings(search_term=term)):
if item['utadded']: if item['utadded']:
try: try:
result_date = datetime.datetime.fromtimestamp(int(item['utadded'])) result_date = datetime.datetime.fromtimestamp(int(item['utadded']))
except: except:
result_date = None result_date = None
if result_date: if result_date and (not search_date or result_date > search_date):
if not search_date or result_date > search_date:
title, url = self._get_title_and_url(item) 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)) results.append(classes.Proper(title, url, result_date, self.show))
return results return results
def _make_post_data_JSON(self, show=None, episode=None, season=None, search_term=None): def _build_search_strings(self, show=None, episode=None, season=None, search_term=None):
post_data = { request_params = {'username': self.username, 'passkey': self.passkey, 'category': [self.categories]}
'username': self.username,
'passkey': self.passkey, if episode or season:
'category': [2], param = {'id': show.indexerid}
# TV Category
}
if episode: if episode:
if show.air_by_date: if show.air_by_date:
post_data['tvdb'] = { param['episode'] = str(episode.airdate).replace('-', '|')
'id': show.indexerid,
'episode': str(episode.airdate).replace('-', '|')
}
elif show.sports: elif show.sports:
post_data['tvdb'] = { param['episode'] = episode.airdate.strftime('%b')
'id': show.indexerid,
'episode': episode.airdate.strftime('%b')
}
elif show.anime: elif show.anime:
post_data['tvdb'] = { param['episode'] = '%i' % int(episode.scene_absolute_number)
'id': show.indexerid,
'episode': "%i" % int(episode.scene_absolute_number)
}
else: else:
post_data['tvdb'] = { param['season'] = episode.scene_season
'id': show.indexerid, param['episode'] = episode.scene_episode
'season': episode.scene_season,
'episode': episode.scene_episode
}
if season: if season:
if show.air_by_date or show.sports: if show.air_by_date or show.sports:
post_data['tvdb'] = { param['season'] = str(season.airdate)[:7]
'id': show.indexerid,
'season': str(season.airdate)[:7],
}
elif show.anime: elif show.anime:
post_data['tvdb'] = { param['season'] = '%d' % season.scene_absolute_number
'id': show.indexerid,
'season': "%d" % season.scene_absolute_number,
}
else: else:
post_data['tvdb'] = { param['season'] = season.scene_season
'id': show.indexerid,
'season': season.scene_season, request_params['tvdb'] = param
}
if search_term: if search_term:
post_data['search'] = search_term request_params['search'] = search_term
return json.dumps(post_data) return json.dumps(request_params)
def seedRatio(self): def get_cache_data(self):
return self.ratio
self._checkAuth()
response_json = self.getURL(self.urls['search'], post_data=self._build_search_strings(), json=True)
if response_json and 'data' in response_json and self.check_auth_from_data(response_json):
return response_json['data']
return []
class HDBitsCache(tvcache.TVCache): class HDBitsCache(tvcache.TVCache):
def __init__(self, provider):
tvcache.TVCache.__init__(self, provider) def __init__(self, this_provider):
tvcache.TVCache.__init__(self, this_provider)
# only poll HDBits every 15 minutes max self.minTime = 15 # cache update frequency
self.minTime = 15
def _getRSSData(self): def _getRSSData(self):
parsedJSON = self.provider.getURL(self.provider.rss_url, post_data=self.provider._make_post_data_JSON(), json=True)
if not self.provider._checkAuthFromData(parsedJSON): return self.provider.get_cache_data()
return []
if parsedJSON and 'data' in parsedJSON:
return parsedJSON['data']
else:
return []
provider = HDBitsProvider() provider = HDBitsProvider()