Refactored few names

This commit is contained in:
Bas Stottelaar
2014-04-06 16:26:32 +02:00
parent 9544b0e616
commit 210c23dd5d
4 changed files with 19 additions and 14 deletions

View File

@@ -597,25 +597,30 @@ def create_https_certificates(ssl_cert, ssl_key):
return True
def request_response(url, method="get", auto_raise=True, status_pass=None, **kwargs):
def request_response(url, method="get", auto_raise=True, whitelist_status_code=None, **kwargs):
"""
Convenient wrapper for `requests.get', which will capture the exceptions and
log them. On success, the Response object is returned. In case of a
exception, None is returned.
"""
if status_pass and type(status_pass) != list:
status_pass = [status_pass]
# Convert whitelist_status_code to a list if needed
if whitelist_status_code and type(whitelist_status_code) != list:
whitelist_status_code = [whitelist_status_code]
# Map method to the request.XXX method. This is a simple hack, but it allows
# requests to apply more magic per method. See it's source.
request_method = requests[method.lower()]
try:
# Request the URL
logger.debug("Requesting URL via %s method: %s", method, url)
response = requests.request(method, url, **kwargs)
response = request_method(url, **kwargs)
# If status code != OK, then raise exception, except if the status code
# is white listed.
if status_pass and auto_raise:
if response.status_code not in status_pass:
if whitelist_status_code and auto_raise:
if response.status_code not in whitelist_status_code:
response.raise_for_status()
else:
logger.debug("Response Status code %d is white listed, not raising exception", response.status_code)

View File

@@ -750,7 +750,7 @@ def getresultNZB(result):
auth=(headphones.HPUSER, headphones.HPPASS),
params={"username": headphones.NEWZBIN_UID, "password": headphones.NEWZBIN_PASSWORD, "reportid": result[2]},
headers={'User-Agent': USER_AGENT},
status_pass=400
whitelist_status_code=400
)
if response.status_code == 400:
@@ -1307,12 +1307,12 @@ def preprocess(resultlist):
return True, result
# Download the torrent file
headers = {}
if result[3] == 'Kick Ass Torrent':
headers = { 'Referer': 'http://kat.ph/' }
headers['Referer'] = 'http://kat.ph/'
elif result[3] == 'What.cd':
headers = { 'User-Agent': 'Headphones' }
else:
headers = {}
headers['User-Agent'] = 'Headphones'
return helpers.request_content(url=result[2], headers=headers), result

View File

@@ -133,9 +133,9 @@ def torrentAction(method, arguments):
# Retrieve session id
if username and password:
response = helpers.request_response(host, auth=(username, password), status_pass=409)
response = helpers.request_response(host, auth=(username, password), whitelist_status_code=409)
else:
response = helpers.request_response(host, status, status_pass=409)
response = helpers.request_response(host, whitelist_status_code=409)
if not response:
logger.error("Error gettings Transmission session ID")

View File

@@ -138,7 +138,7 @@ def checkGithub():
logger.info('Comparing currently installed version with latest GitHub version')
url = 'https://api.github.com/repos/%s/headphones/compare/%s...%s' % (headphones.GIT_USER, headphones.CURRENT_VERSION, headphones.LATEST_VERSION)
commits = helpers.request_json(url, timeout=20, status_pass=[404], validator=lambda x: type(x) == dict)
commits = helpers.request_json(url, timeout=20, whitelist_status_code=404, validator=lambda x: type(x) == dict)
if not commits:
logger.warn('Could not get commits behind from GitHub.')