| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
import json |
| 4 |
from datetime import datetime |
| 5 |
|
| 6 |
from html5_parser import parse |
| 7 |
|
| 8 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 9 |
|
| 10 |
|
| 11 |
class BusinessStandard(BasicNewsRecipe): |
| 12 |
title = 'Business Standard' |
| 13 |
__author__ = 'unkn0wn' |
| 14 |
description = "India's most respected business daily" |
| 15 |
language = 'en_IN' |
| 16 |
masthead_url = 'https://bsmedia.business-standard.com/include/_mod/site/html5/images/business-standard-logo.png' |
| 17 |
|
| 18 |
no_stylesheets = True |
| 19 |
remove_javascript = True |
| 20 |
remove_attributes = ['width', 'height', 'style'] |
| 21 |
|
| 22 |
def get_browser(self): |
| 23 |
return BasicNewsRecipe.get_browser(self, user_agent='common_words/based') |
| 24 |
|
| 25 |
ignore_duplicate_articles = {'title', 'url'} |
| 26 |
remove_empty_feeds = True |
| 27 |
resolve_internal_links = True |
| 28 |
max_articles_per_feed = 50 |
| 29 |
oldest_article = 1.15 |
| 30 |
browser_type = 'webengine' |
| 31 |
|
| 32 |
extra_css = ''' |
| 33 |
img {display:block; margin:0 auto;} |
| 34 |
.sub { font-style:italic; color:#202020; } |
| 35 |
.auth, .cat { font-size:small; color:#202020; } |
| 36 |
.cap { font-size:small; text-align:center; } |
| 37 |
''' |
| 38 |
|
| 39 |
recipe_specific_options = { |
| 40 |
'days': { |
| 41 |
'short': 'Oldest article to download from this news source. In days ', |
| 42 |
'long': 'For example, 0.5, gives you articles from the past 12 hours', |
| 43 |
'default': str(oldest_article), |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
def get_cover_url(self): |
| 48 |
d = self.recipe_specific_options.get('date') |
| 49 |
if not (d and isinstance(d, str)): |
| 50 |
soup = self.index_to_soup('https://www.magzter.com/IN/Business-Standard-Private-Ltd/Business-Standard/Newspaper/') |
| 51 |
for citem in soup.findAll('meta', content=lambda s: s and s.endswith('view/3.jpg')): |
| 52 |
return citem['content'] |
| 53 |
|
| 54 |
def __init__(self, *args, **kwargs): |
| 55 |
BasicNewsRecipe.__init__(self, *args, **kwargs) |
| 56 |
d = self.recipe_specific_options.get('days') |
| 57 |
if d and isinstance(d, str): |
| 58 |
self.oldest_article = float(d) |
| 59 |
|
| 60 |
# https://www.business-standard.com/rss-feeds/listing |
| 61 |
feeds = [ |
| 62 |
('Top Stories', 'https://www.business-standard.com/rss/home_page_top_stories.rss'), |
| 63 |
('Todays Paper', 'https://www.business-standard.com/rss/todays-paper.rss'), |
| 64 |
('Budget', 'https://www.business-standard.com/rss/budget-110.rss'), |
| 65 |
('Economy', 'https://www.business-standard.com/rss/economy-102.rss'), |
| 66 |
('Opinion', 'https://www.business-standard.com/rss/opinion-105.rss'), |
| 67 |
('Companies', 'https://www.business-standard.com/rss/companies-101.rss'), |
| 68 |
('Industries', 'https://www.business-standard.com/rss/industry-217.rss'), |
| 69 |
('Market', 'https://www.business-standard.com/rss/markets-106.rss'), |
| 70 |
('Politics', 'https://www.business-standard.com/rss/budget-110.rss'), |
| 71 |
('World', 'https://www.business-standard.com/rss/industry-217.rss'), |
| 72 |
('Technology', 'https://www.business-standard.com/rss/technology-108.rss'), |
| 73 |
('Latest', 'https://www.business-standard.com/rss/latest.rss'), |
| 74 |
] |
| 75 |
|
| 76 |
def preprocess_raw_html(self, raw, *a): |
| 77 |
root = parse(raw) |
| 78 |
m = root.xpath('//script[@id="__NEXT_DATA__"]') |
| 79 |
|
| 80 |
data = json.loads(m[0].text) |
| 81 |
|
| 82 |
img_url = None |
| 83 |
if 'articleImageUrl' in data['props']['pageProps']['articleSchema']: |
| 84 |
img_url = data['props']['pageProps']['articleSchema']['articleImageUrl'] |
| 85 |
|
| 86 |
art_url = 'https://www.business-standard.com' + data['props']['pageProps']['url'] |
| 87 |
|
| 88 |
data = data['props']['pageProps']['data'] |
| 89 |
|
| 90 |
title = '<h1 title="{}">'.format(art_url) + data['pageTitle'] + '</h1>' |
| 91 |
|
| 92 |
cat = subhead = lede = auth = caption = '' |
| 93 |
|
| 94 |
if 'defaultArticleCat' in data and data['defaultArticleCat'] is not None: |
| 95 |
if 'h1_tag' in data['defaultArticleCat'] and data['defaultArticleCat']['h1_tag'] is not None: |
| 96 |
cat = '<div class="cat">' + data['defaultArticleCat']['h1_tag'] + '</div>' |
| 97 |
|
| 98 |
if 'metaDescription' in data and data['metaDescription'] is not None: |
| 99 |
subhead = '<p class="sub">' + data['metaDescription'] + '</p>' |
| 100 |
self.art_desc = data['metaDescription'] |
| 101 |
|
| 102 |
date = (datetime.fromtimestamp(int(data['publishDate']))).strftime('%b %d, %Y | %I:%M %p') |
| 103 |
|
| 104 |
authors = [] |
| 105 |
if 'articleMappedMultipleAuthors' in data: |
| 106 |
for aut in data['articleMappedMultipleAuthors']: |
| 107 |
authors.append(data['articleMappedMultipleAuthors'][str(aut)]) |
| 108 |
auth = '<div><p class="auth">' + ', '.join(authors) + ' | ' + data['placeName'] + ' | ' + date + '</p></div>' |
| 109 |
|
| 110 |
if 'featuredImageObj' in data: |
| 111 |
if 'url' in data['featuredImageObj']: |
| 112 |
if img_url is not None: |
| 113 |
lede = '<p class="cap"><img src="{}">'.format(img_url) |
| 114 |
else: |
| 115 |
lede = '<p class="cap"><img src="{}">'.format(data['featuredImageObj']['url']) |
| 116 |
if 'alt_text' in data['featuredImageObj']: |
| 117 |
caption = '<span>' + data['featuredImageObj']['alt_text'] + '</span></p>' |
| 118 |
|
| 119 |
body = data['htmlContent'] |
| 120 |
|
| 121 |
return '<html><body>' + cat + title + subhead + auth + lede + caption + '<div><br>' + body + '</div></body></html>' |
| 122 |
|
| 123 |
def preprocess_html(self, soup): |
| 124 |
for img in soup.findAll('img'): |
| 125 |
img.attrs = {'src': img.get('src', '')} |
| 126 |
for x in soup.findAll('div', 'p'): |
| 127 |
x.attrs = {'class': x.get('class', '')} |
| 128 |
for attr in self.remove_attributes: |
| 129 |
for x in soup.findAll(attrs={attr: True}): |
| 130 |
del x[attr] |
| 131 |
for br in soup.findAll(attrs={'class': lambda x: x and x.startswith('brtag')}): |
| 132 |
br.name = 'div' |
| 133 |
return soup |