| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
|
| 4 |
import json |
| 5 |
import re |
| 6 |
from collections import defaultdict |
| 7 |
from datetime import date |
| 8 |
|
| 9 |
from calibre.web.feeds.news import BasicNewsRecipe, classes |
| 10 |
|
| 11 |
|
| 12 |
def absurl(url): |
| 13 |
if url.startswith('/'): |
| 14 |
return 'https://www.thehindubusinessline.com' + url |
| 15 |
return url |
| 16 |
|
| 17 |
|
| 18 |
class BusinessLine(BasicNewsRecipe): |
| 19 |
title = 'The Hindu BusinessLine' |
| 20 |
__author__ = 'unkn0wn, Hardik Gupta' |
| 21 |
description = ( |
| 22 |
'The Hindu BusinessLine is known for its credibility, accuracy, in-depth analysis of markets and sober coverage' |
| 23 |
' of business news. BusinessLine reduces the daily grind of business to relevant, readable, byte-sized stories.' |
| 24 |
' The newspaper is extensively followed by the decision makers and change leaders from the world of business.' |
| 25 |
) |
| 26 |
|
| 27 |
def get_cover_url(self): |
| 28 |
soup = self.index_to_soup('https://www.magzter.com/IN/THG-publishing-pvt-ltd/The-Hindu-Business-Line/Newspaper/') |
| 29 |
return soup.find('img', id=lambda s: s and 'mgd__lhd__cover' in s.split())['src'] |
| 30 |
|
| 31 |
language = 'en_IN' |
| 32 |
|
| 33 |
no_stylesheets = True |
| 34 |
remove_attributes = ['style', 'height', 'width'] |
| 35 |
|
| 36 |
masthead_url = 'https://assetsbl.thehindubusinessline.com/theme/images/bl-online/logo.svg' |
| 37 |
|
| 38 |
extra_css = ''' |
| 39 |
body { font-family: serif; line-height:1.5; } |
| 40 |
img { display:block; margin:1em auto; max-width:100%; } |
| 41 |
.photo-container { text-align:center; margin:1.5em 0; padding: 0.5em; border-bottom: 2px solid #ddd; } |
| 42 |
.photo-caption { font-size:0.85em; font-style: italic; color:#555; margin-top:0.5em; display: block; line-height: 1.3; } |
| 43 |
.byline { font-size:0.9em; color:#444; margin-bottom:1em; font-weight: bold; } |
| 44 |
.keywords { margin-top:2em; font-size:0.9em; border-top:1px solid #ccc; padding-top:0.5em; } |
| 45 |
''' |
| 46 |
|
| 47 |
recipe_specific_options = { |
| 48 |
'location': {'short': 'Edition (e.g., chennai, delhi)', 'default': 'chennai'}, |
| 49 |
'date': { |
| 50 |
'short': 'Edition date (YYYY-MM-DD)', |
| 51 |
}, |
| 52 |
} |
| 53 |
|
| 54 |
ignore_duplicate_articles = {'url'} |
| 55 |
|
| 56 |
keep_only_tags = [classes('article-section'), classes('article-main-col'), dict(id='content-body')] |
| 57 |
|
| 58 |
remove_tags = [dict(id='paywallbox'), classes('comments-shares share-page editiondetails')] |
| 59 |
|
| 60 |
def preprocess_html(self, soup): |
| 61 |
keywords = [] |
| 62 |
|
| 63 |
# ------------------------- |
| 64 |
# Extract metadata |
| 65 |
# ------------------------- |
| 66 |
header = soup.find('div', class_='article-section') |
| 67 |
|
| 68 |
title = '' |
| 69 |
author = '' |
| 70 |
dateline = '' |
| 71 |
|
| 72 |
if header: |
| 73 |
h1 = header.find('h1') |
| 74 |
if h1: |
| 75 |
title = self.tag_to_string(h1) |
| 76 |
|
| 77 |
auth = header.find('h3', class_='author') |
| 78 |
if auth: |
| 79 |
author = self.tag_to_string(auth) |
| 80 |
|
| 81 |
date_div = header.find('div', class_='dateline') |
| 82 |
if date_div: |
| 83 |
dateline = self.tag_to_string(date_div) |
| 84 |
|
| 85 |
# ------------------------- |
| 86 |
# Normalize ALL images |
| 87 |
# ------------------------- |
| 88 |
for img in soup.find_all('img'): |
| 89 |
src = img.get('data-original') or img.get('data-src') or img.get('data-src-template') or img.get('src') |
| 90 |
if src: |
| 91 |
img['src'] = absurl(src) |
| 92 |
|
| 93 |
# ------------------------- |
| 94 |
# Fix article images + captions |
| 95 |
# ------------------------- |
| 96 |
for pic in soup.find_all('div', class_='article-picture'): |
| 97 |
# Use a standard div with a class instead of <figure> |
| 98 |
figure = soup.new_tag('div') |
| 99 |
figure['class'] = 'photo-container' |
| 100 |
|
| 101 |
img = pic.find('img') |
| 102 |
if img: |
| 103 |
figure.append(img) |
| 104 |
|
| 105 |
caption = None |
| 106 |
|
| 107 |
cap = pic.find('p', class_='caption') |
| 108 |
if cap: |
| 109 |
caption = cap.get_text(' ', strip=True) |
| 110 |
|
| 111 |
if not caption: |
| 112 |
nxt = pic.find_next_sibling() |
| 113 |
if nxt and nxt.name == 'p' and 'caption' in (nxt.get('class') or []): |
| 114 |
caption = nxt.get_text(' ', strip=True) |
| 115 |
nxt.decompose() |
| 116 |
|
| 117 |
if caption: |
| 118 |
# Use a standard div with a class instead of <figcaption> |
| 119 |
figcap = soup.new_tag('div') |
| 120 |
figcap['class'] = 'photo-caption' |
| 121 |
figcap.string = caption |
| 122 |
figure.append(figcap) |
| 123 |
|
| 124 |
pic.replace_with(figure) |
| 125 |
|
| 126 |
# ------------------------- |
| 127 |
# Collect subheads as keywords |
| 128 |
# ------------------------- |
| 129 |
for sub in soup.find_all('div', class_='subhead'): |
| 130 |
txt = sub.get_text(strip=True) |
| 131 |
if txt: |
| 132 |
keywords.append(txt) |
| 133 |
sub.decompose() |
| 134 |
|
| 135 |
# ------------------------- |
| 136 |
# Remove empty paragraphs |
| 137 |
# ------------------------- |
| 138 |
for p in soup.find_all('p'): |
| 139 |
if not p.get_text(strip=True) and not p.find('img'): |
| 140 |
p.decompose() |
| 141 |
|
| 142 |
# ------------------------- |
| 143 |
# Build clean article |
| 144 |
# ------------------------- |
| 145 |
|
| 146 |
# 1. Rebuild the header in its original location |
| 147 |
if header: |
| 148 |
new_header = soup.new_tag('div') |
| 149 |
new_header['class'] = 'clean-header' # Optional wrapper |
| 150 |
|
| 151 |
if title: |
| 152 |
h1_tag = soup.new_tag('h1') |
| 153 |
h1_tag.string = title |
| 154 |
new_header.append(h1_tag) |
| 155 |
|
| 156 |
if author or dateline: |
| 157 |
byline = soup.new_tag('div') |
| 158 |
byline['class'] = 'byline' |
| 159 |
byline.string = ' | '.join(filter(None, [author, dateline])) |
| 160 |
new_header.append(byline) |
| 161 |
|
| 162 |
# Replace the old messy header with our clean reconstructed one |
| 163 |
header.replace_with(new_header) |
| 164 |
|
| 165 |
# 2. Add keywords to the end of the body |
| 166 |
body = soup.find('div', id='content-body') |
| 167 |
if body and keywords: |
| 168 |
kw = soup.new_tag('div') |
| 169 |
kw['class'] = 'keywords' |
| 170 |
kw.string = 'Keywords: ' + ' - '.join(keywords) |
| 171 |
body.append(kw) |
| 172 |
|
| 173 |
return soup |
| 174 |
|
| 175 |
def parse_index(self): |
| 176 |
self.title = 'BusinessLine' |
| 177 |
|
| 178 |
local_edition = 'bl_chennai' |
| 179 |
loc = self.recipe_specific_options.get('location') |
| 180 |
|
| 181 |
# CLI Fallback: If it's still the schema dictionary, extract the default value |
| 182 |
if isinstance(loc, dict): |
| 183 |
loc = loc.get('default', 'chennai') |
| 184 |
|
| 185 |
if loc: |
| 186 |
local_edition = 'bl_' + loc |
| 187 |
|
| 188 |
dt = date.today() |
| 189 |
past = self.recipe_specific_options.get('date') |
| 190 |
|
| 191 |
# CLI Fallback: Extract default if it's the schema dictionary (returns None if undefined) |
| 192 |
if isinstance(past, dict): |
| 193 |
past = past.get('default') |
| 194 |
|
| 195 |
if past: |
| 196 |
y, m, d = (int(x) for x in past.split('-')) |
| 197 |
dt = date(y, m, d) |
| 198 |
|
| 199 |
today = dt.strftime('%Y-%m-%d') |
| 200 |
|
| 201 |
self.log(f'Downloading BusinessLine ({local_edition[3:]}) - {today}') |
| 202 |
|
| 203 |
url = absurl(f'/todays-paper/{today}/{local_edition}/') |
| 204 |
|
| 205 |
raw = self.index_to_soup(url, raw=True) |
| 206 |
soup = self.index_to_soup(raw) |
| 207 |
|
| 208 |
feeds = self.parse_businessline_index(soup) |
| 209 |
|
| 210 |
if not feeds: |
| 211 |
raise ValueError('BusinessLine not published today.') |
| 212 |
|
| 213 |
cover = soup.find('img') |
| 214 |
if cover: |
| 215 |
self.cover_url = absurl(cover.get('src')) |
| 216 |
|
| 217 |
return feeds |
| 218 |
|
| 219 |
def parse_businessline_index(self, soup): |
| 220 |
for script in soup.find_all('script'): |
| 221 |
text = self.tag_to_string(script) |
| 222 |
|
| 223 |
if 'grouped_articles = {' not in text: |
| 224 |
continue |
| 225 |
|
| 226 |
match = re.search(r'grouped_articles = ({\".*)', text) |
| 227 |
if not match: |
| 228 |
continue |
| 229 |
|
| 230 |
data = json.JSONDecoder().raw_decode(match.group(1))[0] |
| 231 |
|
| 232 |
feeds_dict = defaultdict(list) |
| 233 |
|
| 234 |
for section_key in data: |
| 235 |
section = section_key.replace('BL_', '').replace('bl_', '') |
| 236 |
|
| 237 |
for item in data[section_key]: |
| 238 |
title = item.get('articleheadline', 'No title') |
| 239 |
url = absurl(item.get('href', '')) |
| 240 |
desc = f"Page {item.get('pageno', '')}" |
| 241 |
|
| 242 |
self.log(' ', title) |
| 243 |
|
| 244 |
feeds_dict[section].append({'title': title, 'url': url, 'description': desc}) |
| 245 |
|
| 246 |
return list(feeds_dict.items()) |
| 247 |
|
| 248 |
return [] |