Debian Codegraph @

recipes/hindufeeds.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 from calibre.web.feeds.news import BasicNewsRecipe, classes
4
5
6 class TheHindufeeds(BasicNewsRecipe):
7 title = 'The Hindu News'
8 __author__ = 'unkn0wn'
9 description = 'The Hindu, based on RSS feeds.'
10 language = 'en_IN'
11 no_stylesheets = True
12 masthead_url = 'https://www.thehindu.com/theme/images/th-online/thehindu-logo.svg'
13 remove_attributes = ['style', 'height', 'width']
14 resolve_internal_links = True
15 remove_empty_feeds = True
16 max_articles_per_feed = 25
17 oldest_article = 1.15 # days
18
19 extra_css = '''
20 .caption {font-size:small; text-align:center;}
21 .author, .dateLine, .publish-time {font-size:small; font-weight:bold;}
22 .subhead, .subhead_lead, .bold {font-weight:bold;}
23 .update-publish-time, .publish-time-new {font-size:small; }
24 .italic {font-style:italic; color:#202020;}
25 '''
26
27 recipe_specific_options = {
28 'days': {
29 'short': 'Oldest article to download from this news source. In days ',
30 'long': 'For example, 0.5, gives you articles from the past 12 hours',
31 'default': str(oldest_article),
32 }
33 }
34
35 def __init__(self, *args, **kwargs):
36 BasicNewsRecipe.__init__(self, *args, **kwargs)
37 d = self.recipe_specific_options.get('days')
38 if d and isinstance(d, str):
39 self.oldest_article = float(d)
40
41 ignore_duplicate_articles = {'url'}
42
43 keep_only_tags = [classes('article-section')]
44
45 remove_tags = [
46 dict(name='button'),
47 dict(attrs={'target': '_self'}),
48 classes(
49 'hide-mobile comments-shares share-page editiondetails '
50 'breadcrumb related-stories-inline related-topics related-stories '
51 'also-read premium-label article-ad'
52 ),
53 ]
54
55 def preprocess_html(self, soup):
56 for cap in soup.findAll('p', attrs={'class': 'caption'}):
57 cap.name = 'div'
58 for img in soup.findAll('img', attrs={'data-original': True}):
59 if img['data-original'].endswith('1x1_spacer.png'):
60 source = img.findPrevious('source', srcset=True)
61 img.extract()
62 if source:
63 source['src'] = source['srcset'].replace('_320', '_1200')
64 source.name = 'img'
65 else:
66 img['src'] = img['data-original']
67 for img in soup.findAll('img', attrs={'data-src-template': True}):
68 img['src'] = img['data-src-template']
69 if h2 := soup.find(**classes('sub-title')):
70 h2.name = 'p'
71 h2['class'] = 'italic'
72 return soup
73
74 def postprocess_html(self, soup, first_fetch):
75 for src in soup.findAll('source'):
76 src.extract()
77 return soup
78
79 def get_cover_url(self):
80 soup = self.index_to_soup('https://www.thehindu.com/todays-paper/')
81 if cover := soup.find(attrs={'class': 'hindu-ad'}):
82 return cover.img['src']
83
84 # https://www.thehindu.com/rssfeeds/
85 feeds = [
86 ('Opinion', 'https://www.thehindu.com/opinion/feeder/default.rss'),
87 ('India', 'https://www.thehindu.com/news/national/feeder/default.rss'),
88 ('States', 'https://www.thehindu.com/news/states/feeder/default.rss'),
89 # ('Cities', 'https://www.thehindu.com/news/cities/feeder/default.rss'),
90 ('Business', 'https://www.thehindu.com/business/feeder/default.rss'),
91 ('World', 'https://www.thehindu.com/news/international/feeder/default.rss'),
92 # ('Sport', 'https://www.thehindu.com/sport/feeder/default.rss'),
93 # ('Entertainment', 'https://www.thehindu.com/entertainment/feeder/default.rss'),
94 # ('Crossword', 'https://crossword.thehindu.com/?utm_source=thehindu&utm_medium=mainmenufeeder/default.rss'),
95 ('Science', 'https://www.thehindu.com/sci-tech/science/feeder/default.rss'),
96 ('Life and Style', 'https://www.thehindu.com/life-and-style/feeder/default.rss'),
97 ('thRead', 'https://www.thehindu.com/thread/feeder/default.rss'),
98 ]