2014-03-10 05:18:05 +00:00
|
|
|
# coding=utf-8
|
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# This file is part of SickGear.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-11-12 16:43:14 +00:00
|
|
|
# SickGear is free software: you can redistribute it and/or modify
|
2014-03-10 05:18:05 +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,
|
2014-03-10 05:18:05 +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-03-10 05:18:05 +00:00
|
|
|
|
2014-05-07 07:50:49 +00:00
|
|
|
import math
|
2015-09-18 00:06:34 +00:00
|
|
|
import re
|
|
|
|
import time
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-16 15:46:33 +00:00
|
|
|
from . import generic
|
2015-09-18 00:06:34 +00:00
|
|
|
from sickbeard import helpers, logger, scene_exceptions, tvcache
|
|
|
|
from sickbeard.helpers import tryInt
|
|
|
|
|
|
|
|
try:
|
|
|
|
import json
|
|
|
|
except ImportError:
|
|
|
|
from lib import simplejson as json
|
|
|
|
import random
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
class BTNProvider(generic.TorrentProvider):
|
2015-06-16 15:46:33 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
def __init__(self):
|
2015-06-16 15:46:33 +00:00
|
|
|
generic.TorrentProvider.__init__(self, 'BTN')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-16 15:46:33 +00:00
|
|
|
self.url_base = 'https://broadcasthe.net'
|
2015-09-18 00:06:34 +00:00
|
|
|
self.url_api = 'https://api.btnapps.net'
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
self.proper_search_terms = ['%.proper.%', '%.repack.%']
|
2015-06-16 15:46:33 +00:00
|
|
|
self.url = self.url_base
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
self.api_key, self.minseed, self.minleech = 3 * [None]
|
|
|
|
self.reject_m2ts = False
|
|
|
|
self.session.headers = {'Content-Type': 'application/json-rpc'}
|
2015-06-16 15:46:33 +00:00
|
|
|
self.cache = BTNCache(self)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def _authorised(self, **kwargs):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
return self._check_auth()
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def _search_provider(self, search_params, age=0, **kwargs):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-07-13 09:39:20 +00:00
|
|
|
self._check_auth()
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-16 15:46:33 +00:00
|
|
|
results = []
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
for mode in search_params.keys():
|
|
|
|
for search_param in search_params[mode]:
|
|
|
|
|
|
|
|
params = {}
|
|
|
|
if 'Propers' == mode:
|
|
|
|
params.update({'release': search_param})
|
|
|
|
age = 4 * 24 * 60 * 60
|
|
|
|
else:
|
|
|
|
search_param and params.update(search_param)
|
|
|
|
age and params.update(dict(age='<=%i' % age)) # age in seconds
|
|
|
|
|
|
|
|
json_rpc = (lambda param_dct, items_per_page=1000, offset=0:
|
|
|
|
'{"jsonrpc": "2.0", "id": "%s", "method": "getTorrents", "params": ["%s", %s, %s, %s]}' %
|
|
|
|
(''.join(random.sample('abcdefghijklmnopqrstuvwxyz0123456789', 8)),
|
|
|
|
self.api_key, json.dumps(param_dct), items_per_page, offset))
|
|
|
|
|
|
|
|
try:
|
|
|
|
response = helpers.getURL(self.url_api, post_data=json_rpc(params), session=self.session, json=True)
|
|
|
|
error_text = response['error']['message']
|
2016-08-26 23:36:01 +00:00
|
|
|
logger.log(
|
|
|
|
('Call Limit' in error_text
|
|
|
|
and u'Action aborted because the %(prov)s 150 calls/hr limit was reached'
|
|
|
|
or u'Action prematurely ended. %(prov)s server error response = %(desc)s') %
|
|
|
|
{'prov': self.name, 'desc': error_text}, logger.WARNING)
|
2015-09-18 00:06:34 +00:00
|
|
|
return results
|
2016-08-26 23:36:01 +00:00
|
|
|
except (KeyError, Exception):
|
2015-09-18 00:06:34 +00:00
|
|
|
data_json = response and 'result' in response and response['result'] or {}
|
|
|
|
|
|
|
|
if data_json:
|
|
|
|
|
2016-08-26 23:36:01 +00:00
|
|
|
found_torrents = 'torrents' in data_json and data_json['torrents'] or {}
|
2015-09-18 00:06:34 +00:00
|
|
|
|
|
|
|
# We got something, we know the API sends max 1000 results at a time.
|
|
|
|
# See if there are more than 1000 results for our query, if not we
|
|
|
|
# keep requesting until we've got everything.
|
|
|
|
# max 150 requests per hour so limit at that. Scan every 15 minutes. 60 / 15 = 4.
|
|
|
|
max_pages = 5 # 150 was the old value and impractical
|
|
|
|
results_per_page = 1000
|
|
|
|
|
|
|
|
if 'results' in data_json and int(data_json['results']) >= results_per_page:
|
|
|
|
pages_needed = int(math.ceil(int(data_json['results']) / results_per_page))
|
|
|
|
if pages_needed > max_pages:
|
|
|
|
pages_needed = max_pages
|
|
|
|
|
|
|
|
# +1 because range(1,4) = 1, 2, 3
|
|
|
|
for page in range(1, pages_needed + 1):
|
|
|
|
|
|
|
|
try:
|
2016-08-26 23:36:01 +00:00
|
|
|
response = helpers.getURL(
|
|
|
|
self.url_api, json=True, session=self.session,
|
|
|
|
post_data=json_rpc(params, results_per_page, page * results_per_page))
|
2015-09-18 00:06:34 +00:00
|
|
|
error_text = response['error']['message']
|
2016-08-26 23:36:01 +00:00
|
|
|
logger.log(
|
|
|
|
('Call Limit' in error_text
|
|
|
|
and u'Action prematurely ended because the %(prov)s 150 calls/hr limit was reached'
|
|
|
|
or u'Action prematurely ended. %(prov)s server error response = %(desc)s') %
|
|
|
|
{'prov': self.name, 'desc': error_text}, logger.WARNING)
|
2015-09-18 00:06:34 +00:00
|
|
|
return results
|
2016-08-26 23:36:01 +00:00
|
|
|
except (KeyError, Exception):
|
2015-09-18 00:06:34 +00:00
|
|
|
data_json = response and 'result' in response and response['result'] or {}
|
|
|
|
|
2016-08-26 23:36:01 +00:00
|
|
|
# Note that this these are individual requests and might time out individually.
|
|
|
|
# This would result in 'gaps' in the results. There is no way to fix this though.
|
2015-09-18 00:06:34 +00:00
|
|
|
if 'torrents' in data_json:
|
|
|
|
found_torrents.update(data_json['torrents'])
|
|
|
|
|
|
|
|
cnt = len(results)
|
|
|
|
for torrentid, torrent_info in found_torrents.iteritems():
|
2016-08-26 23:36:01 +00:00
|
|
|
seeders, leechers, size = (tryInt(n, n) for n in [torrent_info.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.reject_m2ts and re.match(r'(?i)m2?ts', torrent_info.get('Container', '')):
|
|
|
|
continue
|
|
|
|
|
2016-08-26 23:36:01 +00:00
|
|
|
title, url = self._get_title_and_url(torrent_info)
|
2015-09-18 00:06:34 +00:00
|
|
|
if title and url:
|
2016-08-26 23:36:01 +00:00
|
|
|
results.append((title, url, seeders, self._bytesizer(size)))
|
2015-09-18 00:06:34 +00:00
|
|
|
|
2015-12-02 15:19:28 +00:00
|
|
|
self._log_search(mode, len(results) - cnt,
|
|
|
|
('search_param: ' + str(search_param), self.name)['Cache' == mode])
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-08-26 23:36:01 +00:00
|
|
|
results = self._sort_seeding(mode, results)
|
|
|
|
|
2015-06-16 15:46:33 +00:00
|
|
|
return results
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-08-26 23:36:01 +00:00
|
|
|
@staticmethod
|
|
|
|
def _get_title_and_url(data_json):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
# The BTN API gives a lot of information in response,
|
2014-11-12 16:43:14 +00:00
|
|
|
# however SickGear is built mostly around Scene or
|
2014-03-10 05:18:05 +00:00
|
|
|
# release names, which is why we are using them here.
|
|
|
|
|
2015-06-16 15:46:33 +00:00
|
|
|
if 'ReleaseName' in data_json and data_json['ReleaseName']:
|
|
|
|
title = data_json['ReleaseName']
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
# If we don't have a release name we need to get creative
|
|
|
|
title = u''
|
2015-06-16 15:46:33 +00:00
|
|
|
keys = ['Series', 'GroupName', 'Resolution', 'Source', 'Codec']
|
|
|
|
for key in keys:
|
|
|
|
if key in data_json:
|
|
|
|
title += ('', '.')[any(title)] + data_json[key]
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if title:
|
|
|
|
title = title.replace(' ', '.')
|
|
|
|
|
|
|
|
url = None
|
2015-06-16 15:46:33 +00:00
|
|
|
if 'DownloadURL' in data_json:
|
|
|
|
url = data_json['DownloadURL']
|
2014-03-10 05:18:05 +00:00
|
|
|
if url:
|
|
|
|
# unescaped / is valid in JSON, but it can be escaped
|
2015-06-16 15:46:33 +00:00
|
|
|
url = url.replace('\\/', '/')
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-06-16 15:46:33 +00:00
|
|
|
return title, url
|
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def _season_strings(self, ep_obj, **kwargs):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
search_params = []
|
2015-09-18 00:06:34 +00:00
|
|
|
base_params = {'category': 'Season'}
|
2014-07-15 02:00:53 +00:00
|
|
|
|
|
|
|
# Search for entire seasons: no need to do special things for air by date or sports shows
|
2015-08-14 23:02:05 +00:00
|
|
|
if ep_obj.show.air_by_date or ep_obj.show.is_sports:
|
2014-07-15 02:00:53 +00:00
|
|
|
# Search for the year of the air by date show
|
2015-09-18 00:06:34 +00:00
|
|
|
base_params['name'] = str(ep_obj.airdate).split('-')[0]
|
2015-06-16 15:46:33 +00:00
|
|
|
elif ep_obj.show.is_anime:
|
2015-09-18 00:06:34 +00:00
|
|
|
base_params['name'] = '%s' % ep_obj.scene_absolute_number
|
2014-07-15 02:00:53 +00:00
|
|
|
else:
|
2015-09-18 00:06:34 +00:00
|
|
|
base_params['name'] = 'Season %s' % (ep_obj.season, ep_obj.scene_season)[bool(ep_obj.show.is_scene)]
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2015-06-16 15:46:33 +00:00
|
|
|
if 1 == ep_obj.show.indexer:
|
2015-09-18 00:06:34 +00:00
|
|
|
base_params['tvdb'] = ep_obj.show.indexerid
|
|
|
|
search_params.append(base_params)
|
|
|
|
# elif 2 == ep_obj.show.indexer:
|
|
|
|
# current_params['tvrage'] = ep_obj.show.indexerid
|
|
|
|
# search_params.append(current_params)
|
2014-07-15 02:00:53 +00:00
|
|
|
else:
|
|
|
|
name_exceptions = list(
|
2015-09-18 00:06:34 +00:00
|
|
|
set([helpers.sanitizeSceneName(a) for a in
|
|
|
|
scene_exceptions.get_scene_exceptions(ep_obj.show.indexerid) + [ep_obj.show.name]]))
|
2014-07-15 02:00:53 +00:00
|
|
|
for name in name_exceptions:
|
2015-09-18 00:06:34 +00:00
|
|
|
series_param = {'series': name}
|
|
|
|
series_param.update(base_params)
|
|
|
|
search_params.append(series_param)
|
2014-05-04 03:16:26 +00:00
|
|
|
|
2016-08-26 23:36:01 +00:00
|
|
|
return [dict(Season=search_params)]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def _episode_strings(self, ep_obj, **kwargs):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
if not ep_obj:
|
2014-03-10 05:18:05 +00:00
|
|
|
return [{}]
|
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
search_params = []
|
|
|
|
base_params = {'category': 'Episode'}
|
2014-04-22 21:58:48 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
# episode
|
2015-08-14 23:02:05 +00:00
|
|
|
if ep_obj.show.air_by_date or ep_obj.show.is_sports:
|
2014-04-30 13:49:50 +00:00
|
|
|
date_str = str(ep_obj.airdate)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
# BTN uses dots in dates, we just search for the date since that
|
|
|
|
# combined with the series identifier should result in just one episode
|
2015-09-18 00:06:34 +00:00
|
|
|
base_params['name'] = date_str.replace('-', '.')
|
2015-08-14 23:02:05 +00:00
|
|
|
elif ep_obj.show.is_anime:
|
2015-09-18 00:06:34 +00:00
|
|
|
base_params['name'] = '%s' % ep_obj.scene_absolute_number
|
2014-06-06 23:55:14 +00:00
|
|
|
else:
|
2014-03-10 05:18:05 +00:00
|
|
|
# Do a general name search for the episode, formatted like SXXEYY
|
2015-08-14 23:02:05 +00:00
|
|
|
season, episode = ((ep_obj.season, ep_obj.episode),
|
|
|
|
(ep_obj.scene_season, ep_obj.scene_episode))[bool(ep_obj.show.is_scene)]
|
2015-09-18 00:06:34 +00:00
|
|
|
base_params['name'] = 'S%02dE%02d' % (season, episode)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
# search
|
2015-06-16 15:46:33 +00:00
|
|
|
if 1 == ep_obj.show.indexer:
|
2015-09-18 00:06:34 +00:00
|
|
|
base_params['tvdb'] = ep_obj.show.indexerid
|
|
|
|
search_params.append(base_params)
|
|
|
|
# elif 2 == ep_obj.show.indexer:
|
|
|
|
# search_params['tvrage'] = ep_obj.show.indexerid
|
|
|
|
# to_return.append(search_params)
|
2014-07-15 02:00:53 +00:00
|
|
|
else:
|
2014-03-10 05:18:05 +00:00
|
|
|
# add new query string for every exception
|
2014-07-15 02:00:53 +00:00
|
|
|
name_exceptions = list(
|
2015-09-18 00:06:34 +00:00
|
|
|
set([helpers.sanitizeSceneName(a) for a in
|
|
|
|
scene_exceptions.get_scene_exceptions(ep_obj.show.indexerid) + [ep_obj.show.name]]))
|
|
|
|
for name in name_exceptions:
|
|
|
|
series_param = {'series': name}
|
|
|
|
series_param.update(base_params)
|
|
|
|
search_params.append(series_param)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-08-26 23:36:01 +00:00
|
|
|
return [dict(Episode=search_params)]
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def cache_data(self, **kwargs):
|
2015-06-16 15:46:33 +00:00
|
|
|
|
|
|
|
# Get the torrents uploaded since last check.
|
|
|
|
seconds_since_last_update = int(math.ceil(time.time() - time.mktime(kwargs['age'])))
|
|
|
|
|
|
|
|
# default to 15 minutes
|
|
|
|
seconds_min_time = kwargs['min_time'] * 60
|
|
|
|
if seconds_min_time > seconds_since_last_update:
|
|
|
|
seconds_since_last_update = seconds_min_time
|
|
|
|
|
|
|
|
# Set maximum to 24 hours (24 * 60 * 60 = 86400 seconds) of "RSS" data search,
|
|
|
|
# older items will be done through backlog
|
|
|
|
if 86400 < seconds_since_last_update:
|
2016-08-26 23:36:01 +00:00
|
|
|
logger.log(u'Only trying to fetch the last 24 hours even though the last known successful update on ' +
|
|
|
|
'%s was over 24 hours' % self.name, logger.WARNING)
|
2015-06-16 15:46:33 +00:00
|
|
|
seconds_since_last_update = 86400
|
|
|
|
|
2016-08-26 23:36:01 +00:00
|
|
|
return self._search_provider(dict(Cache=['']), age=seconds_since_last_update)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
class BTNCache(tvcache.TVCache):
|
|
|
|
|
2015-06-16 15:46:33 +00:00
|
|
|
def __init__(self, this_provider):
|
|
|
|
tvcache.TVCache.__init__(self, this_provider)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2016-08-26 23:36:01 +00:00
|
|
|
self.update_freq = 15
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
def _cache_data(self):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2015-09-18 00:06:34 +00:00
|
|
|
return self.provider.cache_data(age=self._getLastUpdate().timetuple(), min_time=self.update_freq)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
provider = BTNProvider()
|