summaryrefslogtreecommitdiffstats
path: root/gallery_dl/extractor/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'gallery_dl/extractor/common.py')
-rw-r--r--gallery_dl/extractor/common.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/gallery_dl/extractor/common.py b/gallery_dl/extractor/common.py
index 3a282c2..dd685df 100644
--- a/gallery_dl/extractor/common.py
+++ b/gallery_dl/extractor/common.py
@@ -40,6 +40,7 @@ class Extractor():
self._cookiefile = None
self._cookiejar = self.session.cookies
self._parentdir = ""
+ self._write_pages = self.config("write-pages", False)
self._retries = self.config("retries", 4)
self._timeout = self.config("timeout", 30)
self._verify = self.config("verify", True)
@@ -91,6 +92,8 @@ class Extractor():
raise exception.HttpError(exc)
else:
code = response.status_code
+ if self._write_pages:
+ self._dump_response(response)
if 200 <= code < 400 or fatal is None and \
(400 <= code < 500) or not fatal and \
(400 <= code < 429 or 431 <= code < 500):
@@ -325,6 +328,33 @@ class Extractor():
test = (test, None)
yield test
+ def _dump_response(self, response):
+ """Write the response content to a .dump file in the current directory.
+
+ The file name is derived from the response url,
+ replacing special characters with "_"
+ """
+ for resp in response.history:
+ self._dump_response(resp)
+
+ if hasattr(Extractor, "_dump_index"):
+ Extractor._dump_index += 1
+ else:
+ Extractor._dump_index = 1
+ Extractor._dump_sanitize = re.compile(r"[\\\\|/<>:\"?*&=#]+").sub
+
+ fname = "{:>02}_{}".format(
+ Extractor._dump_index,
+ Extractor._dump_sanitize('_', response.url)
+ )[:250]
+
+ try:
+ with open(fname + ".dump", 'wb') as fp:
+ util.dump_response(response, fp)
+ except Exception as e:
+ self.log.warning("Failed to dump HTTP request (%s: %s)",
+ e.__class__.__name__, e)
+
class GalleryExtractor(Extractor):
@@ -460,7 +490,7 @@ class SharedConfigMixin():
"""Enable sharing of config settings based on 'basecategory'"""
basecategory = ""
- def config(self, key, default=None, *, sentinel=object()):
+ def config(self, key, default=None, *, sentinel=util.SENTINEL):
value = Extractor.config(self, key, sentinel)
return value if value is not sentinel else config.interpolate(
("extractor", self.basecategory, self.subcategory), key, default)