From 7f25fcabf75a4c7e52b374d3ab15e1b077936d9a Mon Sep 17 00:00:00 2001 From: unkn0w7n <51942695+unkn0w7n@users.noreply.github.com> Date: Fri, 10 May 2024 10:52:14 +0530 Subject: [PATCH] Update Economist use local issue cover --- recipes/economist.recipe | 17 ++++++++++++----- recipes/economist_free.recipe | 17 ++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/recipes/economist.recipe b/recipes/economist.recipe index 5e62e3e04f..f4f2e644b4 100644 --- a/recipes/economist.recipe +++ b/recipes/economist.recipe @@ -252,7 +252,10 @@ class Economist(BasicNewsRecipe): 'variables': '{{"path":"/content/{}"}}'.format(content_id), } url = 'https://cp2-graphql-gateway.p.aws.economist.com/graphql?' + urlencode(query, safe='()!', quote_via=quote) - raw = self.index_to_soup(url, raw=True) + try: + raw = self.index_to_soup(url, raw=True) + except Exception: + raise ValueError('Server is not reachable, try again after some time.') ans = self.economist_parse_index(raw) return self.economist_return_index(ans) @@ -261,13 +264,17 @@ class Economist(BasicNewsRecipe): data = json.loads(raw)['data']['section'] else: data = json.loads(raw)['data']['canonical']['hasPart']['parts'][0] - self.description = data['image']['cover'][0]['headline'] dt = datetime.fromisoformat(data['datePublished'][:-1]) + timedelta(seconds=time.timezone) dt = dt.strftime('%b %d, %Y') self.timefmt = ' [' + dt + ']' - self.cover_url = data['image']['cover'][0]['url']['canonical'].replace('economist.com/', - 'economist.com/cdn-cgi/image/width=960,quality=80,format=auto/') - self.log('Got cover:', self.cover_url) + # get local issue cover, title + region = json.loads(self.index_to_soup('https://geolocation-db.com/json', raw=True))['country_code'] + for cov in data['image']['cover']: + if region in cov['regionsAllowed']: + self.description = cov['headline'] + self.cover_url = cov['url']['canonical'].replace('economist.com/', + 'economist.com/cdn-cgi/image/width=960,quality=80,format=auto/') + self.log('Got cover:', self.cover_url, '\n', self.description) feeds_dict = defaultdict(list) for part in safe_dict(data, "hasPart", "parts"): diff --git a/recipes/economist_free.recipe b/recipes/economist_free.recipe index 5e62e3e04f..f4f2e644b4 100644 --- a/recipes/economist_free.recipe +++ b/recipes/economist_free.recipe @@ -252,7 +252,10 @@ class Economist(BasicNewsRecipe): 'variables': '{{"path":"/content/{}"}}'.format(content_id), } url = 'https://cp2-graphql-gateway.p.aws.economist.com/graphql?' + urlencode(query, safe='()!', quote_via=quote) - raw = self.index_to_soup(url, raw=True) + try: + raw = self.index_to_soup(url, raw=True) + except Exception: + raise ValueError('Server is not reachable, try again after some time.') ans = self.economist_parse_index(raw) return self.economist_return_index(ans) @@ -261,13 +264,17 @@ class Economist(BasicNewsRecipe): data = json.loads(raw)['data']['section'] else: data = json.loads(raw)['data']['canonical']['hasPart']['parts'][0] - self.description = data['image']['cover'][0]['headline'] dt = datetime.fromisoformat(data['datePublished'][:-1]) + timedelta(seconds=time.timezone) dt = dt.strftime('%b %d, %Y') self.timefmt = ' [' + dt + ']' - self.cover_url = data['image']['cover'][0]['url']['canonical'].replace('economist.com/', - 'economist.com/cdn-cgi/image/width=960,quality=80,format=auto/') - self.log('Got cover:', self.cover_url) + # get local issue cover, title + region = json.loads(self.index_to_soup('https://geolocation-db.com/json', raw=True))['country_code'] + for cov in data['image']['cover']: + if region in cov['regionsAllowed']: + self.description = cov['headline'] + self.cover_url = cov['url']['canonical'].replace('economist.com/', + 'economist.com/cdn-cgi/image/width=960,quality=80,format=auto/') + self.log('Got cover:', self.cover_url, '\n', self.description) feeds_dict = defaultdict(list) for part in safe_dict(data, "hasPart", "parts"):