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.
This commit is contained in:
parent
95e71e329b
commit
6acfd75780
2 changed files with 46 additions and 4 deletions
|
|
@ -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.
|
||||
|
|
|
|||
36
weboob/tools/browser2/cookies.py
Normal file
36
weboob/tools/browser2/cookies.py
Normal file
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue