From eb601e992cdf78fa7d6a1bae31fd8dc25f713e61 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 20 Jul 2024 12:17:55 +0530 Subject: [PATCH] Make recipe_specific_options usage compatible with old versions of calibre --- recipes/bloomberg-business-week.recipe | 2 +- recipes/bloomberg.recipe | 7 ++++--- recipes/economist.recipe | 11 ++++++----- recipes/economist_free.recipe | 11 ++++++----- recipes/hindu.recipe | 6 +++--- recipes/psych.recipe | 2 +- recipes/wsj.recipe | 5 +++-- 7 files changed, 24 insertions(+), 20 deletions(-) diff --git a/recipes/bloomberg-business-week.recipe b/recipes/bloomberg-business-week.recipe index 5cf8d4d5b0..f4875b6b47 100644 --- a/recipes/bloomberg-business-week.recipe +++ b/recipes/bloomberg-business-week.recipe @@ -87,7 +87,7 @@ class Bloomberg(BasicNewsRecipe): sec = self.index_to_soup(inx + '/wssmobile/v1/bw/news/list?limit=1', raw=True) id = json.loads(sec)['magazines'][0]['id'] past_edition = self.recipe_specific_options.get('date') - if past_edition: + if past_edition and isinstance(past_edition, str): id = past_edition edit = self.index_to_soup(inx + '/wssmobile/v1/bw/news/week/' + id, raw=True) d = json.loads(edit) diff --git a/recipes/bloomberg.recipe b/recipes/bloomberg.recipe index f34278ab9c..656a9aa9fe 100644 --- a/recipes/bloomberg.recipe +++ b/recipes/bloomberg.recipe @@ -81,14 +81,15 @@ class Bloomberg(BasicNewsRecipe): recipe_specific_options = { 'days': { 'short': 'Oldest article to download from this news source. In days ', - 'long': 'For example, 0.5, gives you articles for the past 12 hours' + 'long': 'For example, 0.5, gives you articles for the past 12 hours', + 'default': str(oldest_article), } } def parse_index(self): d = self.recipe_specific_options.get('days') - if d: - self.oldest_article = d + if d and isinstance(d, str): + self.oldest_article = float(d) inx = 'https://cdn-mobapi.bloomberg.com' sec = self.index_to_soup(inx + '/wssmobile/v1/navigation/bloomberg_app/search-v2', raw=True) sec_data = json.loads(sec)['searchNav'] diff --git a/recipes/economist.recipe b/recipes/economist.recipe index 3ecf3082f5..9139ef5e20 100644 --- a/recipes/economist.recipe +++ b/recipes/economist.recipe @@ -215,7 +215,7 @@ class Economist(BasicNewsRecipe): def publication_date(self): edition_date = self.recipe_specific_options.get('date') - if edition_date: + if edition_date and isinstance(edition_date, str): return parse_only_date(edition_date, as_utc=False) url = self.browser.open("https://www.economist.com/printedition").geturl() return parse_only_date(url.split("/")[-1], as_utc=False) @@ -245,7 +245,7 @@ class Economist(BasicNewsRecipe): 'operationName': 'LatestWeeklyAutoEditionQuery', 'variables': '{"ref":"/content/d06tg8j85rifiq3oo544c6b9j61dno2n"}', } - if edition_date: + if edition_date and isinstance(edition_date, str): url = 'https://www.economist.com/weeklyedition/' + edition_date soup = self.index_to_soup(url) script_tag = soup.find("script", id="__NEXT_DATA__") @@ -268,7 +268,7 @@ class Economist(BasicNewsRecipe): def economist_parse_index(self, raw): edition_date = self.recipe_specific_options.get('date') - if edition_date: + if edition_date and isinstance(edition_date, str): data = json.loads(raw)['data']['section'] else: data = json.loads(raw)['data']['canonical']['hasPart']['parts'][0] @@ -336,7 +336,7 @@ class Economist(BasicNewsRecipe): def parse_index(self): edition_date = self.recipe_specific_options.get('date') # return self.economist_test_article() - if edition_date: + if edition_date and isinstance(edition_date, str): url = 'https://www.economist.com/weeklyedition/' + edition_date self.timefmt = ' [' + edition_date + ']' else: @@ -423,10 +423,11 @@ class Economist(BasicNewsRecipe): x.set('style', 'color:#404040;') raw = etree.tostring(root, encoding='unicode') return raw + def parse_index_from_printedition(self): # return self.economist_test_article() edition_date = self.recipe_specific_options.get('date') - if edition_date: + if edition_date and isinstance(edition_date, str): url = 'https://www.economist.com/weeklyedition/' + edition_date self.timefmt = ' [' + edition_date + ']' else: diff --git a/recipes/economist_free.recipe b/recipes/economist_free.recipe index 3ecf3082f5..9139ef5e20 100644 --- a/recipes/economist_free.recipe +++ b/recipes/economist_free.recipe @@ -215,7 +215,7 @@ class Economist(BasicNewsRecipe): def publication_date(self): edition_date = self.recipe_specific_options.get('date') - if edition_date: + if edition_date and isinstance(edition_date, str): return parse_only_date(edition_date, as_utc=False) url = self.browser.open("https://www.economist.com/printedition").geturl() return parse_only_date(url.split("/")[-1], as_utc=False) @@ -245,7 +245,7 @@ class Economist(BasicNewsRecipe): 'operationName': 'LatestWeeklyAutoEditionQuery', 'variables': '{"ref":"/content/d06tg8j85rifiq3oo544c6b9j61dno2n"}', } - if edition_date: + if edition_date and isinstance(edition_date, str): url = 'https://www.economist.com/weeklyedition/' + edition_date soup = self.index_to_soup(url) script_tag = soup.find("script", id="__NEXT_DATA__") @@ -268,7 +268,7 @@ class Economist(BasicNewsRecipe): def economist_parse_index(self, raw): edition_date = self.recipe_specific_options.get('date') - if edition_date: + if edition_date and isinstance(edition_date, str): data = json.loads(raw)['data']['section'] else: data = json.loads(raw)['data']['canonical']['hasPart']['parts'][0] @@ -336,7 +336,7 @@ class Economist(BasicNewsRecipe): def parse_index(self): edition_date = self.recipe_specific_options.get('date') # return self.economist_test_article() - if edition_date: + if edition_date and isinstance(edition_date, str): url = 'https://www.economist.com/weeklyedition/' + edition_date self.timefmt = ' [' + edition_date + ']' else: @@ -423,10 +423,11 @@ class Economist(BasicNewsRecipe): x.set('style', 'color:#404040;') raw = etree.tostring(root, encoding='unicode') return raw + def parse_index_from_printedition(self): # return self.economist_test_article() edition_date = self.recipe_specific_options.get('date') - if edition_date: + if edition_date and isinstance(edition_date, str): url = 'https://www.economist.com/weeklyedition/' + edition_date self.timefmt = ' [' + edition_date + ']' else: diff --git a/recipes/hindu.recipe b/recipes/hindu.recipe index cb285f743b..4dcd162fc6 100644 --- a/recipes/hindu.recipe +++ b/recipes/hindu.recipe @@ -62,7 +62,7 @@ class TheHindu(BasicNewsRecipe): def parse_index(self): local_edition = self.recipe_specific_options.get('location') - if local_edition: + if local_edition and isinstance(local_edition, str): local_edition = 'th_' + local_edition else: local_edition = 'th_international' @@ -70,8 +70,8 @@ class TheHindu(BasicNewsRecipe): past_edition = self.recipe_specific_options.get('date') dt = date.today() - if past_edition: - year, month, day = (int(x) for x in past_edition.split('-')) + if past_edition and isinstance(past_edition, str): + year, month, day = (int(x) for x in past_edition.split('-')) dt = date(year, month, day) today = dt.strftime('%Y-%m-%d') diff --git a/recipes/psych.recipe b/recipes/psych.recipe index 5cad8e823b..b5353a4f1e 100644 --- a/recipes/psych.recipe +++ b/recipes/psych.recipe @@ -53,7 +53,7 @@ class PsychologyToday(BasicNewsRecipe): a = soup.find(**classes('magazine-thumbnail')).a url = a['href'] past_edition = self.recipe_specific_options.get('date') - if past_edition: + if past_edition and isinstance(past_edition, str): url = '/us/magazine/archive/' + past_edition soup = self.index_to_soup(absurl(url)) cov = soup.find(**classes('content-header--cover-image')) diff --git a/recipes/wsj.recipe b/recipes/wsj.recipe index b8571040dc..35587f971d 100644 --- a/recipes/wsj.recipe +++ b/recipes/wsj.recipe @@ -107,7 +107,8 @@ class WSJ(BasicNewsRecipe): return soup def _download_cover(self): - if not self.recipe_specific_options.get('date'): + d = self.recipe_specific_options.get('date') + if not (d and isinstance(d, str)): import os from contextlib import closing @@ -143,7 +144,7 @@ class WSJ(BasicNewsRecipe): past_edition = self.recipe_specific_options.get('date') for itm in catalog['items']: - if past_edition: + if past_edition and isinstance(past_edition, str): if itm['key'] == 'ITPNEXTGEN' + past_edition: key = itm['key'] manifest = itm['manifest']