Merge pull request #459 from JackDandy/feature/ChangeSimplifyTOTV

Change refactor TOTV to use torrent provider simplification and PEP8.
This commit is contained in:
JackDandy 2015-07-05 15:14:34 +01:00
commit 36b6335c7f
2 changed files with 60 additions and 71 deletions

View file

@ -50,6 +50,7 @@
* Change refactor FSH to use torrent provider simplification and PEP8 * Change refactor FSH to use torrent provider simplification and PEP8
* Change refactor IPT to use torrent provider simplification and PEP8 * Change refactor IPT to use torrent provider simplification and PEP8
* Change refactor KAT to use torrent provider simplification and PEP8 * Change refactor KAT to use torrent provider simplification and PEP8
* Change refactor TOTV to use torrent provider simplification and PEP8
* Remove HDTorrents torrent provider * Remove HDTorrents torrent provider
* Remove NextGen torrent provider * Remove NextGen torrent provider
* Add Rarbg torrent provider * Add Rarbg torrent provider

View file

@ -1,5 +1,4 @@
# coding=utf-8 # coding=utf-8
# URL: http://code.google.com/p/sickbeard
# #
# This file is part of SickGear. # This file is part of SickGear.
# #
@ -16,126 +15,115 @@
# 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 datetime
import urllib import urllib
import requests from . import generic
import generic
from sickbeard import logger, tvcache from sickbeard import logger, tvcache
from sickbeard.helpers import mapIndexersToShow from sickbeard.helpers import mapIndexersToShow
from sickbeard.exceptions import AuthException from sickbeard.exceptions import AuthException
class ToTVProvider(generic.TorrentProvider): class ToTVProvider(generic.TorrentProvider):
def __init__(self): def __init__(self):
generic.TorrentProvider.__init__(self, 'ToTV', True, False) generic.TorrentProvider.__init__(self, 'ToTV')
self.api_key = None
self.ratio = None self.url_base = 'https://titansof.tv/'
self.urls = {'config_provider_home_uri': self.url_base,
'search': self.url_base + 'api/torrents?%s',
'get': self.url_base + 'api/torrents/%s/download?apikey=%s'}
self.url = self.urls['config_provider_home_uri']
self.api_key, self.minseed, self.minleech = 3 * [None]
self.cache = ToTVCache(self) self.cache = ToTVCache(self)
self.url = 'https://titansof.tv/api/torrents'
self.download_url = 'http://titansof.tv/api/torrents/%s/download?apikey=%s'
self.session = requests.Session()
def _checkAuth(self): def _check_auth_from_data(self, data_json):
if not self.api_key:
raise AuthException('Your authentication credentials for ' + self.name + ' are missing, check your config.')
return True if 'error' not in data_json:
return True
def _checkAuthFromData(self, data): logger.log(u'Incorrect authentication credentials for %s : %s' % (self.name, data_json['error']),
logger.DEBUG)
raise AuthException('Your authentication credentials for %s are incorrect, check your config.' % self.name)
if 'error' in data: def _doSearch(self, search_params, mode='eponly', epcount=0, age=0):
logger.log(u'Incorrect authentication credentials for ' + self.name + ' : ' + data['error'],
logger.DEBUG)
raise AuthException(
'Your authentication credentials for ' + self.name + ' are incorrect, check your config.')
return True
def _doSearch(self, search_params, search_mode='eponly', epcount=0, age=0):
self._checkAuth() self._checkAuth()
results = []
params = {}
self.headers.update({'X-Authorization': self.api_key}) self.headers.update({'X-Authorization': self.api_key})
results = []
params = {'limit': 100}
mode = ('season' in search_params.keys() and 'Season') or \
('episode' in search_params.keys() and 'Episode') or 'Cache'
if search_params: if search_params:
params.update(search_params) params.update(search_params)
search_url = self.url + '?' + urllib.urlencode(params) search_url = self.urls['search'] % urllib.urlencode(params)
logger.log(u'Search url: %s' % search_url)
parsedJSON = self.getURL(search_url, json=True) # do search data_json = self.getURL(search_url, json=True)
if not parsedJSON: cnt = len(results)
logger.log(u'No data returned from ' + self.name, logger.ERROR) if data_json and 'results' in data_json and self._check_auth_from_data(data_json):
return results for result in data_json['results']:
try:
seeders, leechers = result['seeders'], result['leechers']
if 'Cache' != mode and (seeders < self.minseed or leechers < self.minleech):
continue
if self._checkAuthFromData(parsedJSON): title, download_url = result['release_name'], str(self.urls['get'] % (result['id'], self.api_key))
except (AttributeError, TypeError):
continue
try: if title and download_url:
found_torrents = parsedJSON['results'] results.append((title, download_url))
except:
found_torrents = {}
for result in found_torrents:
(title, url) = self._get_title_and_url(result)
if title and url:
results.append(result)
self._log_result(mode, len(results) - cnt, search_url)
return results return results
def _get_title_and_url(self, parsedJSON): def findPropers(self, search_date=datetime.datetime.today()):
title = parsedJSON['release_name'] return self._find_propers(search_date)
id = parsedJSON['id']
url = self.download_url % (id, self.api_key)
return title, url def _get_season_search_strings(self, ep_obj, **kwargs):
def _get_season_search_strings(self, ep_obj): return self._build_search_str(ep_obj, {'season': 'Season %02d' % ep_obj.scene_season})
search_params = {'limit': 100}
search_params['season'] = 'Season %02d' % ep_obj.scene_season def _get_episode_search_strings(self, ep_obj, add_string='', **kwargs):
if ep_obj.show.indexer == 1:
search_params['series_id'] = ep_obj.show.indexerid
elif ep_obj.show.indexer == 2:
tvdbid = mapIndexersToShow(ep_obj.show)[1]
if tvdbid:
search_params['series_id'] = tvdbid
return [search_params]
def _get_episode_search_strings(self, ep_obj, add_string=''):
if not ep_obj: if not ep_obj:
return [{}] return [{}]
search_params = {'limit': 100}
# Do a general name search for the episode, formatted like SXXEYY # Do a general name search for the episode, formatted like SXXEYY
search_params['episode'] = 'S%02dE%02d' % (ep_obj.scene_season, ep_obj.scene_episode) return self._build_search_str(ep_obj, {'episode': 'S%02dE%02d %s'
% (ep_obj.scene_season, ep_obj.scene_episode, add_string)})
if ep_obj.show.indexer == 1: @staticmethod
def _build_search_str(ep_obj, search_params):
if 1 == ep_obj.show.indexer:
search_params['series_id'] = ep_obj.show.indexerid search_params['series_id'] = ep_obj.show.indexerid
elif ep_obj.show.indexer == 2: elif 2 == ep_obj.show.indexer:
tvdbid = mapIndexersToShow(ep_obj.show)[1] tvdbid = mapIndexersToShow(ep_obj.show)[1]
if tvdbid: if tvdbid:
search_params['series_id'] = tvdbid search_params['series_id'] = tvdbid
return [search_params] return [search_params]
def get_cache_data(self, *args, **kwargs):
return self._doSearch({})
class ToTVCache(tvcache.TVCache): class ToTVCache(tvcache.TVCache):
def __init__(self, provider):
tvcache.TVCache.__init__(self, provider)
# At least 10 minutes between queries def __init__(self, this_provider):
self.minTime = 10 tvcache.TVCache.__init__(self, this_provider)
def _getRSSData(self): def _getRSSData(self):
search_params = {'limit': 100}
return self.provider._doSearch(search_params) return self.provider.get_cache_data()
provider = ToTVProvider() provider = ToTVProvider()