| 1 |
#!/usr/bin/env pythona |
| 2 |
import json |
| 3 |
|
| 4 |
from html5_parser import parse |
| 5 |
|
| 6 |
from calibre.web.feeds.news import BasicNewsRecipe, prefixed_classes |
| 7 |
|
| 8 |
|
| 9 |
def absurl(url): |
| 10 |
if url.startswith('/'): |
| 11 |
url = 'https://asia.nikkei.com' + url |
| 12 |
return url |
| 13 |
|
| 14 |
|
| 15 |
class Nikkei(BasicNewsRecipe): |
| 16 |
title = 'Nikkei Asia Magazine' |
| 17 |
__author__ = 'unkn0wn' |
| 18 |
language = 'en' |
| 19 |
no_stylesheets = True |
| 20 |
description = 'The voice of the Asian century. Trusted independent journalism from Asia, the center of global growth.' |
| 21 |
masthead_url = 'https://upload.wikimedia.org/wikipedia/commons/2/2f/Nikkei_Asia_logo.svg' |
| 22 |
remove_attributes = ['style', 'height', 'width'] |
| 23 |
ignore_duplicate_articles = {'url'} |
| 24 |
resolve_internal_links = True |
| 25 |
remove_empty_feeds = True |
| 26 |
encoding = 'utf-8' |
| 27 |
use_embedded_content = False |
| 28 |
|
| 29 |
extra_css = ''' |
| 30 |
.subhead { font-style:italic; color:#202020; } |
| 31 |
em, blockquote { color:#202020; } |
| 32 |
.sec, .byline { font-size:small; font-weight:bold; } |
| 33 |
.article__image, .article__caption { font-size:small; text-align:center; } |
| 34 |
''' |
| 35 |
|
| 36 |
recipe_specific_options = {'date': {'short': 'The edition date (YYYY-MM-DD format)', 'long': '2024-09-19'}} |
| 37 |
|
| 38 |
remove_tags = [dict(name='svg')] |
| 39 |
|
| 40 |
def parse_index(self): |
| 41 |
d = self.recipe_specific_options.get('date') |
| 42 |
if d and isinstance(d, str): |
| 43 |
url = 'https://asia.nikkei.com/Print-Edition/Issue-' + d |
| 44 |
else: |
| 45 |
archives = self.index_to_soup('https://asia.nikkei.com/Print-Edition/Archives') |
| 46 |
card = archives.find(**prefixed_classes('MagazineIssueCardArchives_magazineIssueCardContent__')) |
| 47 |
url = absurl(card.a['href']) |
| 48 |
|
| 49 |
self.timefmt = f' [{url.split("Issue-")[-1]}]' |
| 50 |
self.title = 'Nikkei Asia' |
| 51 |
self.log(self.title, self.timefmt) |
| 52 |
soup = self.index_to_soup(url) |
| 53 |
self.cover_url = soup.find(**prefixed_classes('MagazineIssueCard_magazineIssueCardCoverImage__'))['src'].split('?')[0] + '?width=600&source=nar-cms' |
| 54 |
|
| 55 |
ans = [] |
| 56 |
|
| 57 |
grid = soup.find(**prefixed_classes('MagazineArticles_magazineArticlesGrid__')) |
| 58 |
for a in grid.findAll( |
| 59 |
**prefixed_classes('MagazineArticlesSpotlightCard_magazineArticlesSpotlightCardHeadline__ StreamArticleCard_streamArticleCardHeadline__') |
| 60 |
): |
| 61 |
title = self.tag_to_string(a) |
| 62 |
url = absurl(a.a['href']) |
| 63 |
desc = '' |
| 64 |
exc = a.findNext( |
| 65 |
**prefixed_classes('MagazineArticlesSpotlightCard_magazineArticlesSpotlightCardSubheadWrapper__ StreamArticleCard_streamArticleCardSubhead__') |
| 66 |
) |
| 67 |
if exc: |
| 68 |
desc = self.tag_to_string(exc) |
| 69 |
self.log(title, '\n ', desc, '\n ', url) |
| 70 |
ans.append({'title': title, 'url': url, 'description': desc}) |
| 71 |
return [('Articles', ans)] |
| 72 |
|
| 73 |
def preprocess_raw_html(self, raw, url): |
| 74 |
root = parse(raw) |
| 75 |
script = root.xpath('//script[@id="__NEXT_DATA__"]')[0].text |
| 76 |
data = json.loads(script)['props']['pageProps']['data'] |
| 77 |
title = f'<h1>{data["headline"]}</h1>' |
| 78 |
exp = auth = image = sec = '' |
| 79 |
sec = f'<div class="sec">{data["primaryTag"]["name"]}</div>' |
| 80 |
if data.get('subhead'): |
| 81 |
exp = f'<p class="subhead">{data["subhead"]}</p>' |
| 82 |
if data.get('byline'): |
| 83 |
auth = f'<p class="byline">{data["byline"]}</p>' |
| 84 |
if data.get('image'): |
| 85 |
img = data['image'] |
| 86 |
image = f'<div><img src="{img["imageUrl"]}"><div class="article__caption">{data.get("fullCaption", "")}</div></div>' |
| 87 |
return '<html><body>' + sec + title + exp + image + auth + data['body'] + '</body></html>' |
| 88 |
|
| 89 |
def preprocess_html(self, soup): |
| 90 |
for attr in self.remove_attributes: |
| 91 |
for x in soup.findAll(attrs={attr: True}): |
| 92 |
del x[attr] |
| 93 |
for img in soup.findAll('img', src=True): |
| 94 |
img['src'] = img['src'].split('?')[0] + '?width=600&source=nar-cms' |
| 95 |
return soup |