Fix false +ve outcome from incorrect return value and fix nzbfile implementation.

Change refactor code.
This commit is contained in:
JackDandy 2015-12-17 01:21:09 +00:00
parent a7b5ef1888
commit fcd2810351
5 changed files with 82 additions and 104 deletions

View file

@ -1,4 +1,4 @@
### 0.11.0 (2015-xx-xx xx:xx:xx UTC)
### 0.11.0 (2015-xx-xx xx:xx:xx UTC)
* Change to only refresh scene exception data for shows that need it
* Change reduce aggressive use of scene numbering that was overriding user preference where not needed
@ -96,7 +96,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

View file

@ -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:

View file

@ -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'

View file

@ -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)

View file

@ -634,20 +634,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')