Debian Codegraph @

recipes/india_today.recipe

raw
1 #!/usr/bin/env python
2 import json
3 import re
4
5 from calibre.web.feeds.news import BasicNewsRecipe
6
7
8 class IndiaToday(BasicNewsRecipe):
9 title = 'India Today Magazine'
10 language = 'en_IN'
11 __author__ = 'unkn0wn'
12 no_stylesheets = True
13 use_embedded_content = False
14 remove_attributes = ['style', 'height', 'width']
15 ignore_duplicate_articles = {'url'}
16 description = (
17 'India’s Most Reputed, Credible and Popular news magazine.'
18 ' Read the most preferred magazine of 9.5 million Indians to access highly researched and unbiased content.'
19 )
20 masthead_url = 'https://akm-img-a-in.tosshub.com/sites/all/themes/itg/logo.png'
21
22 extra_css = '''
23 #sub-h {font-style:italic; color:#202020;}
24 .body_caption, #imgcap, .mos__alt .caption, .caption-drupal-entity, .calibre-nuked-tag-figcaption {font-size:small; text-align:center;}
25 #author, .authors__container {font-size:small;}
26 blockquote {color:#404040;}
27 '''
28
29 remove_tags = [dict(attrs={id: ['tab-link-wrapper-plugin']})]
30
31 recipe_specific_options = {'date': {'short': 'The date of the edition to download (DD-MM-YYYY format)', 'long': 'For example, 22-07-2024'}}
32
33 def get_cover_url(self):
34 d = self.recipe_specific_options.get('date')
35 if not (d and isinstance(d, str)):
36 soup = self.index_to_soup('https://www.readwhere.com/magazine/the-india-today-group/India-Today/1154')
37 for citem in soup.findAll('meta', content=lambda s: s and s.endswith('/magazine/300/new')):
38 return citem['content'].replace('300', '600')
39
40 def parse_index(self):
41 issue = 'https://www.indiatoday.in/magazine'
42 d = self.recipe_specific_options.get('date')
43 if d and isinstance(d, str):
44 issue = issue + '/' + d
45 soup = self.index_to_soup(issue)
46
47 section = None
48 sections = {}
49
50 for tag in soup.findAll('div', attrs={'class': lambda x: x and 'NoCard_story__grid__' in x}):
51 sec = tag.find('div', attrs={'class': lambda x: x and 'NoCard_header__nav__' in x})
52 section = self.tag_to_string(sec).strip()
53 self.log(section)
54 sections[section] = []
55
56 for art in tag.findAll('article'):
57 title = self.tag_to_string(art.find(attrs={'class': lambda x: x and 'NoCard_articletitle__' in x})).strip()
58 url = art.find('a', href=True, title=True)['href']
59 if url.startswith('/'):
60 url = 'https://www.indiatoday.in' + url
61 desc = self.tag_to_string(art.find(attrs={'class': lambda x: x and 'NoCard_story__shortcont__' in x})).strip()
62 self.log('\t', title, '\n\t', desc, '\n\t\t', url)
63 sections[section].append({'title': title, 'url': url, 'description': desc})
64
65 def sort_key(x):
66 section = x[0]
67 try:
68 return ("Editor's Note", 'Cover Story', 'The Big Story', 'Upfront', 'NATION', 'INTERVIEW').index(section)
69 except Exception:
70 return 99999999
71
72 return sorted(sections.items(), key=sort_key)
73
74 def preprocess_html(self, soup):
75 for quo in soup.findAll(attrs={'class': 'quotes'}):
76 quo.name = 'blockquote'
77 return soup
78
79 def preprocess_raw_html(self, raw, *a):
80 m = re.search(r'id="__NEXT_DATA__" type="application/json">', raw)
81 raw = raw[m.start() :]
82 raw = raw.split('>', 1)[1]
83 data = json.JSONDecoder().raw_decode(raw)[0]
84 data = data['props']['pageProps']['initialState']['server']['page_data']
85 title = data['title']
86 body = '<div>' + data['description'] + '</div>'
87
88 slug = desc = image = author = date = imagecap = city = ''
89
90 if 'slug' in data:
91 slug = '<div>' + data['slug'] + '</div>\n'
92 if 'description_short' in data:
93 desc = '<p id="sub-h">' + data['description_short'] + '</p>\n'
94 if data.get('author'):
95 author = ', '.join([names['title'] for names in data['author']])
96 if 'city' in data:
97 city = data['city']
98 if 'datetime_updated' in data:
99 date = data['datetime_updated']
100 if 'image_main' in data:
101 image = '<br/><img src="{}">'.format(data['image_main'])
102 if 'image_caption' in data:
103 imagecap = '<div id="imgcap">' + data['image_caption'] + '</div>'
104
105 return (
106 '<html><body>'
107 + slug
108 + '<h1>'
109 + title
110 + '</h1>\n'
111 + desc
112 + '<div id="author">'
113 + author
114 + '<span> '
115 + city
116 + ' UPDATED: '
117 + date
118 + '</span></div>\n'
119 + image
120 + imagecap
121 + body
122 + '</body></html>'
123 )