update functions working with cookies.txt files

- rename
  - load_cookiestxt -> cookiestxt_load
  - save_cookiestxt -< cookiestxt_store
- in cookiestxt_load, add cookies directly to a cookie jar
  instead of storing them in a list first
- other unnoticeable performance increases
pull/2584/head
Mike Fährmann 2 years ago
parent f190018e37
commit c4b9f7bab8
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -306,11 +306,10 @@ class Extractor():
cookiefile = util.expand_path(cookies) cookiefile = util.expand_path(cookies)
try: try:
with open(cookiefile) as fp: with open(cookiefile) as fp:
cookies = util.load_cookiestxt(fp) util.cookiestxt_load(fp, self._cookiejar)
except Exception as exc: except Exception as exc:
self.log.warning("cookies: %s", exc) self.log.warning("cookies: %s", exc)
else: else:
self._update_cookies(cookies)
self._cookiefile = cookiefile self._cookiefile = cookiefile
else: else:
self.log.warning( self.log.warning(
@ -322,7 +321,7 @@ class Extractor():
if self._cookiefile and self.config("cookies-update", True): if self._cookiefile and self.config("cookies-update", True):
try: try:
with open(self._cookiefile, "w") as fp: with open(self._cookiefile, "w") as fp:
util.save_cookiestxt(fp, self._cookiejar) util.cookiestxt_store(fp, self._cookiejar)
except OSError as exc: except OSError as exc:
self.log.warning("cookies: %s", exc) self.log.warning("cookies: %s", exc)

@ -302,9 +302,9 @@ def set_mtime(path, mtime):
pass pass
def load_cookiestxt(fp): def cookiestxt_load(fp, cookiejar):
"""Parse a Netscape cookies.txt file and return a list of its Cookies""" """Parse a Netscape cookies.txt file and add its Cookies to 'cookiejar'"""
cookies = [] set_cookie = cookiejar.set_cookie
for line in fp: for line in fp:
@ -321,11 +321,12 @@ def load_cookiestxt(fp):
domain, domain_specified, path, secure, expires, name, value = \ domain, domain_specified, path, secure, expires, name, value = \
line.split("\t") line.split("\t")
if not name: if not name:
name = value name = value
value = None value = None
cookies.append(Cookie( set_cookie(Cookie(
0, name, value, 0, name, value,
None, False, None, False,
domain, domain,
@ -337,12 +338,11 @@ def load_cookiestxt(fp):
False, None, None, {}, False, None, None, {},
)) ))
return cookies
def save_cookiestxt(fp, cookies): def cookiestxt_store(fp, cookies):
"""Write 'cookies' in Netscape cookies.txt format to 'fp'""" """Write 'cookies' in Netscape cookies.txt format to 'fp'"""
fp.write("# Netscape HTTP Cookie File\n\n") write = fp.write
write("# Netscape HTTP Cookie File\n\n")
for cookie in cookies: for cookie in cookies:
if not cookie.domain: if not cookie.domain:
@ -355,15 +355,15 @@ def save_cookiestxt(fp, cookies):
name = cookie.name name = cookie.name
value = cookie.value value = cookie.value
fp.write("\t".join(( write("\t".join((
cookie.domain, cookie.domain,
"TRUE" if cookie.domain.startswith(".") else "FALSE", "TRUE" if cookie.domain.startswith(".") else "FALSE",
cookie.path, cookie.path,
"TRUE" if cookie.secure else "FALSE", "TRUE" if cookie.secure else "FALSE",
"0" if cookie.expires is None else str(cookie.expires), "0" if cookie.expires is None else str(cookie.expires),
name, name,
value, value + "\n",
)) + "\n") )))
def code_to_language(code, default=None): def code_to_language(code, default=None):

@ -168,11 +168,12 @@ class TestISO639_1(unittest.TestCase):
class TestCookiesTxt(unittest.TestCase): class TestCookiesTxt(unittest.TestCase):
def test_load_cookiestxt(self): def test_cookiestxt_load(self):
def _assert(content, expected): def _assert(content, expected):
cookies = util.load_cookiestxt(io.StringIO(content, None)) jar = http.cookiejar.CookieJar()
for c, e in zip(cookies, expected): util.cookiestxt_load(io.StringIO(content, None), jar)
for c, e in zip(jar, expected):
self.assertEqual(c.__dict__, e.__dict__) self.assertEqual(c.__dict__, e.__dict__)
_assert("", []) _assert("", [])
@ -218,13 +219,14 @@ class TestCookiesTxt(unittest.TestCase):
) )
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
util.load_cookiestxt("example.org\tTRUE\t/\tTRUE\t0\tname") util.cookiestxt_load("example.org\tTRUE\t/\tTRUE\t0\tname",
http.cookiejar.CookieJar())
def test_save_cookiestxt(self): def test_cookiestxt_store(self):
def _assert(cookies, expected): def _assert(cookies, expected):
fp = io.StringIO(newline=None) fp = io.StringIO(newline=None)
util.save_cookiestxt(fp, cookies) util.cookiestxt_store(fp, cookies)
self.assertMultiLineEqual(fp.getvalue(), expected) self.assertMultiLineEqual(fp.getvalue(), expected)
_assert([], "# Netscape HTTP Cookie File\n\n") _assert([], "# Netscape HTTP Cookie File\n\n")

Loading…
Cancel
Save