From 6acfd75780fad59f0bcd50c7afad4aeec97fa27e Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Fri, 14 Mar 2014 01:22:00 +0100 Subject: [PATCH] Save cookies on save_responses and pave the way for more good stuff We apparently can't override the class in the Response, even if it is changed in the Session. Still, it will be useful to have our own class. --- weboob/tools/browser2/browser.py | 14 +++++++++---- weboob/tools/browser2/cookies.py | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 weboob/tools/browser2/cookies.py diff --git a/weboob/tools/browser2/browser.py b/weboob/tools/browser2/browser.py index 2a02c3a9..7cc412b3 100644 --- a/weboob/tools/browser2/browser.py +++ b/weboob/tools/browser2/browser.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright(C) 2012 Laurent Bachelier +# Copyright(C) 2012-2014 Laurent Bachelier # # This file is part of weboob. # @@ -36,8 +36,7 @@ except ImportError: from weboob.tools.log import getLogger - -# TODO define __all__ +from .cookies import WeboobCookieJar class Profile(object): @@ -137,7 +136,7 @@ class BaseBrowser(object): self.response = None self.responses_dirname = responses_dirname - self.responses_count = 0 + self.responses_count = 1 def _save(self, response, warning=False, **kwargs): if self.responses_dirname is None: @@ -162,8 +161,12 @@ class BaseBrowser(object): (self.responses_count, response.status_code, '-' if path else '', path, ext) response_filepath = os.path.join(self.responses_dirname, filename) + with open(response_filepath, 'w') as f: f.write(response.content) + if response.cookies and len(response.cookies): + WeboobCookieJar.from_cookiejar(response.cookies).export(response_filepath + '-cookies.txt') + match_filepath = os.path.join(self.responses_dirname, 'url_response_match.txt') with open(match_filepath, 'a') as f: f.write('# %d %s %s\n' % (response.status_code, response.reason, response.headers.get('Content-Type', ''))) @@ -195,6 +198,9 @@ class BaseBrowser(object): self.session = session + cj = WeboobCookieJar() + session.cookies = cj + def location(self, url, **kwargs): """ Like open() but also changes the current URL and response. diff --git a/weboob/tools/browser2/cookies.py b/weboob/tools/browser2/cookies.py new file mode 100644 index 00000000..26bb2ead --- /dev/null +++ b/weboob/tools/browser2/cookies.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# Copyright(C) 2014 Laurent Bachelier +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + +import requests.cookies +import cookielib + + +class WeboobCookieJar(requests.cookies.RequestsCookieJar): + @classmethod + def from_cookiejar(klass, cj): + """ + Create a WeboobCookieJar from another CookieJar instance. + """ + return requests.cookies.merge_cookies(klass(), cj) + + def export(self, filename): + """ + Export all cookies to a file, regardless of expiration, etc. + """ + cj = requests.cookies.merge_cookies(cookielib.LWPCookieJar(), self) + cj.save(filename, ignore_discard=True, ignore_expires=True)