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)