Skip to content

Commit

Permalink
Handle all cookies with a float value of max-age
Browse files Browse the repository at this point in the history
  • Loading branch information
elrob committed Nov 16, 2019
1 parent 9a695c8 commit 10600fd
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions safaribooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import requests
import traceback
import re
from http.cookies import SimpleCookie
from lxml import html, etree
from html import escape
from random import random
Expand Down Expand Up @@ -301,6 +302,8 @@ class SafariBooks:
"<navMap>{4}</navMap>\n" \
"</ncx>"

COOKIE_FLOAT_MAX_AGE_PATTERN = re.compile(r'(max-age=\d*)\.\d*', re.IGNORECASE)

def __init__(self, args):
self.args = args
self.display = Display("info_%s.log" % escape(args.bookid))
Expand Down Expand Up @@ -410,14 +413,11 @@ def return_headers(self, url):

return self.HEADERS

def update_cookies(self, jar, set_cookie_header):
for cookie in jar:
self.cookies.update({
cookie.name: cookie.value
})
orm_rt_cookie_search_result = re.search(r'orm-rt=(\S*)', set_cookie_header)
if orm_rt_cookie_search_result:
self.cookies["orm-rt"] = orm_rt_cookie_search_result.group(1)
def update_cookies(self, set_cookie_headers):
for morsel in set_cookie_headers:
morsel_without_float_max_age = self.COOKIE_FLOAT_MAX_AGE_PATTERN.sub(r'\1', morsel)
for name, parsed_morsel in SimpleCookie(morsel_without_float_max_age).items():
self.cookies[name] = parsed_morsel.value

def requests_provider(
self, url, post=False, data=None, perfom_redirect=True, update_cookies=True, update_referer=True, **kwargs
Expand All @@ -442,7 +442,7 @@ def requests_provider(
return 0

if update_cookies:
self.update_cookies(response.cookies, response.headers.get("Set-Cookie", ""))
self.update_cookies(response.raw.headers.getlist("Set-Cookie"))

if update_referer:
# TODO Update Referer HTTP Header
Expand Down

0 comments on commit 10600fd

Please sign in to comment.