Support requests module >= 1.0

This commit is contained in:
Bastian Kleineidam 2012-12-19 20:43:18 +01:00
parent afa93b498b
commit 6a2f57b132
2 changed files with 24 additions and 6 deletions

View file

@ -1,3 +1,10 @@
Dosage 1.8 (released 19.12.2012)
Changes:
- comics: Add compatibility to requests module >= 1.0.
- comics: Updated the comic list with the generator scripts.
Dosage 1.7 (released 18.12.2012) Dosage 1.7 (released 18.12.2012)
Features: Features:

View file

@ -27,9 +27,16 @@ MaxContentBytes = 1024 * 1024 * 2 # 2 MB
# Maximum content size for images # Maximum content size for images
MaxImageBytes = 1024 * 1024 * 20 # 20 MB MaxImageBytes = 1024 * 1024 * 20 # 20 MB
# Default number of retries
MaxRetries = 3
# Default connection timeout # Default connection timeout
ConnectionTimeoutSecs = 60 ConnectionTimeoutSecs = 60
if hasattr(requests, 'adapters'):
# requests >= 1.0
requests.adapters.DEFAULT_RETRIES = MaxRetries
def tagre(tag, attribute, value, quote='"', before="", after=""): def tagre(tag, attribute, value, quote='"', before="", after=""):
"""Return a regular expression matching the given HTML tag, attribute """Return a regular expression matching the given HTML tag, attribute
and value. It matches the tag and attribute names case insensitive, and value. It matches the tag and attribute names case insensitive,
@ -218,21 +225,25 @@ def get_robotstxt_parser(url):
return rp return rp
def urlopen(url, referrer=None, retries=3, retry_wait_seconds=5, max_content_bytes=None, def urlopen(url, referrer=None, max_content_bytes=None,
timeout=ConnectionTimeoutSecs, session=None, raise_for_status=True): timeout=ConnectionTimeoutSecs, session=None, raise_for_status=True):
"""Open an URL and return the response object.""" """Open an URL and return the response object."""
out.debug('Open URL %s' % url) out.debug('Open URL %s' % url)
assert retries >= 0, 'invalid retry value %r' % retries
assert retry_wait_seconds > 0, 'invalid retry seconds value %r' % retry_wait_seconds
headers = {'User-Agent': UserAgent} headers = {'User-Agent': UserAgent}
if referrer: if referrer:
headers['Referer'] = referrer headers['Referer'] = referrer
config = {"max_retries": retries}
if session is None: if session is None:
session = requests session = requests
kwargs = {
"headers": headers,
"timeout": timeout,
}
if not hasattr(requests, 'adapters'):
# requests << 1.0
kwargs["prefetch"] = False
kwargs["config"] = {"max_retries": MaxRetries}
try: try:
req = session.get(url, headers=headers, config=config, req = session.get(url, **kwargs)
prefetch=False, timeout=timeout)
check_content_size(url, req.headers, max_content_bytes) check_content_size(url, req.headers, max_content_bytes)
if raise_for_status: if raise_for_status:
req.raise_for_status() req.raise_for_status()