calibre/recipes/business_standard.recipe
2024-01-03 11:10:50 +05:30

119 lines
4.9 KiB
Plaintext

from calibre.web.feeds.news import BasicNewsRecipe
from calibre.ptempfile import PersistentTemporaryFile
from html5_parser import parse
from datetime import datetime
import json
class BusinessStandard(BasicNewsRecipe):
title = 'Business Standard'
__author__ = 'unkn0wn'
description = "India's most respected business daily"
language = 'en_IN'
masthead_url = 'https://bsmedia.business-standard.com/include/_mod/site/html5/images/business-standard-logo.png'
no_stylesheets = True
remove_javascript = True
remove_attributes = ['width', 'height', 'float', 'style']
def get_cover_url(self):
soup = self.index_to_soup('https://www.magzter.com/IN/Business-Standard-Private-Ltd/Business-Standard/Newspaper/')
for citem in soup.findAll('meta', content=lambda s: s and s.endswith('view/3.jpg')):
return citem['content']
def get_browser(self):
return BasicNewsRecipe.get_browser(self, user_agent='common_words/based')
ignore_duplicate_articles = {'title', 'url'}
remove_empty_feeds = True
resolve_internal_links = True
max_articles_per_feed = 20
extra_css = '''
img {display:block; margin:0 auto;}
.auth, .cat { font-size:small; color:#202020; }
.cap { font-size:small; text-align:center; }
'''
articles_are_obfuscated = True
def get_obfuscated_article(self, url):
br = self.get_browser()
soup = self.index_to_soup(url)
link = soup.find('a', attrs={'href':lambda x: x and x.startswith('https://www.business-standard.com')})
skip_sections =[ # add sections you want to skip
'/video/', '/videos/', '/multimedia/',
]
if any(x in link['href'] for x in skip_sections):
self.abort_article('skipping video links ', link['href'])
self.log('Found ', link['href'])
html = br.open(link['href']).read()
pt = PersistentTemporaryFile('.html')
pt.write(html)
pt.close()
return pt.name
feeds = []
sections = [
'india-news', 'economy', 'opinion', 'markets', 'companies', 'industry', 'finance', 'world-news',
# 'politics', 'cricket', 'sports', 'technology', 'book', 'education', 'specials'
]
for sec in sections:
a = 'https://news.google.com/rss/search?q=when:27h+allinurl:business-standard.com{}&hl=en-IN&gl=IN&ceid=IN:en'
feeds.append((sec.capitalize(), a.format('%2F' + sec + '%2F')))
# feeds.append(('Others', a.format('')))
def preprocess_raw_html(self, raw, *a):
root = parse(raw)
m = root.xpath('//script[@id="__NEXT_DATA__"]')
data = json.loads(m[0].text)
img_url = None
if 'articleImageUrl' in data['props']['pageProps']['articleSchema']:
img_url = data['props']['pageProps']['articleSchema']['articleImageUrl']
art_url = 'https://www.business-standard.com' + data['props']['pageProps']['url']
data = data['props']['pageProps']['data']
title = '<h1 title="{}">'.format(art_url) + data['pageTitle'] + '</h1>'
cat = subhead = lede = auth = caption = ''
if 'defaultArticleCat' in data and data['defaultArticleCat'] is not None:
if 'h1_tag' in data['defaultArticleCat'] and data['defaultArticleCat']['h1_tag'] is not None:
cat = '<div><p class="cat">' + data['defaultArticleCat']['h1_tag'] + '</p></div>'
if 'metaDescription' in data and data['metaDescription'] is not None:
subhead = '<h3>' + data['metaDescription'] + '</h3>'
self.art_desc = data['metaDescription']
date = (datetime.fromtimestamp(int(data['publishDate']))).strftime('%b %d, %Y | %I:%M %p')
authors = []
if 'articleMappedMultipleAuthors' in data:
for aut in data['articleMappedMultipleAuthors']:
authors.append(data['articleMappedMultipleAuthors'][str(aut)])
auth = '<div><p class="auth">' + ', '.join(authors) + ' | ' + data['placeName'] + ' | ' + date + '</p></div>'
if 'featuredImageObj' in data:
if 'url' in data['featuredImageObj']:
if img_url is not None:
lede = '<p class="cap"><img src="{}">'.format(img_url)
else:
lede = '<p class="cap"><img src="{}">'.format(data['featuredImageObj']['url'])
if 'alt_text' in data['featuredImageObj']:
caption = '<span>' + data['featuredImageObj']['alt_text'] + '</span></p>'
body = data['htmlContent']
return '<html><body>' + cat + title + subhead + auth + lede + caption + '<div><p></p>' + body + '</div></body></html>'
def populate_article_metadata(self, article, soup, first):
article.url = soup.find('h1')['title']
article.summary = self.tag_to_string(soup.find('h3'))
article.text_summary = self.tag_to_string(soup.find('h3'))
article.title = article.title.replace(' - Business Standard', '')