Update Hindu BusinessLine Print Edition

This commit is contained in:
Kovid Goyal 2022-11-16 13:08:09 +05:30
parent 414991fa00
commit c3778f5a02
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -1,5 +1,13 @@
import json
import re
from collections import defaultdict
from calibre.web.feeds.news import BasicNewsRecipe, classes from calibre.web.feeds.news import BasicNewsRecipe, classes
import string
def absurl(url):
if url.startswith('/'):
url = 'https://www.thehindubusinessline.com' + url
return url
class BusinessLine(BasicNewsRecipe): class BusinessLine(BasicNewsRecipe):
@ -10,66 +18,70 @@ class BusinessLine(BasicNewsRecipe):
' of business news. BusinessLine reduces the daily grind of business to relevant, readable, byte-sized stories.' ' of business news. BusinessLine reduces the daily grind of business to relevant, readable, byte-sized stories.'
' The newspaper is extensively followed by the decision makers and change leaders from the world of business.' ' The newspaper is extensively followed by the decision makers and change leaders from the world of business.'
) )
no_stylesheets = True
use_embedded_content = False
encoding = 'utf-8'
language = 'en_IN' language = 'en_IN'
remove_attributes = ['height', 'width', 'padding-bottom'] no_stylesheets = True
masthead_url = 'https://www.thehindubusinessline.com/theme/images/bl-online/bllogo.png' masthead_url = 'https://www.thehindubusinessline.com/theme/images/bl-online/bllogo.png'
ignore_duplicate_articles = {'title', 'url'} remove_attributes = ['style', 'height', 'width']
extra_css = '.caption{font-size:small; text-align:center;}'\
'.author{font-size:small; font-weight:bold;}'\
'.subhead{font-weight:bold;}'
def get_cover_url(self): ignore_duplicate_articles = {'url'}
soup = self.index_to_soup(
'https://www.magzter.com/IN/THG-publishing-pvt-ltd/The-Hindu-Business-Line/Newspaper/'
)
for citem in soup.findAll(
'meta', content=lambda s: s and s.endswith('view/3.jpg')
):
return citem['content']
keep_only_tags = [ keep_only_tags = [
classes( classes('articlepage')
'tp-title-inf bi-line leadtext lead-img-caption slide-moadal img-container'
),
dict(
name='div', attrs={'id': lambda x: x and x.startswith('content-body-')}
)
] ]
remove_tags = [ remove_tags = [
classes( classes('hide-mobile comments-shares share-page editiondetails')
'swiper-button-prev left-arrow swiper-button-next right-arrow close cursor tagsBtm share-topic comment-rules vuukle-div paywallbox '
)
] ]
def preprocess_html(self, soup):
for cap in soup.findAll('p', attrs={'class':'caption'}):
cap.name = 'figcaption'
for img in soup.findAll('img', attrs={'data-original':True}):
img['src'] = img['data-original']
return soup
def populate_article_metadata(self, article, soup, first):
if first and hasattr(self, 'add_toc_thumbnail'):
image = soup.find('img')
if image is not None:
self.add_toc_thumbnail(article, image['src'])
def parse_index(self): def parse_index(self):
soup = self.index_to_soup( url = 'https://www.thehindubusinessline.com/todays-paper/'
'https://www.thehindubusinessline.com/todays-paper/' raw = self.index_to_soup(url, raw=True)
) soup = self.index_to_soup(raw)
ans = self.bl_parse_index(soup) ans = self.hindu_parse_index(soup)
if not ans:
raise ValueError(
'The Hindu BusinessLine Newspaper is not published Today.'
)
cover = soup.find(attrs={'class':'hindu-ad'})
if cover:
self.cover_url = cover.img['src']
return ans return ans
def bl_parse_index(self, soup): def hindu_parse_index(self, soup):
feeds = [] for script in soup.findAll('script'):
for section in soup.findAll(**classes('section-container')): if not self.tag_to_string(script).strip().startswith('let grouped_articles = {}'):
div = section.find_previous_sibling('div', **classes('section-header')) continue
a = div.find('a', **classes('section-list-heading')) if script is not None:
secname = string.capwords(self.tag_to_string(a).strip()) art = re.search(r'grouped_articles = ({\"[^<]+?]})', self.tag_to_string(script))
self.log(secname) data = json.loads(art.group(1))
articles = []
for a in section.findAll('a', href=True):
url = a['href']
title = self.tag_to_string(a)
articles.append({'title': title, 'url': url})
self.log('\t', title)
self.log('\t\t', url)
if articles:
feeds.append((secname, articles))
return feeds
def preprocess_html(self, soup): feeds_dict = defaultdict(list)
for image in soup.findAll('source', attrs={'srcset': True}):
image['src'] = image['srcset'] a = json.dumps(data)
for img in soup.findAll('img', attrs={'data-src-template': True}): for sec in json.loads(a):
img['src'] = img['data-src-template'] for item in data[sec]:
return soup section = sec.replace('BL_', '')
title = item['articleheadline']
url = absurl(item['href'])
desc = 'Page no.' + item['pageno'] + ' | ' + item['teaser_text'] or ''
self.log('\t', title, '\n\t\t', url)
feeds_dict[section].append({"title": title, "url": url, "description": desc})
return [(section, articles) for section, articles in feeds_dict.items()]
else:
return []