Debian Codegraph @

recipes/moneycontrol.recipe

raw
1 #!/usr/bin/env python
2 from calibre.web.feeds.news import BasicNewsRecipe, classes
3
4
5 class MoneyControlRecipe(BasicNewsRecipe):
6 title = 'Money Control'
7 __author__ = 'unkn0wn'
8 description = 'Read the latest business news on the Indian economy, global market, upcoming IPOs and more.'
9 language = 'en_IN'
10 masthead_url = 'https://images.moneycontrol.com/images/ftpopup/moneyloginlogo.png'
11 encoding = 'utf-8'
12 no_stylesheets = True
13 remove_javascript = True
14 remove_attributes = ['width', 'height', 'float', 'style']
15
16 ignore_duplicate_articles = {'title', 'url'}
17 remove_empty_feeds = True
18 resolve_internal_links = True
19 oldest_article = 1 # days
20
21 extra_css = '''
22 img {display:block; margin:0 auto;}
23 .article_image_wrapper { font-size:small; text-align:center; }
24 .articlename_join_follow, .author_wrapper, .FT_block_article { font-size:small; color:#404040; }
25 .article_desc { font-style:italic; color:#202020; }
26 '''
27
28 keep_only_tags = [dict(name='div', attrs={'id': lambda x: x and x.startswith('article-')})]
29
30 remove_tags = [
31 dict(name=['svg', 'style', 'button', 'script']),
32 dict(attrs={'id': ['social_icon_impression', 'taboola-mid-article-thumbnails']}),
33 classes(
34 'social_icons_wrapper mid-arti-ad lastPara related_stories_left_block social_icons_mobile_wrapper '
35 'advSlotsWithoutGrayBox tags_wrapper maintextdiv page_right_wrapper stockwidget tech_newsletter'
36 ),
37 ]
38
39 def preprocess_html(self, soup):
40 desc = soup.find(**classes('article_desc'))
41 if desc:
42 desc.name = 'p'
43 for wrap in soup.findAll(**classes('article_image_wrapper')):
44 for h2 in wrap.findAll('h2'):
45 h2.name = 'span'
46 for img in soup.findAll('img', attrs={'data-src': True}):
47 img['src'] = img['data-src']
48 return soup
49
50 def parse_index(self):
51 index = 'https://www.moneycontrol.com/'
52
53 business_sections = [
54 'markets',
55 'stocks',
56 'ipo',
57 'budget',
58 'banks',
59 'moneycontrol-research',
60 'economy',
61 'earnings',
62 'real-estate',
63 'personal-finance',
64 'commodities',
65 'trade',
66 'companies',
67 ]
68
69 news_sections = ['india', 'world', 'opinion', 'politics', 'technology', 'trends', 'lifestyle']
70
71 feeds = []
72 soup = self.index_to_soup(index)
73 for b_sec in business_sections:
74 burl = index + 'news/business/'
75 section = b_sec.capitalize()
76 self.log(section)
77 articles = []
78 for a in soup.findAll('a', attrs={'href': lambda x: x and x.startswith(burl + b_sec + '/')}):
79 url = a['href']
80 if url in {burl + b_sec + '/', burl + b_sec}:
81 continue
82 title = self.tag_to_string(a)
83 self.log('\t', title, '\n\t\t', url)
84 articles.append({'title': title, 'url': url})
85 if articles:
86 feeds.append((section, articles))
87 for n_sec in news_sections:
88 nurl = index + 'news/'
89 nsection = n_sec.capitalize()
90 self.log(nsection)
91 articles = []
92 for a in soup.findAll('a', attrs={'href': lambda x: x and x.startswith(nurl + n_sec + '/')}):
93 url = a['href']
94 if url in {nurl + n_sec + '/', nurl + n_sec}:
95 continue
96 title = self.tag_to_string(a)
97 self.log('\t', title, '\n\t\t', url)
98 articles.append({'title': title, 'url': url})
99 if articles:
100 feeds.append((nsection, articles))
101 return feeds
102
103 def populate_article_metadata(self, article, soup, first):
104 desc = soup.find(**classes('article_desc'))
105 if desc:
106 article.summary = self.tag_to_string(desc)
107 article.text_summary = article.summary