diff --git a/hexlib/web.py b/hexlib/web.py index 5c9af8d..76c3e89 100644 --- a/hexlib/web.py +++ b/hexlib/web.py @@ -1,6 +1,10 @@ from http.cookiejar import Cookie +import re from dateutil.parser import parse +import pickle + +from requests.cookies import RequestsCookieJar def cookie_from_string(text: str, domain: str) -> Cookie: @@ -33,3 +37,25 @@ def cookie_from_string(text: str, domain: str) -> Cookie: rest=None, rfc2109=False ) + + +def save_cookiejar(cj, filename): + with open(filename, "wb") as f: + f.truncate() + pickle.dump(cj._cookies, f) + + +def load_cookiejar(filename): + with open(filename, "rb") as f: + cookies = pickle.load(f) + cj = RequestsCookieJar() + cj._cookies = cookies + return cj + + +def cookiejar_filter(cj, pattern): + filtered_cj = RequestsCookieJar() + for c in cj: + if re.match(pattern, c.domain): + filtered_cj.set_cookie(c) + return filtered_cj diff --git a/setup.py b/setup.py index e1e5532..7e8cc46 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name="hexlib", - version="1.4", + version="1.5", description="Misc utility methods", author="simon987", author_email="me@simon987.net",