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 keep_only_tags = [classes('articlecontent')]
33
34 remove_tags = [
35 dict(name='source'),
36 dict(attrs={'class': lambda cls: cls and 'ad_slot' in cls}),
37 dict(attrs={'class': lambda cls: cls and 'article-header__action-block' in cls}),
38 classes(
39 'article-header__author-social cmp-social-media__nav advt-pos-wrap list-container-wrapper mmtagblock vuuklecomments article-header__author-image'
40 ),
41 ]
42
43 extra_css = '''
44 em, blockquote { color: #202020; }
45 .calibre-nuked-tag-figcaption, .mmtagblock { text-align:center; font-size:small; }
46 .article-header__author-block { font-size:small; }
47 img { display:block; margin:0 auto; }
48 '''
49
50 recipe_specific_options = {
51 'date': {
52 'short': 'Edition date to download (YYYY/MM/DD format)',
53 'long': 'For example, 2026/06/21',
54 }
55 }
56
57 def get_cover_url(self) -> str:
58 # Fetch the cover image URL for the magazine issue
59 edition_date = self.recipe_specific_options.get('date')
60 if not (edition_date and isinstance(edition_date, str)):
61 soup = self.index_to_soup('https://theweekmagazine.in/collections/weekly-magazine-subscription')
62 cover_img = soup.find('img', attrs={'class': 'product-media__image'})
63 return 'https:' + cover_img['src']
64
65 def parse_index(self):
66 # Parse the index page to collect article feeds
67 edition_date = self.recipe_specific_options.get('date')
68 if edition_date and isinstance(edition_date, str):
69 issue_url = f'https://www.theweek.in/theweek/{edition_date}.html'
70 else:
71 archive_soup = self.index_to_soup('https://www.theweek.in/magazine-archives.html')
72 issue_url = build_absolute_url(archive_soup.find('a', **classes('cmp-story-list__image-link'))['href'])
73 self.log(issue_url)
74
75 soup = self.index_to_soup(issue_url)
76 feeds = []
77
78 for list_container in soup.findAll(**classes('listcontainer')):
79 comment_node = list_container.find(string=lambda text: isinstance(text, Comment))
80 if comment_node:
81 match = re.search(r'path:\s*(\S+),', comment_node)
82 if match:
83 list_url = match.group(1)
84 feeds.append(self.parse_section_index(list_url))
85
86 return self.reorder_feeds(feeds)
87
88 def parse_section_index(self, list_url: str):
89 # Parse a section index page and extract articles
90 soup = self.index_to_soup(list_url)
91 for section in soup.findAll(**classes('story-list-block')):
92 section_title = self.tag_to_string(section.find('h3', **classes('cmp-section-title')))
93 self.log(section_title)
94 articles = []
95
96 for item in section.findAll(**classes('cmp-story-list__item')):
97 title_tag = item.find(**classes('cmp-story-list__title-link'))
98 title = self.tag_to_string(title_tag)
99 link_tag = item.find('a', **classes('cmp-story-list__title-link'))
100 url = build_absolute_url(link_tag['href'])
101
102 description = ''
103 if item.find(**classes('cmp-story-list__dispn')):
104 description = self.tag_to_string(item.find(**classes('cmp-story-list__dispn')))
105 elif item.find(**classes('cmp-live-label')):
106 description = self.tag_to_string(item.find(**classes('cmp-live-label')))
107
108 self.log('\t', title, '\n\t', description, '\n\t\t', url)
109 articles.append({'title': title, 'description': description, 'url': url})
110
111 if articles:
112 return (section_title, articles)
113
114 def reorder_feeds(self, feeds):
115 editor_articles = []
116 other_sections = []
117
118 for section_title, articles in feeds:
119 # Split out articles with description "LETTER FROM THE EDITOR"
120 editor = [a for a in articles if a.get('description', '').strip().upper() == 'LETTER FROM THE EDITOR']
121 non_editor = [a for a in articles if a not in editor]
122
123 if editor:
124 editor_articles.extend(editor)
125
126 if non_editor:
127 other_sections.append((section_title.strip(), non_editor))
128
129 # Build new feed list with editor section first
130 if editor_articles:
131 return [('Letter from the Editor', editor_articles)] + other_sections
132 return other_sections
133
134 def preprocess_html(self, soup):
135 # Replace lazy-loaded image sources with actual URLs
136 for img in soup.findAll('img', attrs={'data-websrc': True}):
137 img['src'] = img['data-websrc']
138 return soup