1#!/usr/local/bin/python3.8 2# vim:fileencoding=utf-8 3# License: GPLv3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net> 4 5from __future__ import absolute_import, division, print_function, unicode_literals 6 7import re 8 9from calibre.web.feeds.news import BasicNewsRecipe 10 11 12def classes(classes): 13 q = frozenset(classes.split(' ')) 14 return dict(attrs={ 15 'class': lambda x: x and frozenset(x.split()).intersection(q)}) 16 17 18class TheHindu(BasicNewsRecipe): 19 title = u'The Business Line' 20 language = 'en_IN' 21 22 oldest_article = 7 23 __author__ = 'Dhiru' 24 max_articles_per_feed = 100 25 no_stylesheets = True 26 27 keep_only_tags = [ 28 dict(name='h1'), 29 classes('textbyline article-image contentbody'), 30 ] 31 remove_tags = [ 32 ] 33 34 extra_css = '.photo-caption { font-size: smaller }' 35 36 def preprocess_html(self, soup, *a): 37 for img in soup.findAll(attrs={'data-proxy-image': True}): 38 img['src'] = re.sub(r'/alternates/[^/]+', '/alternates/LANDSCAPE_730', img['data-proxy-image'], flags=re.I) 39 return soup 40 41 def parse_index(self): 42 soup = self.index_to_soup( 43 'https://www.thehindubusinessline.com/todays-paper/tp-index') 44 div = soup.find(attrs={'class': 'left-column'}) 45 feeds = [] 46 current_section = None 47 current_articles = [] 48 for x in div.findAll(['h2', 'li']): 49 if current_section and x.name == 'li': 50 a = x.find('a', href=True) 51 if a is not None: 52 title = self.tag_to_string(a) 53 current_articles.append({'url': a['href'], 'title': title, 'date': '', 'description': ''}) 54 self.log('\t' + title) 55 if x.name == 'h2': 56 if current_section and current_articles: 57 feeds.append((current_section, current_articles)) 58 current_section = self.tag_to_string(x).strip().capitalize() 59 self.log(current_section) 60 current_articles = [] 61 return feeds 62