Debian Codegraph @

recipes/theindiaforum.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 from datetime import datetime, timedelta, timezone
4
5 from calibre.utils.date import parse_date
6 from calibre.web.feeds.news import BasicNewsRecipe, classes
7
8
9 class mains(BasicNewsRecipe):
10 title = 'The India Forum'
11 __author__ = 'unkn0wn'
12 description = 'The India Forum is an independent online journal-magazine that seeks to widen and deepen our conversations on the issues that concern us.'
13 language = 'en_IN'
14 encoding = 'utf-8'
15 ignore_duplicate_articles = {'url'}
16 remove_attributes = ['height', 'width', 'style']
17 no_stylesheets = True
18 resolve_internal_links = True
19 remove_empty_feeds = True
20 use_embedded_content = False
21 oldest_article = 30 # days
22 masthead_url = 'https://www.theindiaforum.in/themes/the_india_forum/images/tif_logo.png'
23 extra_css = '''
24 [class*="caption"], [class*="references"], #article-author-top-container { font-size:small; }
25 [class*="blurb"] { font-style:italic; }
26 blockquote, em { color:#202020; }
27 '''
28
29 keep_only_tags = [
30 classes('article-lead-container block-views-blockarticle-block-1'),
31 dict(name='section', attrs={'id': 'article-author-top-container'}),
32 classes('block-field-blocknodearticlebody block-field-blocknodearticlefield-references'),
33 ]
34
35 remove_tags = [dict(name=['source', 'svg']), dict(attrs={'src': lambda x: x and x.endswith('quote_logo.png')}), classes('s_info')]
36
37 def parse_index(self):
38 soup = self.index_to_soup('https://www.theindiaforum.in/')
39 ul = soup.find('ul', attrs={'class': 'float-left'})
40
41 section_list = []
42
43 for x in ul.findAll('a', href=True):
44 if '/podcast' in x['href']:
45 continue
46 section_list.append((self.tag_to_string(x).strip().replace('■', '■ '), 'https://www.theindiaforum.in' + x['href']))
47
48 feeds = []
49
50 for section in section_list:
51 section_title = section[0]
52 section_url = section[1]
53 self.log(section_title, section_url)
54 soup = self.index_to_soup(section_url)
55 articles = self.articles_from_soup(soup)
56 if articles:
57 feeds.append((section_title, articles))
58 return feeds
59
60 def articles_from_soup(self, soup):
61 ans = []
62 for art in soup.findAll('div', attrs={'class': lambda x: x and 'views-col' in x.split()}):
63 h2 = art.find(['h2', 'h3'])
64 url = 'https://www.theindiaforum.in' + h2.a['href']
65 title = self.tag_to_string(h2).strip()
66 desc = ''
67 if summ := art.find(**classes('summary')):
68 desc = self.tag_to_string(summ)
69 if inline := summ.find(**classes('inline-date')):
70 date = parse_date(self.tag_to_string(inline))
71 today = (datetime.now(timezone.utc)).replace(microsecond=0)
72 if (today - date) > timedelta(self.oldest_article):
73 continue
74 self.log('\t', title, '\n\t', desc, '\n\t\t', url)
75 ans.append({'title': title, 'url': url, 'description': desc})
76 return ans