Debian Codegraph @

recipes/the_week.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 import re
4
5 from calibre.ebooks.BeautifulSoup import Comment
6 from calibre.web.feeds.news import BasicNewsRecipe, classes
7
8
9 def build_absolute_url(url: str) -> str:
10 # Ensure relative URLs are converted to absolute URLs for The Week site
11 if url.startswith('/'):
12 return 'https://www.theweek.in' + url
13 return url
14
15
16 class TheWeekRecipe(BasicNewsRecipe):
17 title = 'The Week'
18 description = (
19 'The Week is a best-selling general interest English news magazine. '
20 'It covers politics, entertainment, social issues, trends, technology, '
21 'lifestyle, and more. Best downloaded on Mondays.'
22 )
23 language = 'en_IN'
24 __author__ = 'unkn0wn'
25 encoding = 'utf-8'
26 no_stylesheets = True
27 use_embedded_content = False
28 ignore_duplicate_articles = {'url', 'title'}
29 remove_attributes = ['style', 'height', 'width']
30 masthead_url = 'https://www.theweek.in/content/dam/revamp-week/logo/THE-WEEK-LOGO.jpg'
31
32 browser_type = 'webengine'
33
34 keep_only_tags = [classes('articlecontent')]
35
36 remove_tags = [
37 dict(name='source'),
38 dict(attrs={'class': lambda cls: cls and 'ad_slot' in cls}),
39 dict(attrs={'class': lambda cls: cls and 'article-header__action-block' in cls}),
40 classes(
41 'article-header__author-social cmp-social-media__nav advt-pos-wrap list-container-wrapper mmtagblock vuuklecomments article-header__author-image'
42 ),
43 ]
44
45 extra_css = '''
46 em, blockquote { color: #202020; }
47 .calibre-nuked-tag-figcaption, .mmtagblock { text-align:center; font-size:small; }
48 .article-header__author-block { font-size:small; }
49 img { display:block; margin:0 auto; }
50 '''
51
52 recipe_specific_options = {
53 'date': {
54 'short': 'Edition date to download (YYYY/MM/DD format)',
55 'long': 'For example, 2026/06/21',
56 }
57 }
58
59 def get_cover_url(self) -> str:
60 # Fetch the cover image URL for the magazine issue
61 edition_date = self.recipe_specific_options.get('date')
62 if not (edition_date and isinstance(edition_date, str)):
63 soup = self.index_to_soup('https://theweekmagazine.in/collections/weekly-magazine-subscription')
64 cover_img = soup.find('img', attrs={'class': 'product-media__image'})
65 return 'https:' + cover_img['src']
66
67 def parse_index(self):
68 # Parse the index page to collect article feeds
69 edition_date = self.recipe_specific_options.get('date')
70 if edition_date and isinstance(edition_date, str):
71 issue_url = f'https://www.theweek.in/theweek/{edition_date}.html'
72 else:
73 archive_soup = self.index_to_soup('https://www.theweek.in/magazine-archives.html')
74 arch = archive_soup.find('a', attrs={'class': 'cmp-story-list__image-link'})
75 issue_url = build_absolute_url(arch['href'])
76 self.timefmt = ' [' + arch['title'] + ']'
77
78 soup = self.index_to_soup(issue_url)
79 feeds = []
80
81 for list_container in soup.findAll(**classes('listcontainer')):
82 comment_node = list_container.find(string=lambda text: isinstance(text, Comment))
83 if comment_node:
84 match = re.search(r'path:\s*(\S+),', comment_node)
85 if match:
86 list_url = match.group(1)
87 feeds.append(self.parse_section_index(list_url))
88
89 return self.reorder_feeds(feeds)
90
91 def parse_section_index(self, list_url: str):
92 # Parse a section index page and extract articles
93 soup = self.index_to_soup(list_url)
94 for section in soup.findAll(**classes('story-list-block')):
95 section_title = self.tag_to_string(section.find('h3', **classes('cmp-section-title')))
96 self.log(section_title)
97 articles = []
98
99 for item in section.findAll(**classes('cmp-story-list__item')):
100 title_tag = item.find(**classes('cmp-story-list__title-link'))
101 title = self.tag_to_string(title_tag)
102 link_tag = item.find('a', **classes('cmp-story-list__title-link'))
103 url = build_absolute_url(link_tag['href'])
104
105 description = ''
106 if item.find(**classes('cmp-story-list__dispn')):
107 description = self.tag_to_string(item.find(**classes('cmp-story-list__dispn')))
108 elif item.find(**classes('cmp-live-label')):
109 description = self.tag_to_string(item.find(**classes('cmp-live-label')))
110
111 self.log('\t', title, '\n\t', description, '\n\t\t', url)
112 articles.append({'title': title, 'description': description, 'url': url})
113
114 if articles:
115 return (section_title, articles)
116
117 def reorder_feeds(self, feeds):
118 editor_articles = []
119 other_sections = []
120
121 for section_title, articles in feeds:
122 # Split out articles with description "LETTER FROM THE EDITOR"
123 editor = [a for a in articles if a.get('description', '').strip().upper() == 'LETTER FROM THE EDITOR']
124 non_editor = [a for a in articles if a not in editor]
125
126 if editor:
127 editor_articles.extend(editor)
128
129 if non_editor:
130 other_sections.append((section_title.strip(), non_editor))
131
132 # Build new feed list with editor section first
133 if editor_articles:
134 return [('Letter from the Editor', editor_articles)] + other_sections
135 return other_sections
136
137 def preprocess_html(self, soup):
138 # Replace lazy-loaded image sources with actual URLs
139 for img in soup.findAll('img', attrs={'data-websrc': True}):
140 img['src'] = img['data-websrc']
141 return soup