#!/usr/bin/env python
# vim:fileencoding=utf-8
import base64
import json
import re
from collections import defaultdict
from datetime import datetime
from html import escape
from calibre.web.feeds.news import BasicNewsRecipe
keys = [
'ZXlKaGJHY2lPaUpTVXpJMU5pSjk=',
'V0Zac2FITjNNWGQzU213MFYza3dSWEJ6Y2xR',
'cXd3QmVkQVVOWEhUUWNob3dRWjV6TXdtblhxREtlTWhvUkpsa0I3ZHJqV21iMGt0WkNTY0locTVscElpV2FNeU5KQQ==',
'T0RZZ0hBZklvaTdES1drUzhnOEd1bkZOQVhwSkRVT0xkSTJydFFrVEVp',
'RTNvOTByZFpIdW5QUjdwMFVMalJtSENuRG9mQWRwVFFkSnRUWGpROWVFRFpUMnhvb29WR2RCcG9WS2hF',
]
def _content_to_html(content):
html = ''
for node in content or []:
if not isinstance(node, dict):
continue
text = escape(node.get('text', ''))
if not text:
continue
if node.get('type') == 'link' and node.get('uri'):
html += f'{text}'
else:
html += text
return html
def _body_to_html(body):
html = ''
for item in body or []:
if not isinstance(item, dict):
continue
typ = item.get('type', '')
if typ == 'paragraph':
text = _content_to_html(item.get('content'))
if text:
html += f'
{text}
\n'
elif typ in ('heading', 'subheading'):
text = _content_to_html(item.get('content'))
if text:
html += f'
{text}
\n'
elif typ == 'image':
src = item.get('url') or item.get('imageUrl', '')
cap_raw = item.get('caption', '')
cap = _content_to_html(cap_raw) if isinstance(cap_raw, list) else escape(str(cap_raw or ''))
if src:
html += f''
if cap:
html += f'{cap}'
html += '\n'
elif typ == 'list':
tag = 'ol' if item.get('ordered') else 'ul'
html += f'<{tag}>\n'
for li in item.get('items') or item.get('content') or []:
if isinstance(li, dict):
html += f'
{_content_to_html(li.get("content"))}
\n'
html += f'{tag}>\n'
return html
class Barrons(BasicNewsRecipe):
title = "Barron's Magazine"
__author__ = 'unkn0wn'
description = (
"Barron's is an American weekly magazine/newspaper published by Dow Jones & Company. Founded in 1921 as a sister "
"publication to The Wall Street Journal, Barron's covers U.S. financial information, market developments, and "
'relevant statistics.'
)
language = 'en_US'
encoding = 'utf-8'
ignore_duplicate_articles = {'url'}
masthead_url = 'https://www.barrons.com/asset/barrons/images/barrons-logo.png'
resolve_internal_links = True
delay = 1
extra_css = '''
.auth { font-size:small; }
.caption { font-size:small; text-align:center; }
img { display:block; margin:0 auto; }
'''
recipe_specific_options = {
'date': {
'short': 'The date of the edition to download (MMM DD, YYYY format)',
'long': 'For example, Dec 30, 2024',
},
}
def preprocess_raw_html(self, raw, url):
html = raw if isinstance(raw, str) else raw.decode('utf-8', errors='ignore')
nd_match = re.search(r'', html, re.DOTALL)
if not nd_match:
return html
try:
j = json.loads(nd_match.group(1))
pp = j['props']['pageProps']
art = pp.get('articleData', {})
headline_text = art.get('headline', {})
if isinstance(headline_text, dict):
headline_text = headline_text.get('text', '')
byline = art.get('byline', [])
byline_text = ''.join(x.get('text', '') for x in byline if isinstance(x, dict)).strip()
# Subscribers get full body; non-subscribers get snippet (first para only)
body = art.get('body') or pp.get('snippet', [])
out = '\n'
if headline_text:
out += f'
{escape(headline_text)}
\n'
if byline_text:
out += f'
{escape(byline_text)}
\n'
out += _body_to_html(body)
out += ''
return out
except Exception:
return html
def parse_index(self):
# Step 1: Use mobile API for archive listing (cover image + edition dates)
index = 'https://barrons.djmedia.djservices.io'
theatre = '/apps/barrons/theaters/'
archive = self.index_to_soup(
index + theatre + 'magazine-archive?screen_ids=magazine-archive',
raw=True,
)
scrn = json.loads(archive)['screens'][0]['frames']
self.log(
'Available Editions: ',
' | '.join(x['screenIds'][0]['name'][6:] for x in scrn),
)
selected = None
for frme in scrn:
edition_date = self.recipe_specific_options.get('date')
if edition_date and isinstance(edition_date, str):
nme = frme['screenIds'][0]['name']
if edition_date.lower() not in nme.lower():
continue
selected = frme
break
if selected is None:
selected = scrn[0]
nme = selected['screenIds'][0]['name']
self.cover_url = selected['image']['url']
self.log('Downloading ', nme)
self.timefmt = ' [' + nme[6:] + ']'
# Convert mobile API date to website URL
date_text = selected['date']['text'] # e.g. "2026-08-31T04:00:00Z"
dt = datetime.strptime(date_text[:10], '%Y-%m-%d')
date_str = dt.strftime('%Y%m%d')
# Step 2: Fetch article list from website __NEXT_DATA__
web_url = 'https://www.barrons.com/magazine/' + date_str
raw_html = self.index_to_soup(web_url, raw=True).decode('utf-8', errors='ignore')
nd_match = re.search(r'', raw_html, re.DOTALL)
if not nd_match:
raise ValueError('Could not find article data in Barrons magazine page')
mag_data = json.loads(nd_match.group(1))['props']['pageProps']['magazineData']
sections_map = [
('coverArticle', 'Cover Story'),
('featureArticles', 'Features'),
('columnsArticles', 'Columns'),
('departmentsArticles', 'Departments'),
('restArticles', 'More'),
]
feeds_dict = defaultdict(list)
for key, section_name in sections_map:
for art in mag_data.get(key, []):
if not isinstance(art, dict):
continue
url = art.get('url', '')
if 'barrons.com/articles/' not in url:
continue
url = url.split('?')[0]
title = art.get('headline', '')
if not title or not url:
continue
desc = art.get('summary', '') or ''
self.log(' ', title, '\n\t', desc)
feeds_dict[section_name].append({'title': title, 'url': url, 'description': desc})
return list(feeds_dict.items())
def get_browser(self, *args, **kw):
kw['user_agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
br = BasicNewsRecipe.get_browser(self, *args, **kw)
k2 = '.'.join(base64.b64decode(b).decode() for b in keys[:3])
k3 = '_'.join(base64.b64decode(v).decode() for v in keys[3:])
br.addheaders += [
('Accept-Encoding', 'gzip'),
('Accept-Language', 'en-US,en;q=0.9'),
('Sec-Ch-Ua', '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"'),
('Sec-Ch-Ua-Mobile', '?0'),
('Sec-Ch-Ua-Platform', '"Windows"'),
('Sec-Fetch-Dest', 'document'),
('Sec-Fetch-Mode', 'navigate'),
('Sec-Fetch-Site', 'none'),
('Sec-Fetch-User', '?1'),
# Mobile API token (used only for the archive listing)
('App-Identifier', 'com.news.screens'),
('App-Version', '1'),
('Device-Type', 'phone'),
('Os-Name', 'Android'),
('X-Access-Token', k2 + '_' + k3),
]
return br
def populate_article_metadata(self, article, soup, first):
if '/articles/' not in article.url:
article.url = 'https://www.barrons.com/articles/' + article.url.split('=')[-1]