Debian Codegraph @

recipes/theeconomictimes_india_print_edition.recipe

raw
1 """
2 economictimes.indiatimes.com
3 """
4
5 import re
6
7 from calibre.web.feeds.news import BasicNewsRecipe, classes
8
9
10 class TheEconomicTimes(BasicNewsRecipe):
11 title = 'The Economic Times | Print Edition'
12 __author__ = 'unkn0wn'
13 description = 'Financial news from India.'
14 publisher = 'economictimes.indiatimes.com'
15 category = 'news, finances, politics, India'
16 no_stylesheets = True
17 use_embedded_content = False
18 encoding = 'utf-8'
19 language = 'en_IN'
20 remove_attributes = ['style', 'height', 'width']
21 publication_type = 'newspaper'
22 masthead_url = 'https://upload.wikimedia.org/wikipedia/commons/9/98/The_Economic_Times_logo.svg'
23 ignore_duplicate_articles = {'title', 'url'}
24 extra_css = '''
25 .artByline {font-size:small;}
26 .artImg, .imgBox {font-size:small; color:#202020; text-align:center;}
27 img {display:block; margin:0 auto;}
28 '''
29
30 def get_cover_url(self):
31 from datetime import date
32
33 yr = str(date.today().year)
34 mn = date.today().strftime('%m')
35 dy = date.today().strftime('%d')
36 cover = 'https://asset.harnscloud.com/PublicationData/ET/etbg/' + yr + '/' + mn + '/' + dy + '/Page/' + dy + '_' + mn + '_' + yr + '_001_etbg.jpg'
37 self.log('cover_url ', cover)
38 br = BasicNewsRecipe.get_browser(self)
39 try:
40 br.open(cover)
41 except Exception:
42 self.log('\nCover unavailable')
43 cover = None
44 return cover
45
46 keep_only_tags = [
47 dict(name='h1'),
48 classes('artByline pageContent'),
49 ]
50 remove_tags = [
51 dict(name='button'),
52 classes(
53 'story_title storyCollection shareBar sr_widget_free jsSrWidgetFree srwidgetfree_3 showWhatsMsg artSyn'
54 ' sr_paid jsSrWidgetPaid ar_wrp arwd_ld_chk adBox custom_ad mgid orn_free_r bold primeSWrapper external_widget'
55 ),
56 ]
57
58 def parse_index(self):
59 self.log('\n***\nif this recipe fails, report it on: https://www.mobileread.com/forums/forumdisplay.php?f=228\n***\n')
60 soup = self.index_to_soup('https://economictimes.indiatimes.com/print_edition.cms')
61 date = soup.find(**classes('labelDate'))
62 if date:
63 self.timefmt = ' [' + self.tag_to_string(date).strip() + ']'
64 self.log(self.timefmt)
65 ans = self.et_parse_index(soup)
66 return ans
67
68 def et_parse_index(self, soup):
69 feeds = []
70 for section in soup.findAll('div', attrs={'class': 'printBox'}):
71 h2 = section.find('h2')
72 sec = self.tag_to_string(h2)
73 secname = re.sub(r'[0-9]', '', sec)
74 self.log(secname)
75 articles = []
76 for h3 in section.findAll(('h1', 'h3', 'h4', 'h5')):
77 span = h3.find('span', href=lambda x: x and x.startswith('https://economictimes.indiatimes.com/epaper/'), attrs={'class': 'banner'})
78 url = span['href']
79 title = self.tag_to_string(span)
80 div = h3.find_next_sibling('div', attrs={'class': 'dsc'})
81 if div is not None:
82 desc = self.tag_to_string(div)
83 articles.append({'title': title, 'url': url, 'description': desc})
84 self.log('\t', title)
85 self.log('\t', desc)
86 self.log('\t\t', url)
87 if articles:
88 feeds.append((secname, articles))
89 return feeds
90
91 def preprocess_html(self, soup):
92 for image in soup.findAll('img', attrs={'src': True}):
93 image['src'] = image['src'].replace('width-300', 'width-640')
94 for img in soup.findAll('img', attrs={'data-original': True}):
95 img['src'] = img['data-original']
96 return soup