From 1c2e89708ffb05cda2013be2fdcdd1c4b8bdc445 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 7 Nov 2014 08:56:35 +0530 Subject: [PATCH] Fix Economist not working from France --- recipes/economist.recipe | 29 ++++++++++++++--------------- recipes/economist_free.recipe | 28 +++++++++++++--------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/recipes/economist.recipe b/recipes/economist.recipe index 9511d828b9..ce9d88ea26 100644 --- a/recipes/economist.recipe +++ b/recipes/economist.recipe @@ -9,7 +9,7 @@ from calibre.web.feeds.news import BasicNewsRecipe from calibre.ebooks.BeautifulSoup import Tag, NavigableString from collections import OrderedDict -import re +import re, cookielib class Economist(BasicNewsRecipe): @@ -58,22 +58,20 @@ class Economist(BasicNewsRecipe): delay = 1 needs_subscription = False - ''' + def get_browser(self): br = BasicNewsRecipe.get_browser(self) - if self.username and self.password: - br.open('http://www.economist.com/user/login') - br.select_form(nr=1) - br['name'] = self.username - br['pass'] = self.password - res = br.submit() - raw = res.read() - if '>Log out<' not in raw: - raise ValueError('Failed to login to economist.com. ' - 'Check your username and password.') + # Add a cookie indicating we have accepted Economist's cookie + # policy (needed when running from some European countries) + ck = cookielib.Cookie( + version=0, name='notice_preferences', value='2:', port=None, + port_specified=False, domain='.economist.com', + domain_specified=False, domain_initial_dot=True, path='/', + path_specified=False, secure=False, expires=None, discard=False, + comment=None, comment_url=None, rest={'HttpOnly': None}, + rfc2109=False) + br.cookiejar.set_cookie(ck) return br - ''' - def parse_index(self): return self.economist_parse_index() @@ -105,7 +103,8 @@ class Economist(BasicNewsRecipe): a = node.find('a', href=True) if a is not None: url = a['href'] - if url.startswith('/'): url = 'http://www.economist.com'+url + if url.startswith('/'): + url = 'http://www.economist.com'+url url += '/print' title = self.tag_to_string(a) if title: diff --git a/recipes/economist_free.recipe b/recipes/economist_free.recipe index 9511d828b9..9dbe5992db 100644 --- a/recipes/economist_free.recipe +++ b/recipes/economist_free.recipe @@ -9,7 +9,7 @@ from calibre.web.feeds.news import BasicNewsRecipe from calibre.ebooks.BeautifulSoup import Tag, NavigableString from collections import OrderedDict -import re +import re, cookielib class Economist(BasicNewsRecipe): @@ -58,22 +58,19 @@ class Economist(BasicNewsRecipe): delay = 1 needs_subscription = False - ''' def get_browser(self): br = BasicNewsRecipe.get_browser(self) - if self.username and self.password: - br.open('http://www.economist.com/user/login') - br.select_form(nr=1) - br['name'] = self.username - br['pass'] = self.password - res = br.submit() - raw = res.read() - if '>Log out<' not in raw: - raise ValueError('Failed to login to economist.com. ' - 'Check your username and password.') + # Add a cookie indicating we have accepted Economist's cookie + # policy (needed when running from some European countries) + ck = cookielib.Cookie( + version=0, name='notice_preferences', value='2:', port=None, + port_specified=False, domain='.economist.com', + domain_specified=False, domain_initial_dot=True, path='/', + path_specified=False, secure=False, expires=None, discard=False, + comment=None, comment_url=None, rest={'HttpOnly': None}, + rfc2109=False) + br.cookiejar.set_cookie(ck) return br - ''' - def parse_index(self): return self.economist_parse_index() @@ -105,7 +102,8 @@ class Economist(BasicNewsRecipe): a = node.find('a', href=True) if a is not None: url = a['href'] - if url.startswith('/'): url = 'http://www.economist.com'+url + if url.startswith('/'): + url = 'http://www.economist.com'+url url += '/print' title = self.tag_to_string(a) if title: