improve util.load/save_cookiestxt() and add tests

- take a file object as argument instead of an filename
- accept whitespace before comments ("   # comment")
- map expiration "0" to None and not the number 0
This commit is contained in:
Mike Fährmann
2020-01-25 22:57:08 +01:00
parent e35c2ea1a6
commit 2a9be48511
3 changed files with 148 additions and 51 deletions

View File

@@ -197,7 +197,8 @@ class Extractor():
elif isinstance(cookies, str):
cookiefile = util.expand_path(cookies)
try:
cookies = util.load_cookiestxt(cookiefile)
with open(cookiefile) as fp:
cookies = util.load_cookiestxt(fp)
except Exception as exc:
self.log.warning("cookies: %s", exc)
else:
@@ -217,7 +218,8 @@ class Extractor():
"""Store the session's cookiejar in a cookies.txt file"""
if self._cookiefile and self.config("cookies-update", True):
try:
util.save_cookiestxt(self._cookiefile, self._cookiejar)
with open(self._cookiefile, "w") as fp:
util.save_cookiestxt(fp, self._cookiejar)
except OSError as exc:
self.log.warning("cookies: %s", exc)