mirror of
https://github.com/SickGear/SickGear.git
synced 2025-01-05 17:43:37 +00:00
Merge branch 'release/0.11.0' into develop
This commit is contained in:
commit
2a762ae7db
6 changed files with 114 additions and 130 deletions
|
@ -1,4 +1,4 @@
|
|||
### 0.12.0 (2015-xx-xx xx:xx:xx UTC)
|
||||
### 0.12.0 (2015-xx-xx xx:xx:xx UTC)
|
||||
|
||||
* Change to generalize xem id fetching
|
||||
|
||||
|
@ -101,7 +101,6 @@
|
|||
* Change position of parsed qualities to the start of log lines
|
||||
* Change to always display branch and commit hash on 'Help & Info' page
|
||||
* Add option to create season search exceptions from editShow page
|
||||
* Change editshow scene exceptions description
|
||||
* Change sab to use requests library
|
||||
|
||||
|
||||
|
|
|
@ -1100,7 +1100,7 @@ def proxy_setting(proxy_setting, request_url, force=False):
|
|||
return (False, proxy_address)[request_url_match], True
|
||||
|
||||
|
||||
def getURL(url, post_data=None, params=None, headers=None, timeout=30, session=None, json=False):
|
||||
def getURL(url, post_data=None, params=None, headers=None, timeout=30, session=None, json=False, **kwargs):
|
||||
"""
|
||||
Returns a byte-string retrieved from the url provider.
|
||||
"""
|
||||
|
@ -1145,9 +1145,9 @@ def getURL(url, post_data=None, params=None, headers=None, timeout=30, session=N
|
|||
|
||||
# decide if we get or post data to server
|
||||
if post_data:
|
||||
resp = session.post(url, data=post_data, timeout=timeout)
|
||||
resp = session.post(url, data=post_data, timeout=timeout, **kwargs)
|
||||
else:
|
||||
resp = session.get(url, timeout=timeout)
|
||||
resp = session.get(url, timeout=timeout, **kwargs)
|
||||
|
||||
if not resp.ok:
|
||||
if resp.status_code in clients.http_error_code:
|
||||
|
|
|
@ -224,7 +224,7 @@ class NewznabProvider(generic.NZBProvider):
|
|||
base_params['apikey'] = api_key
|
||||
|
||||
results = []
|
||||
total = 0
|
||||
total, cnt, search_url, exit_log = 0, len(results), '', False
|
||||
|
||||
for mode in search_params.keys():
|
||||
for i, params in enumerate(search_params[mode]):
|
||||
|
@ -285,8 +285,13 @@ class NewznabProvider(generic.NZBProvider):
|
|||
except AttributeError:
|
||||
break
|
||||
|
||||
# No items found or cache mode, prevent from doing another search
|
||||
if 0 == total or 'Cache' == mode:
|
||||
# No items found, prevent from doing another search
|
||||
if 0 == total:
|
||||
break
|
||||
|
||||
# Cache mode, prevent from doing another search
|
||||
if 'Cache' == mode:
|
||||
exit_log = True
|
||||
break
|
||||
|
||||
if offset != request_params['offset']:
|
||||
|
@ -295,6 +300,7 @@ class NewznabProvider(generic.NZBProvider):
|
|||
|
||||
request_params['offset'] += request_params['limit']
|
||||
if total <= request_params['offset']:
|
||||
exit_log = True
|
||||
logger.log('%s item%s found that will be used for episode matching' % (total, helpers.maybe_plural(total)),
|
||||
logger.DEBUG)
|
||||
break
|
||||
|
@ -304,15 +310,23 @@ class NewznabProvider(generic.NZBProvider):
|
|||
logger.log('%s more item%s to fetch from a batch of up to %s items.'
|
||||
% (items, helpers.maybe_plural(items), request_params['limit']), logger.DEBUG)
|
||||
|
||||
batch_count = len(results) - cnt
|
||||
if batch_count:
|
||||
self._log_search(mode, batch_count, search_url)
|
||||
batch_count = self._log_result(results, mode, cnt, search_url)
|
||||
|
||||
if exit_log:
|
||||
self._log_result(results, mode, cnt, search_url)
|
||||
exit_log = False
|
||||
|
||||
if 'tvdbid' in request_params and len(results):
|
||||
break
|
||||
|
||||
return results
|
||||
|
||||
def _log_result(self, results, mode, cnt, url):
|
||||
count = len(results) - cnt
|
||||
if count:
|
||||
self._log_search(mode, count, url)
|
||||
return count
|
||||
|
||||
|
||||
class NewznabCache(tvcache.TVCache):
|
||||
|
||||
|
|
152
sickbeard/sab.py
152
sickbeard/sab.py
|
@ -16,142 +16,122 @@
|
|||
# You should have received a copy of the GNU General Public License
|
||||
# along with SickGear. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from six import moves
|
||||
|
||||
import sickbeard
|
||||
|
||||
from lib import MultipartPostHandler
|
||||
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
from lib import simplejson as json
|
||||
|
||||
from sickbeard.common import USER_AGENT
|
||||
from sickbeard import logger
|
||||
from sickbeard.exceptions import ex
|
||||
from sickbeard import helpers
|
||||
|
||||
|
||||
def sendNZB(nzb):
|
||||
def send_nzb(nzb):
|
||||
"""
|
||||
Sends an NZB to SABnzbd via the API.
|
||||
Sends an nzb to SABnzbd via the API.
|
||||
|
||||
nzb: The NZBSearchResult object to send to SAB
|
||||
:param nzb: The NZBSearchResult object to send to SAB
|
||||
"""
|
||||
|
||||
# set up a dict with the URL params in it
|
||||
params = {'output': 'json'}
|
||||
if sickbeard.SAB_USERNAME is not None:
|
||||
params['ma_username'] = sickbeard.SAB_USERNAME
|
||||
if sickbeard.SAB_PASSWORD is not None:
|
||||
params['ma_password'] = sickbeard.SAB_PASSWORD
|
||||
if sickbeard.SAB_APIKEY is not None:
|
||||
params['apikey'] = sickbeard.SAB_APIKEY
|
||||
if sickbeard.SAB_CATEGORY is not None:
|
||||
params['cat'] = sickbeard.SAB_CATEGORY
|
||||
success, result, nzb_type = True, '', 'nzb'
|
||||
if nzb.resultType in ('nzb', 'nzbdata'):
|
||||
|
||||
# use high priority if specified (recently aired episode)
|
||||
if nzb.priority == 1:
|
||||
params['priority'] = 1
|
||||
# set up a dict with the URL params in it
|
||||
params = {'output': 'json'}
|
||||
if sickbeard.SAB_USERNAME is not None:
|
||||
params['ma_username'] = sickbeard.SAB_USERNAME
|
||||
if sickbeard.SAB_PASSWORD is not None:
|
||||
params['ma_password'] = sickbeard.SAB_PASSWORD
|
||||
if sickbeard.SAB_APIKEY is not None:
|
||||
params['apikey'] = sickbeard.SAB_APIKEY
|
||||
if sickbeard.SAB_CATEGORY is not None:
|
||||
params['cat'] = sickbeard.SAB_CATEGORY
|
||||
|
||||
# if it's a normal result we just pass SAB the URL
|
||||
if nzb.resultType == 'nzb':
|
||||
params['mode'] = 'addurl'
|
||||
params['name'] = nzb.url
|
||||
# use high priority if specified (recently aired episode)
|
||||
if 1 == nzb.priority:
|
||||
params['priority'] = 1
|
||||
|
||||
# if we get a raw data result we want to upload it to SAB
|
||||
elif nzb.resultType == 'nzbdata':
|
||||
params['mode'] = 'addfile'
|
||||
multiPartParams = {'nzbfile': (nzb.name + '.nzb', nzb.extraInfo[0])}
|
||||
kwargs = {}
|
||||
# if it's a normal result we just pass SAB the URL
|
||||
if 'nzb' == nzb.resultType:
|
||||
nzb_type = 'nzb url'
|
||||
params['mode'] = 'addurl'
|
||||
params['name'] = nzb.url
|
||||
kwargs['params'] = params
|
||||
|
||||
url = sickbeard.SAB_HOST + 'api'
|
||||
# if we get a raw data result we want to upload it to SAB
|
||||
else:
|
||||
nzb_type = 'file nzb'
|
||||
params['mode'] = 'addfile'
|
||||
kwargs['post_data'] = params
|
||||
kwargs['files'] = {'nzbfile': ('%s.nzb' % nzb.name, nzb.extraInfo[0])}
|
||||
|
||||
logger.log(u'Sending NZB to SABnzbd: %s' % nzb.name)
|
||||
logger.log(u'Using SABnzbd URL: %s with parameters: %s' % (url, params), logger.DEBUG)
|
||||
logger.log(u'Sending %s to SABnzbd: %s' % (nzb_type, nzb.name))
|
||||
|
||||
# if we have the URL to an NZB then we've built up the SAB API URL already so just call it
|
||||
if nzb.resultType == 'nzb':
|
||||
success, result = _sabURLOpenSimple(url, params)
|
||||
|
||||
# if we are uploading the NZB data to SAB then we need to build a little POST form and send it
|
||||
elif nzb.resultType == 'nzbdata':
|
||||
headers = {'User-Agent': USER_AGENT}
|
||||
success, result = _sabURLOpenSimple(url, params=params, post_data=multiPartParams, headers=headers)
|
||||
url = '%sapi' % sickbeard.SAB_HOST
|
||||
logger.log(u'SABnzbd at %s sent params: %s' % (url, params), logger.DEBUG)
|
||||
success, result = _get_url(url, **kwargs)
|
||||
|
||||
if not success:
|
||||
return False, result
|
||||
return False
|
||||
|
||||
# do some crude parsing of the result text to determine what SAB said
|
||||
if result['status']:
|
||||
logger.log(u'NZB sent to SABnzbd successfully', logger.DEBUG)
|
||||
if result.get('status'):
|
||||
logger.log(u'Success from SABnzbd using %s' % nzb_type, logger.DEBUG)
|
||||
return True
|
||||
elif 'error' in result:
|
||||
logger.log(u'NZB failed to send to SABnzbd. Return error text is: %s', logger.ERROR)
|
||||
return False
|
||||
logger.log(u'Failed using %s with SABnzbd, response: %s' % (nzb_type, result.get('error', 'und')), logger.ERROR)
|
||||
else:
|
||||
logger.log(u'Unknown failure sending NZB to SABnzbd. Return text is: %s' % result, logger.ERROR)
|
||||
return False
|
||||
logger.log(u'Failure unknown using %s with SABnzbd, response: %s' % (nzb_type, result), logger.ERROR)
|
||||
return False
|
||||
|
||||
|
||||
def _checkSabResponse(result):
|
||||
if len(result) == 0:
|
||||
logger.log(u'No data returned from SABnzbd, NZB not sent', logger.ERROR)
|
||||
def _check_sab_response(result):
|
||||
|
||||
if 0 == len(result):
|
||||
logger.log('No data returned from SABnzbd, nzb not used', logger.ERROR)
|
||||
return False, 'No data from SABnzbd'
|
||||
|
||||
if 'error' in result:
|
||||
logger.log(result['error'], logger.ERROR)
|
||||
return False, result['error']
|
||||
else:
|
||||
return True, result
|
||||
return True, result
|
||||
|
||||
|
||||
def _sabURLOpenSimple(url, params=None, post_data=None, headers=None):
|
||||
result = helpers.getURL(url, params=params, post_data=post_data, headers=headers, json=True)
|
||||
if result is None:
|
||||
logger.log(u'No data returned from SABnzbd', logger.ERROR)
|
||||
return False, u'No data returned from SABnzbd'
|
||||
else:
|
||||
return True, result
|
||||
def _get_url(url, params=None, **kwargs):
|
||||
|
||||
result = sickbeard.helpers.getURL(url, params=params, json=True, **kwargs)
|
||||
if None is result:
|
||||
logger.log('Error, no response from SABnzbd', logger.ERROR)
|
||||
return False, 'Error, no response from SABnzbd'
|
||||
return True, result
|
||||
|
||||
|
||||
def getSabAccesMethod(host=None, username=None, password=None, apikey=None):
|
||||
url = host + 'api'
|
||||
params = {u'mode': u'auth', u'output': u'json'}
|
||||
def access_method(host):
|
||||
|
||||
success, result = _sabURLOpenSimple(url, params=params)
|
||||
success, result = _get_url('%sapi' % host, params={'mode': 'auth', 'output': 'json'})
|
||||
if not success:
|
||||
return False, result
|
||||
|
||||
return True, result['auth']
|
||||
|
||||
|
||||
def testAuthentication(host=None, username=None, password=None, apikey=None):
|
||||
def test_authentication(host=None, username=None, password=None, apikey=None):
|
||||
"""
|
||||
Sends a simple API request to SAB to determine if the given connection information is connect
|
||||
|
||||
host: The host where SAB is running (incl port)
|
||||
username: The username to use for the HTTP request
|
||||
password: The password to use for the HTTP request
|
||||
apikey: The API key to provide to SAB
|
||||
|
||||
Returns: A tuple containing the success boolean and a message
|
||||
:param host: The host where SAB is running (incl port)
|
||||
:param username: The username to use for the HTTP request
|
||||
:param password: The password to use for the HTTP request
|
||||
:param apikey: The API key to provide to SAB
|
||||
"""
|
||||
|
||||
# build up the URL parameters
|
||||
params = {u'mode': u'queue', u'output': u'json', u'ma_username': username, u'ma_password': password, u'apikey': apikey}
|
||||
url = host + 'api'
|
||||
params = {'mode': 'queue', 'ma_username': username, 'ma_password': password, 'apikey': apikey, 'output': 'json'}
|
||||
url = '%sapi' % host
|
||||
|
||||
# send the test request
|
||||
logger.log(u'SABnzbd test URL: %s with parameters: %s' % (url, params), logger.DEBUG)
|
||||
success, result = _sabURLOpenSimple(url, params=params)
|
||||
success, result = _get_url(url, params=params)
|
||||
if not success:
|
||||
return False, result
|
||||
|
||||
# check the result and determine if it's good or not
|
||||
success, sabText = _checkSabResponse(result)
|
||||
success, response = _check_sab_response(result)
|
||||
if not success:
|
||||
return False, sabText
|
||||
|
||||
return True, u'Success'
|
||||
|
||||
return False, response
|
||||
return True, 'Success'
|
||||
|
|
|
@ -117,7 +117,7 @@ def snatch_episode(result, end_status=SNATCHED):
|
|||
if 'blackhole' == sickbeard.NZB_METHOD:
|
||||
dl_result = _download_result(result)
|
||||
elif 'sabnzbd' == sickbeard.NZB_METHOD:
|
||||
dl_result = sab.sendNZB(result)
|
||||
dl_result = sab.send_nzb(result)
|
||||
elif 'nzbget' == sickbeard.NZB_METHOD:
|
||||
is_proper = True if SNATCHED_PROPER == end_status else False
|
||||
dl_result = nzbget.sendNZB(result, is_proper)
|
||||
|
|
|
@ -635,20 +635,19 @@ class Home(MainHandler):
|
|||
self.set_header('Cache-Control', 'max-age=0,no-cache,no-store')
|
||||
|
||||
host = config.clean_url(host)
|
||||
if None is not password and set('*') == set(password):
|
||||
password = sickbeard.SAB_PASSWORD
|
||||
if None is not apikey and starify(apikey, True):
|
||||
apikey = sickbeard.SAB_APIKEY
|
||||
|
||||
connection, accesMsg = sab.getSabAccesMethod(host, username, password, apikey)
|
||||
connection, access_msg = sab.access_method(host)
|
||||
if connection:
|
||||
authed, authMsg = sab.testAuthentication(host, username, password, apikey)
|
||||
if None is not password and set('*') == set(password):
|
||||
password = sickbeard.SAB_PASSWORD
|
||||
if None is not apikey and starify(apikey, True):
|
||||
apikey = sickbeard.SAB_APIKEY
|
||||
|
||||
authed, auth_msg = sab.test_authentication(host, username, password, apikey)
|
||||
if authed:
|
||||
return u'Success. Connected and authenticated'
|
||||
else:
|
||||
return u'Authentication failed. %s' % authMsg
|
||||
else:
|
||||
return u'Unable to connect to host'
|
||||
return u'Success. Connected %s authentication' % \
|
||||
('using %s' % access_msg, 'with no')['None' == auth_msg.lower()]
|
||||
return u'Authentication failed. %s' % auth_msg
|
||||
return u'Unable to connect to host'
|
||||
|
||||
def testTorrent(self, torrent_method=None, host=None, username=None, password=None):
|
||||
self.set_header('Cache-Control', 'max-age=0,no-cache,no-store')
|
||||
|
@ -4241,33 +4240,25 @@ class ConfigProviders(Config):
|
|||
return newProvider.get_id() + '|' + newProvider.config_str()
|
||||
|
||||
def getNewznabCategories(self, name, url, key):
|
||||
'''
|
||||
"""
|
||||
Retrieves a list of possible categories with category id's
|
||||
Using the default url/api?cat
|
||||
http://yournewznaburl.com/api?t=caps&apikey=yourapikey
|
||||
'''
|
||||
error = ''
|
||||
success = False
|
||||
"""
|
||||
|
||||
if not name:
|
||||
error += '\nNo Provider Name specified'
|
||||
if not url:
|
||||
error += '\nNo Provider Url specified'
|
||||
if not key:
|
||||
error += '\nNo Provider Api key specified'
|
||||
error = not name and 'Name' or not url and 'Url' or not key and 'Apikey' or ''
|
||||
if error:
|
||||
error = '\nNo provider %s specified' % error
|
||||
return json.dumps({'success': False, 'error': error})
|
||||
|
||||
if error <> '':
|
||||
return json.dumps({'success' : False, 'error': error})
|
||||
providers = dict(zip([x.get_id() for x in sickbeard.newznabProviderList], sickbeard.newznabProviderList))
|
||||
temp_provider = newznab.NewznabProvider(name, url, key)
|
||||
if None is not key and starify(key, True):
|
||||
temp_provider.key = providers[temp_provider.get_id()].key
|
||||
|
||||
#Get list with Newznabproviders
|
||||
#providerDict = dict(zip([x.get_id() for x in sickbeard.newznabProviderList], sickbeard.newznabProviderList))
|
||||
success, tv_categories, error = temp_provider.get_newznab_categories()
|
||||
|
||||
#Get newznabprovider obj with provided name
|
||||
tempProvider= newznab.NewznabProvider(name, url, key)
|
||||
|
||||
success, tv_categories, error = tempProvider.get_newznab_categories()
|
||||
|
||||
return json.dumps({'success' : success,'tv_categories' : tv_categories, 'error' : error})
|
||||
return json.dumps({'success': success, 'tv_categories': tv_categories, 'error': error})
|
||||
|
||||
def deleteNewznabProvider(self, nnid):
|
||||
|
||||
|
|
Loading…
Reference in a new issue