2014-03-10 05:18:05 +00:00
|
|
|
# coding=utf-8
|
2014-04-22 08:02:43 +00:00
|
|
|
# Author: Daniel Heimans
|
2014-03-10 05:18:05 +00:00
|
|
|
# URL: http://code.google.com/p/sickbeard
|
|
|
|
#
|
2014-05-23 12:37:22 +00:00
|
|
|
# This file is part of SickRage.
|
2014-03-10 05:18:05 +00:00
|
|
|
#
|
2014-05-23 12:37:22 +00:00
|
|
|
# SickRage 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-05-23 12:37:22 +00:00
|
|
|
# SickRage 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-05-23 12:37:22 +00:00
|
|
|
# along with SickRage. 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 time
|
|
|
|
import socket
|
|
|
|
import math
|
2014-03-10 05:18:05 +00:00
|
|
|
import sickbeard
|
|
|
|
import generic
|
|
|
|
|
|
|
|
from sickbeard import classes
|
|
|
|
from sickbeard import scene_exceptions
|
|
|
|
from sickbeard import logger
|
|
|
|
from sickbeard import tvcache
|
2014-07-24 04:44:11 +00:00
|
|
|
from sickbeard.helpers import sanitizeSceneName
|
2014-03-10 05:18:05 +00:00
|
|
|
from sickbeard.exceptions import ex, AuthException
|
|
|
|
|
|
|
|
from lib import jsonrpclib
|
|
|
|
from datetime import datetime
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
class BTNProvider(generic.TorrentProvider):
|
|
|
|
def __init__(self):
|
|
|
|
generic.TorrentProvider.__init__(self, "BTN")
|
|
|
|
|
|
|
|
self.supportsBacklog = True
|
2014-05-27 07:44:23 +00:00
|
|
|
self.supportsAbsoluteNumbering = True
|
2014-05-17 05:23:11 +00:00
|
|
|
|
|
|
|
self.enabled = False
|
|
|
|
self.api_key = None
|
|
|
|
self.ratio = None
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
self.cache = BTNCache(self)
|
|
|
|
|
2014-05-28 21:13:29 +00:00
|
|
|
self.url = "http://api.btnapps.net"
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def isEnabled(self):
|
2014-05-17 05:23:11 +00:00
|
|
|
return self.enabled
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def imageName(self):
|
|
|
|
return 'btn.png'
|
|
|
|
|
|
|
|
def _checkAuth(self):
|
2014-05-17 05:23:11 +00:00
|
|
|
if not self.api_key:
|
2014-03-10 05:18:05 +00:00
|
|
|
raise AuthException("Your authentication credentials for " + self.name + " are missing, check your config.")
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _checkAuthFromData(self, parsedJSON):
|
|
|
|
|
|
|
|
if parsedJSON is None:
|
|
|
|
return self._checkAuth()
|
|
|
|
|
|
|
|
if 'api-error' in parsedJSON:
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.log(u"Incorrect authentication credentials for " + self.name + " : " + parsedJSON['api-error'],
|
|
|
|
logger.DEBUG)
|
|
|
|
raise AuthException(
|
|
|
|
"Your authentication credentials for " + self.name + " are incorrect, check your config.")
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2014-07-21 05:47:13 +00:00
|
|
|
def _doSearch(self, search_params, search_mode='eponly', epcount=0, age=0):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
self._checkAuth()
|
|
|
|
|
|
|
|
params = {}
|
2014-05-17 05:23:11 +00:00
|
|
|
apikey = self.api_key
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
# age in seconds
|
|
|
|
if age:
|
|
|
|
params['age'] = "<=" + str(int(age))
|
|
|
|
|
|
|
|
if search_params:
|
|
|
|
params.update(search_params)
|
|
|
|
|
|
|
|
parsedJSON = self._api_call(apikey, params)
|
|
|
|
|
|
|
|
if not parsedJSON:
|
|
|
|
logger.log(u"No data returned from " + self.name, logger.ERROR)
|
|
|
|
return []
|
|
|
|
|
|
|
|
if self._checkAuthFromData(parsedJSON):
|
|
|
|
|
|
|
|
if 'torrents' in parsedJSON:
|
|
|
|
found_torrents = parsedJSON['torrents']
|
|
|
|
else:
|
|
|
|
found_torrents = {}
|
|
|
|
|
|
|
|
# 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.
|
2014-07-15 02:00:53 +00:00
|
|
|
max_pages = 150
|
2014-03-10 05:18:05 +00:00
|
|
|
results_per_page = 1000
|
|
|
|
|
|
|
|
if 'results' in parsedJSON and int(parsedJSON['results']) >= results_per_page:
|
|
|
|
pages_needed = int(math.ceil(int(parsedJSON['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):
|
|
|
|
parsedJSON = self._api_call(apikey, params, results_per_page, page * results_per_page)
|
|
|
|
# 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.
|
|
|
|
if 'torrents' in parsedJSON:
|
|
|
|
found_torrents.update(parsedJSON['torrents'])
|
|
|
|
|
|
|
|
results = []
|
|
|
|
for torrentid, torrent_info in found_torrents.iteritems():
|
|
|
|
(title, url) = self._get_title_and_url(torrent_info)
|
|
|
|
|
|
|
|
if title and url:
|
|
|
|
results.append(torrent_info)
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
return []
|
|
|
|
|
2014-03-20 18:03:22 +00:00
|
|
|
def _api_call(self, apikey, params={}, results_per_page=1000, offset=0):
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-05-28 21:13:29 +00:00
|
|
|
server = jsonrpclib.Server(self.url)
|
2014-03-10 05:18:05 +00:00
|
|
|
parsedJSON = {}
|
|
|
|
|
|
|
|
try:
|
|
|
|
parsedJSON = server.getTorrents(apikey, params, int(results_per_page), int(offset))
|
|
|
|
|
2014-04-28 02:51:15 +00:00
|
|
|
except jsonrpclib.jsonrpc.ProtocolError, error:
|
2014-03-10 05:18:05 +00:00
|
|
|
logger.log(u"JSON-RPC protocol error while accessing " + self.name + ": " + ex(error), logger.ERROR)
|
|
|
|
parsedJSON = {'api-error': ex(error)}
|
|
|
|
return parsedJSON
|
|
|
|
|
|
|
|
except socket.timeout:
|
|
|
|
logger.log(u"Timeout while accessing " + self.name, logger.WARNING)
|
|
|
|
|
|
|
|
except socket.error, error:
|
|
|
|
# Note that sometimes timeouts are thrown as socket errors
|
|
|
|
logger.log(u"Socket error while accessing " + self.name + ": " + error[1], logger.ERROR)
|
|
|
|
|
|
|
|
except Exception, error:
|
|
|
|
errorstring = str(error)
|
2014-03-25 05:57:24 +00:00
|
|
|
if (errorstring.startswith('<') and errorstring.endswith('>')):
|
2014-03-10 05:18:05 +00:00
|
|
|
errorstring = errorstring[1:-1]
|
|
|
|
logger.log(u"Unknown error while accessing " + self.name + ": " + errorstring, logger.ERROR)
|
|
|
|
|
|
|
|
return parsedJSON
|
|
|
|
|
|
|
|
def _get_title_and_url(self, parsedJSON):
|
|
|
|
|
|
|
|
# The BTN API gives a lot of information in response,
|
2014-05-23 12:37:22 +00:00
|
|
|
# however SickRage is built mostly around Scene or
|
2014-03-10 05:18:05 +00:00
|
|
|
# release names, which is why we are using them here.
|
|
|
|
|
|
|
|
if 'ReleaseName' in parsedJSON and parsedJSON['ReleaseName']:
|
|
|
|
title = parsedJSON['ReleaseName']
|
|
|
|
|
|
|
|
else:
|
|
|
|
# If we don't have a release name we need to get creative
|
|
|
|
title = u''
|
|
|
|
if 'Series' in parsedJSON:
|
|
|
|
title += parsedJSON['Series']
|
|
|
|
if 'GroupName' in parsedJSON:
|
|
|
|
title += '.' + parsedJSON['GroupName'] if title else parsedJSON['GroupName']
|
|
|
|
if 'Resolution' in parsedJSON:
|
|
|
|
title += '.' + parsedJSON['Resolution'] if title else parsedJSON['Resolution']
|
|
|
|
if 'Source' in parsedJSON:
|
|
|
|
title += '.' + parsedJSON['Source'] if title else parsedJSON['Source']
|
|
|
|
if 'Codec' in parsedJSON:
|
|
|
|
title += '.' + parsedJSON['Codec'] if title else parsedJSON['Codec']
|
|
|
|
if title:
|
|
|
|
title = title.replace(' ', '.')
|
|
|
|
|
|
|
|
url = None
|
|
|
|
if 'DownloadURL' in parsedJSON:
|
|
|
|
url = parsedJSON['DownloadURL']
|
|
|
|
if url:
|
|
|
|
# unescaped / is valid in JSON, but it can be escaped
|
|
|
|
url = url.replace("\\/", "/")
|
|
|
|
|
|
|
|
return (title, url)
|
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
def _get_season_search_strings(self, ep_obj):
|
2014-03-10 05:18:05 +00:00
|
|
|
search_params = []
|
2014-07-15 02:00:53 +00:00
|
|
|
current_params = {'category': 'Season'}
|
|
|
|
|
|
|
|
# Search for entire seasons: no need to do special things for air by date or sports shows
|
|
|
|
if ep_obj.show.air_by_date or ep_obj.show.sports:
|
|
|
|
# Search for the year of the air by date show
|
|
|
|
current_params['name'] = str(ep_obj.airdate).split('-')[0]
|
|
|
|
elif ep_obj.show.is_anime:
|
|
|
|
current_params['name'] = "%d" % ep_obj.scene_absolute_number
|
|
|
|
else:
|
|
|
|
current_params['name'] = 'Season ' + str(ep_obj.scene_season)
|
|
|
|
|
|
|
|
# search
|
|
|
|
if ep_obj.show.indexer == 1:
|
|
|
|
current_params['tvdb'] = ep_obj.show.indexerid
|
|
|
|
search_params.append(current_params)
|
|
|
|
elif ep_obj.show.indexer == 2:
|
|
|
|
current_params['tvrage'] = ep_obj.show.indexerid
|
|
|
|
search_params.append(current_params)
|
|
|
|
else:
|
|
|
|
name_exceptions = list(
|
|
|
|
set(scene_exceptions.get_scene_exceptions(ep_obj.show.indexerid) + [ep_obj.show.name]))
|
|
|
|
for name in name_exceptions:
|
2014-05-12 16:35:40 +00:00
|
|
|
# Search by name if we don't have tvdb or tvrage id
|
|
|
|
current_params['series'] = sanitizeSceneName(name)
|
2014-07-15 02:00:53 +00:00
|
|
|
search_params.append(current_params)
|
2014-05-04 03:16:26 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
return search_params
|
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
def _get_episode_search_strings(self, ep_obj, add_string=''):
|
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 [{}]
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
to_return = []
|
2014-03-10 05:18:05 +00:00
|
|
|
search_params = {'category': 'Episode'}
|
2014-04-22 21:58:48 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
# episode
|
|
|
|
if ep_obj.show.air_by_date or ep_obj.show.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
|
|
|
|
search_params['name'] = date_str.replace('-', '.')
|
2014-07-15 02:00:53 +00:00
|
|
|
elif ep_obj.show.anime:
|
2014-06-06 23:55:14 +00:00
|
|
|
search_params['name'] = "%i" % int(ep_obj.scene_absolute_number)
|
|
|
|
else:
|
2014-03-10 05:18:05 +00:00
|
|
|
# Do a general name search for the episode, formatted like SXXEYY
|
2014-04-30 13:49:50 +00:00
|
|
|
search_params['name'] = "S%02dE%02d" % (ep_obj.scene_season, ep_obj.scene_episode)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
# search
|
|
|
|
if ep_obj.show.indexer == 1:
|
|
|
|
search_params['tvdb'] = ep_obj.show.indexerid
|
|
|
|
to_return.append(search_params)
|
|
|
|
elif ep_obj.show.indexer == 2:
|
|
|
|
search_params['tvrage'] = ep_obj.show.indexerid
|
|
|
|
to_return.append(search_params)
|
|
|
|
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(
|
|
|
|
set(scene_exceptions.get_scene_exceptions(ep_obj.show.indexerid) + [ep_obj.show.name]))
|
2014-03-10 05:18:05 +00:00
|
|
|
for cur_exception in name_exceptions:
|
2014-07-15 02:00:53 +00:00
|
|
|
search_params['series'] = sanitizeSceneName(cur_exception)
|
|
|
|
to_return.append(search_params)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
return to_return
|
|
|
|
|
2014-04-30 13:49:50 +00:00
|
|
|
def _doGeneralSearch(self, search_string):
|
2014-03-10 05:18:05 +00:00
|
|
|
# 'search' looks as broad is it can find. Can contain episode overview and title for example,
|
|
|
|
# use with caution!
|
2014-04-30 13:49:50 +00:00
|
|
|
return self._doSearch({'search': search_string})
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def findPropers(self, search_date=None):
|
|
|
|
results = []
|
|
|
|
|
|
|
|
search_terms = ['%.proper.%', '%.repack.%']
|
|
|
|
|
|
|
|
for term in search_terms:
|
|
|
|
for item in self._doSearch({'release': term}, age=4 * 24 * 60 * 60):
|
|
|
|
if item['Time']:
|
|
|
|
try:
|
|
|
|
result_date = datetime.fromtimestamp(float(item['Time']))
|
|
|
|
except TypeError:
|
|
|
|
result_date = None
|
|
|
|
|
|
|
|
if result_date:
|
|
|
|
if not search_date or result_date > search_date:
|
|
|
|
title, url = self._get_title_and_url(item)
|
2014-07-15 02:00:53 +00:00
|
|
|
results.append(classes.Proper(title, url, result_date, self.show))
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
2014-05-08 22:28:28 +00:00
|
|
|
def seedRatio(self):
|
2014-05-17 05:23:11 +00:00
|
|
|
return self.ratio
|
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):
|
|
|
|
def __init__(self, provider):
|
|
|
|
tvcache.TVCache.__init__(self, provider)
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
# At least 15 minutes between queries
|
2014-03-10 05:18:05 +00:00
|
|
|
self.minTime = 15
|
|
|
|
|
|
|
|
def updateCache(self):
|
|
|
|
|
2014-05-18 15:33:31 +00:00
|
|
|
# delete anything older then 7 days
|
|
|
|
self._clearCache()
|
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
if not self.shouldUpdate():
|
|
|
|
return
|
|
|
|
|
|
|
|
if self._checkAuth(None):
|
|
|
|
|
|
|
|
data = self._getRSSData()
|
|
|
|
|
|
|
|
# As long as we got something from the provider we count it as an update
|
|
|
|
if data:
|
|
|
|
self.setLastUpdate()
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
if self._checkAuth(data):
|
|
|
|
# By now we know we've got data and no auth errors, all we need to do is put it in the database
|
|
|
|
cl = []
|
|
|
|
for item in data:
|
2014-05-08 14:03:50 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
ci = self._parseItem(item)
|
|
|
|
if ci is not None:
|
|
|
|
cl.append(ci)
|
2014-03-25 05:57:24 +00:00
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
if len(cl) > 0:
|
2014-06-22 01:35:57 +00:00
|
|
|
myDB = self._getDB()
|
|
|
|
myDB.mass_action(cl)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
else:
|
2014-03-25 05:57:24 +00:00
|
|
|
raise AuthException(
|
|
|
|
"Your authentication info for " + self.provider.name + " is incorrect, check your config")
|
2014-03-10 05:18:05 +00:00
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def _getRSSData(self):
|
|
|
|
# Get the torrents uploaded since last check.
|
|
|
|
seconds_since_last_update = math.ceil(time.time() - time.mktime(self._getLastUpdate().timetuple()))
|
|
|
|
|
|
|
|
# default to 15 minutes
|
|
|
|
seconds_minTime = self.minTime * 60
|
|
|
|
if seconds_since_last_update < seconds_minTime:
|
|
|
|
seconds_since_last_update = seconds_minTime
|
|
|
|
|
|
|
|
# Set maximum to 24 hours (24 * 60 * 60 = 86400 seconds) of "RSS" data search, older things will need to be done through backlog
|
|
|
|
if seconds_since_last_update > 86400:
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.log(
|
|
|
|
u"The last known successful update on " + self.provider.name + " was more than 24 hours ago, only trying to fetch the last 24 hours!",
|
|
|
|
logger.WARNING)
|
2014-03-10 05:18:05 +00:00
|
|
|
seconds_since_last_update = 86400
|
|
|
|
|
2014-04-28 10:57:30 +00:00
|
|
|
return self.provider._doSearch(search_params=None, age=seconds_since_last_update)
|
2014-03-10 05:18:05 +00:00
|
|
|
|
|
|
|
def _parseItem(self, item):
|
|
|
|
(title, url) = self.provider._get_title_and_url(item)
|
|
|
|
|
|
|
|
if title and url:
|
|
|
|
logger.log(u"Adding item to results: " + title, logger.DEBUG)
|
|
|
|
return self._addCacheEntry(title, url)
|
|
|
|
else:
|
2014-03-25 05:57:24 +00:00
|
|
|
logger.log(u"The data returned from the " + self.provider.name + " is incomplete, this result is unusable",
|
|
|
|
logger.ERROR)
|
2014-03-10 05:18:05 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def _checkAuth(self, data):
|
|
|
|
return self.provider._checkAuthFromData(data)
|
|
|
|
|
2014-07-15 02:00:53 +00:00
|
|
|
|
2014-03-10 05:18:05 +00:00
|
|
|
provider = BTNProvider()
|