This commit is contained in:
Bas Stottelaar
2014-04-06 16:14:51 +02:00
parent 51c32e09bd
commit 084bf59b6a
+27 -11
View File
@@ -24,8 +24,10 @@ import headphones
from headphones import logger from headphones import logger
from xml.dom import minidom
from operator import itemgetter from operator import itemgetter
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from beets.mediafile import MediaFile, FileTypeError, UnreadableFileError from beets.mediafile import MediaFile, FileTypeError, UnreadableFileError
# Modified from https://github.com/Verrus/beets-plugin-featInTitle # Modified from https://github.com/Verrus/beets-plugin-featInTitle
@@ -595,17 +597,20 @@ def create_https_certificates(ssl_cert, ssl_key):
return True return True
def request_response(url, method="GET", auto_raise=True, status_pass=None, *args, **kwargs): def request_response(url, method="get", auto_raise=True, status_pass=None, **kwargs):
""" """
Convenient wrapper for `requests.get', which will capture the exceptions and Convenient wrapper for `requests.get', which will capture the exceptions and
log them. On success, the Response object is returned. In case of a log them. On success, the Response object is returned. In case of a
exception, None is returned. exception, None is returned.
""" """
if status_pass and type(status_pass) != list:
status_pass = [status_pass]
try: try:
# Request the URL # Request the URL
logger.debug("Requesting URL via %s method: %s", method, url) logger.debug("Requesting URL via %s method: %s", method, url)
response = requests.request(method, url, *args, **kwargs) response = requests.request(method, url, **kwargs)
# If status code != OK, then raise exception, except if the status code # If status code != OK, then raise exception, except if the status code
# is white listed. # is white listed.
@@ -630,18 +635,29 @@ def request_response(url, method="GET", auto_raise=True, status_pass=None, *args
except requests.RequestException, e: except requests.RequestException, e:
logger.error("Request raised exception: %s", e) logger.error("Request raised exception: %s", e)
def request_soup(*args, **kwargs): def request_soup(url, **kwargs):
""" """
Wrapper for `request_response', which will return a BeatifulSoup object if Wrapper for `request_response', which will return a BeatifulSoup object if
no exceptions are raised. no exceptions are raised.
""" """
response = request_response(*args, **kwargs) response = request_response(url, **kwargs)
if response is not None: if response is not None:
return BeautifulSoup(response.content) return BeautifulSoup(response.content)
def request_json(*args, **kwargs): def request_minidom(url, **kwargs):
"""
Wrapper for `request_response', which will return a Minidom object if no
exceptions are raised.
"""
response = request_response(url, **kwargs)
if response is not None:
return minidom.parseString(response.content)
def request_json(url, **kwargs):
""" """
Wrapper for `request_response', which will decode the response as JSON Wrapper for `request_response', which will decode the response as JSON
object and return the result, if no exceptions are raised. object and return the result, if no exceptions are raised.
@@ -650,8 +666,8 @@ def request_json(*args, **kwargs):
the result is valid. the result is valid.
""" """
validator = kwargs.pop("validator") validator = kwargs.pop("validator", None)
response = request_response(*args, **kwargs) response = request_response(url, **kwargs)
if response is not None: if response is not None:
try: try:
@@ -664,22 +680,22 @@ def request_json(*args, **kwargs):
except ValueError: except ValueError:
logger.error("Response returned invalid JSON data") logger.error("Response returned invalid JSON data")
def request_content(*args, **kwargs): def request_content(url, **kwargs):
""" """
Wrapper for `request_response', which will return the raw content. Wrapper for `request_response', which will return the raw content.
""" """
response = request_response(*args, **kwargs) response = request_response(url, **kwargs)
if response is not None: if response is not None:
return response.content return response.content
def request_feed(*args, **kwargs): def request_feed(url, **kwargs):
""" """
Wrapper for `request_response', which will return a feed object. Wrapper for `request_response', which will return a feed object.
""" """
response = request_response(*args, **kwargs) response = request_response(url, **kwargs)
if response is not None: if response is not None:
return feedparser.parse(response.content) return feedparser.parse(response.content)