| 1 |
#!/usr/bin/env python |
| 2 |
import json |
| 3 |
import time |
| 4 |
from datetime import datetime, timedelta |
| 5 |
from urllib.parse import quote, urlencode |
| 6 |
from uuid import uuid4 |
| 7 |
|
| 8 |
from html5_parser import parse |
| 9 |
from lxml import etree |
| 10 |
from mechanize import Request |
| 11 |
|
| 12 |
from calibre import browser |
| 13 |
from calibre.ptempfile import PersistentTemporaryFile |
| 14 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 15 |
|
| 16 |
|
| 17 |
def safe_dict(data, *names): |
| 18 |
ans = data |
| 19 |
for x in names: |
| 20 |
ans = ans.get(x) or {} |
| 21 |
return ans |
| 22 |
|
| 23 |
|
| 24 |
def process_web_list(li_node): |
| 25 |
li_html = '' |
| 26 |
for li in li_node['items']: |
| 27 |
if li.get('textHtml'): |
| 28 |
li_html += f'<li>{li["textHtml"]}</li>' |
| 29 |
elif li.get('textJson'): |
| 30 |
li_html += f'<li>{parse_textjson(li["textJson"])}</li>' |
| 31 |
else: |
| 32 |
li_html += f'<li>{li.get("text", "")}</li>' |
| 33 |
return '<ul>' + li_html + '</ul>' |
| 34 |
|
| 35 |
|
| 36 |
def process_info_box(bx): |
| 37 |
info = '' |
| 38 |
for x in safe_dict(bx, 'components'): |
| 39 |
info += f'<blockquote>{process_web_node(x)}</blockquote>' |
| 40 |
return info |
| 41 |
|
| 42 |
|
| 43 |
def parse_txt(ty): |
| 44 |
typ = ty.get('type', '') |
| 45 |
children = ty.get('children', []) |
| 46 |
href = '#' |
| 47 |
attributes = ty.get('attributes') or () |
| 48 |
for a in attributes: |
| 49 |
if a.get('name') == 'href': |
| 50 |
href = a.get('value', href) |
| 51 |
break |
| 52 |
|
| 53 |
tag_map = { |
| 54 |
'text': lambda: [ty.get('value', '')], |
| 55 |
'scaps': lambda: [ |
| 56 |
f'<span style="text-transform: uppercase; font-size: 0.85em; letter-spacing: 0.05em;">{"".join(parse_txt(c))}</span>' for c in children |
| 57 |
], |
| 58 |
'bold': lambda: [f'<b>{"".join(parse_txt(c))}</b>' for c in children], |
| 59 |
'drop_caps': lambda: [f'<b>{"".join(parse_txt(c))}</b>' for c in children], |
| 60 |
'italic': lambda: [f'<i>{"".join(parse_txt(c))}</i>' for c in children], |
| 61 |
'linebreak': lambda: ['<br>'], |
| 62 |
'external_link': lambda: [f'<a href="{href}">{"".join(parse_txt(c))}</a>' for c in children], |
| 63 |
'internal_link': lambda: [f'<a href="{href}">{"".join(parse_txt(c))}</a>' for c in children], |
| 64 |
'ufinish': lambda: [text for c in children for text in parse_txt(c)], |
| 65 |
'subscript': lambda: [f'<sub>{"".join(parse_txt(c))}</sub>' for c in children], |
| 66 |
'superscript': lambda: [f'<sup>{"".join(parse_txt(c))}</sup>' for c in children], |
| 67 |
} |
| 68 |
|
| 69 |
if typ in tag_map: |
| 70 |
yield from tag_map[typ]() |
| 71 |
else: |
| 72 |
print('** ', typ) |
| 73 |
|
| 74 |
|
| 75 |
def parse_textjson(nt): |
| 76 |
return ''.join(''.join(parse_txt(n)) for n in nt) |
| 77 |
|
| 78 |
|
| 79 |
def process_web_node(node): |
| 80 |
ntype = node.get('type', '') |
| 81 |
if ntype == 'CROSSHEAD': |
| 82 |
if node.get('textHtml'): |
| 83 |
return f'<h4>{node.get("textHtml")}</h4>' |
| 84 |
return f'<h4>{node.get("text", "")}</h4>' |
| 85 |
elif ntype in ['PARAGRAPH', 'BOOK_INFO']: |
| 86 |
if node.get('textHtml'): |
| 87 |
return f'\n<p>{node.get("textHtml")}</p>' |
| 88 |
if node.get('textJson'): |
| 89 |
return f'\n<p>{parse_textjson(node["textJson"])}</p>' |
| 90 |
return f'\n<p>{node.get("text", "")}</p>' |
| 91 |
elif (ntype == 'IMAGE') or (node.get('__typename', '') == 'ImageComponent'): |
| 92 |
alt = '' if node.get('altText') is None else node.get('altText') |
| 93 |
cap = '' |
| 94 |
if node.get('caption'): |
| 95 |
if node['caption'].get('textHtml') is not None: |
| 96 |
cap = node['caption']['textHtml'] |
| 97 |
elif node['caption'].get('textJson') is not None: |
| 98 |
cap = parse_textjson(node['caption']['textJson']) |
| 99 |
elif node['caption'].get('text') is not None: |
| 100 |
cap = node['caption']['text'] |
| 101 |
return f'<div><img src="{node["url"]}" title="{alt}"></div><div style="text-align:center; font-size:small;">{cap}</div>' |
| 102 |
elif ntype == 'PULL_QUOTE': |
| 103 |
if node.get('textHtml'): |
| 104 |
return f'<blockquote>{node.get("textHtml")}</blockquote>' |
| 105 |
if node.get('textJson'): |
| 106 |
return f'<blockquote>{parse_textjson(node["textJson"])}</blockquote>' |
| 107 |
return f'<blockquote>{node.get("text", "")}</blockquote>' |
| 108 |
elif ntype == 'BLOCK_QUOTE': |
| 109 |
if node.get('textHtml'): |
| 110 |
return f'<blockquote><i>{node.get("textHtml")}</i></blockquote>' |
| 111 |
if node.get('textJson'): |
| 112 |
return f'<blockquote><i>{parse_textjson(node["textJson"])}</i></blockquote>' |
| 113 |
return f'<blockquote><i>{node.get("text", "")}</i></blockquote>' |
| 114 |
elif ntype == 'DIVIDER': |
| 115 |
return '<hr>' |
| 116 |
elif ntype == 'INFOGRAPHIC': |
| 117 |
if node.get('fallback'): |
| 118 |
return process_web_node(node['fallback']) |
| 119 |
elif ntype == 'INFOBOX': |
| 120 |
return process_info_box(node) |
| 121 |
elif ntype == 'UNORDERED_LIST': |
| 122 |
if node.get('items'): |
| 123 |
return process_web_list(node) |
| 124 |
elif ntype: |
| 125 |
print('** ', ntype) |
| 126 |
return '' |
| 127 |
|
| 128 |
|
| 129 |
def load_article_from_web_json(raw): |
| 130 |
# open('/t/raw.json', 'w').write(raw) |
| 131 |
body = '' |
| 132 |
data = json.loads(raw)['data']['findArticleByUrl'] |
| 133 |
body += f'<div style="color: red; font-size:small; font-weight:bold;">{data.get("flyTitle", "")}</div>' |
| 134 |
body += f'<h1>{data["headline"]}</h1>' |
| 135 |
if data.get('rubric') and data.get('rubric') is not None: |
| 136 |
body += f'<div style="font-style: italic; color:#202020;">{data.get("rubric", "")}</div>' |
| 137 |
try: |
| 138 |
date = data['dateModified'] |
| 139 |
except Exception: |
| 140 |
date = data['datePublished'] |
| 141 |
dt = datetime.fromisoformat(date[:-1]) + timedelta(seconds=time.timezone) |
| 142 |
dt = dt.strftime('%b %d, %Y %I:%M %p') |
| 143 |
if data.get('dateline') is None: |
| 144 |
body += f'<p style="color: gray; font-size: small;">{dt}</p>' |
| 145 |
else: |
| 146 |
body += f'<p style="color: gray; font-size: small;">{dt + " | " + (data["dateline"])}</p>' |
| 147 |
main_image_url = safe_dict(data, 'leadComponent') or '' |
| 148 |
if main_image_url: |
| 149 |
body += process_web_node(data['leadComponent']) |
| 150 |
if data.get('byline'): |
| 151 |
if data['byline'] is not None: |
| 152 |
body += f'<p style="color: gray; font-size: small;"><i>{"By " + data["byline"]}</i></p>' |
| 153 |
for node in data.get('body'): |
| 154 |
body += process_web_node(node) |
| 155 |
return '<html><body><article>' + body + '</article></body></html>' |
| 156 |
|
| 157 |
|
| 158 |
class NoArticles(Exception): |
| 159 |
pass |
| 160 |
|
| 161 |
|
| 162 |
def get_content(url_): |
| 163 |
|
| 164 |
headers = { |
| 165 |
'User-Agent': 'TheEconomist-Liskov-android', |
| 166 |
'accept': 'multipart/mixed; deferSpec=20220824, application/json', |
| 167 |
'accept-encoding': 'gzip', |
| 168 |
'content-type': 'application/json', |
| 169 |
'x-app-trace-id': str(uuid4()), |
| 170 |
'x-economist-consumer': 'TheEconomist-Liskov-android', |
| 171 |
'x-teg-client-name': 'Economist-Android', |
| 172 |
'x-teg-client-os': 'Android', |
| 173 |
'x-teg-client-version': '4.40.0', |
| 174 |
} |
| 175 |
br = browser() |
| 176 |
req = Request( |
| 177 |
url_, |
| 178 |
headers=headers, |
| 179 |
) |
| 180 |
res = br.open(req) |
| 181 |
return res.read() |
| 182 |
|
| 183 |
|
| 184 |
def process_url(url): |
| 185 |
if url.startswith('/'): |
| 186 |
url = 'https://www.economist.com' + url |
| 187 |
return url |
| 188 |
|
| 189 |
|
| 190 |
class econ_search(BasicNewsRecipe): |
| 191 |
title = 'The Economist - Search' |
| 192 |
language = 'en_GB' |
| 193 |
encoding = 'utf-8' |
| 194 |
__author__ = 'unkn0wn' |
| 195 |
description = 'Use the Advanced section of the recipe to search.' |
| 196 |
|
| 197 |
ignore_duplicate_articles = {'url'} |
| 198 |
extra_css = ''' |
| 199 |
em { color:#202020; } |
| 200 |
img {display:block; margin:0 auto;} |
| 201 |
''' |
| 202 |
# economist.com has started throttling after about 60% of the total has |
| 203 |
# downloaded with connection reset by peer (104) errors. |
| 204 |
delay = 1 |
| 205 |
browser_type = 'webengine' |
| 206 |
|
| 207 |
recipe_specific_options = { |
| 208 |
'q': {'short': 'Text Search', 'default': 'schools brief'}, |
| 209 |
's': {'short': 'Sort by (date/relevance)', 'long': 'you can sort by date or relevance', 'default': 'relevance'}, |
| 210 |
'p': {'short': 'number of pages', 'long': 'number of pages of search results you want', 'default': '2'}, |
| 211 |
'res': { |
| 212 |
'short': 'For hi-res images, select a resolution from the\nfollowing options: 834, 960, 1096, 1280, 1424', |
| 213 |
'long': 'This is useful for non e-ink devices, and for a lower file size\nthan the default, use from 480, 384, 360, 256.', |
| 214 |
'default': '600', |
| 215 |
}, |
| 216 |
} |
| 217 |
|
| 218 |
def get_browser(self, *args, **kwargs): |
| 219 |
from calibre.utils.random_ua import common_english_word_ua |
| 220 |
|
| 221 |
kwargs['user_agent'] = common_english_word_ua() |
| 222 |
br = BasicNewsRecipe.get_browser(self, *args, **kwargs) |
| 223 |
return br |
| 224 |
|
| 225 |
def parse_index(self): |
| 226 |
url = 'https://www.economist.com/search?q={query}&sort={sort}&page={page}' |
| 227 |
search = self.recipe_specific_options.get('q') |
| 228 |
sort_type = self.recipe_specific_options.get('s') |
| 229 |
page = self.recipe_specific_options.get('p') |
| 230 |
self.title = 'The Economist - ' + search |
| 231 |
ans = [] |
| 232 |
for num in range(1, int(page) + 1): |
| 233 |
ans.extend(self.economist_parse_index(url.format(query=search.replace(' ', '+'), sort=sort_type, page=num))) |
| 234 |
return [('Articles', ans)] |
| 235 |
|
| 236 |
def economist_parse_index(self, url): |
| 237 |
self.log('Page ', url.rsplit('=', 1)[-1]) |
| 238 |
soup = self.index_to_soup(url) |
| 239 |
results = soup.find('ol', id='search-results') |
| 240 |
if not results: |
| 241 |
self.log('\tPage ', url.rsplit('=', 1)[-1], ' not found') |
| 242 |
return |
| 243 |
for a in results.findAll('a', attrs={'class': '_search-result'}): |
| 244 |
url = a['href'] |
| 245 |
title = self.tag_to_string(a.find(attrs={'class': '_headline'})) |
| 246 |
desc = self.tag_to_string(a.find(attrs={'class': '_snippet'})) |
| 247 |
self.log('\t', title, '\n\t', desc, '\n\t\t', url) |
| 248 |
yield {'title': title, 'url': url, 'description': desc} |
| 249 |
|
| 250 |
def preprocess_html(self, soup): |
| 251 |
width = '600' |
| 252 |
w = self.recipe_specific_options.get('res') |
| 253 |
if w and isinstance(w, str): |
| 254 |
width = w |
| 255 |
for img in soup.findAll('img', src=True): |
| 256 |
qua = 'economist.com/cdn-cgi/image/width=' + width + ',quality=80,format=auto/' |
| 257 |
img['src'] = img['src'].replace('economist.com/', qua) |
| 258 |
return soup |
| 259 |
|
| 260 |
def preprocess_raw_html(self, raw, url): |
| 261 |
# open('/t/raw.html', 'wb').write(raw.encode('utf-8')) |
| 262 |
html = load_article_from_web_json(raw) |
| 263 |
root = parse(html) |
| 264 |
# the economist uses <small> for small caps with a custom font |
| 265 |
for init in root.xpath('//span[@data-caps="initial"]'): |
| 266 |
init.set('style', 'font-weight:bold;') |
| 267 |
for x in root.xpath('//small'): |
| 268 |
if x.text and len(x) == 0: |
| 269 |
x.text = x.text.upper() |
| 270 |
x.tag = 'span' |
| 271 |
x.set( |
| 272 |
'style', |
| 273 |
'text-transform: uppercase; font-size: 0.85em; letter-spacing: 0.05em;', |
| 274 |
) |
| 275 |
for h2 in root.xpath('//h2'): |
| 276 |
h2.tag = 'h4' |
| 277 |
for x in root.xpath('//figcaption'): |
| 278 |
x.set('style', 'text-align:center; font-size:small;') |
| 279 |
for x in root.xpath('//cite'): |
| 280 |
x.tag = 'blockquote' |
| 281 |
x.set('style', 'color:#404040;') |
| 282 |
raw = etree.tostring(root, encoding='unicode') |
| 283 |
return raw |
| 284 |
|
| 285 |
def get_article(self, url): |
| 286 |
query = { |
| 287 |
'operationName': 'ArticleDeeplinkQuery', |
| 288 |
'variables': '{{"ref":"{}"}}'.format(url), |
| 289 |
'query': 'query ArticleDeeplinkQuery($ref: String!, $includeRelatedArticles: Boolean = true ) { findArticleByUrl(url: $ref) { __typename ...ArticleDataFragment } } fragment ContentIdentityFragment on ContentIdentity { articleType forceAppWebView leadMediaType } fragment NarrationFragment on Narration { album bitrate duration filename id provider url isAiGenerated fileHash } fragment ImageTeaserFragment on ImageComponent { altText height imageType source url width } fragment PodcastAudioFragment on PodcastEpisode { id audio { url durationInSeconds } } fragment ArticleTeaserFragment on Article { id tegId url rubric headline flyTitle brand byline dateFirstPublished dateline dateModified datePublished dateRevised estimatedReadTime wordCount printHeadline contentIdentity { __typename ...ContentIdentityFragment } section { tegId name } teaserImage { __typename type ...ImageTeaserFragment } leadComponent { __typename type ...ImageTeaserFragment } narration(selectionMethod: PREFER_ACTOR_NARRATION) { __typename ...NarrationFragment } podcast { __typename ...PodcastAudioFragment } } fragment AnnotatedTextFragment on AnnotatedText { text textJson annotations { type length index attributes { name value } } } fragment ImageComponentFragment on ImageComponent { altText caption { __typename ...AnnotatedTextFragment } credit height imageType mode source url width } fragment BlockQuoteComponentFragment on BlockQuoteComponent { text textJson annotations { type length index attributes { name value } } } fragment BookInfoComponentFragment on BookInfoComponent { text textJson annotations { type length index attributes { name value } } } fragment ParagraphComponentFragment on ParagraphComponent { text textJson annotations { type length index attributes { name value } } } fragment PullQuoteComponentFragment on PullQuoteComponent { text textJson annotations { type length index attributes { name value } } } fragment CrossheadComponentFragment on CrossheadComponent { text } fragment OrderedListComponentFragment on OrderedListComponent { items { __typename ...AnnotatedTextFragment } } fragment UnorderedListComponentFragment on UnorderedListComponent { items { __typename ...AnnotatedTextFragment } } fragment VideoComponentFragment on VideoComponent { url title thumbnailImage } fragment InfoboxComponentFragment on InfoboxComponent { components { __typename type ...BlockQuoteComponentFragment ...BookInfoComponentFragment ...ParagraphComponentFragment ...PullQuoteComponentFragment ...CrossheadComponentFragment ...OrderedListComponentFragment ...UnorderedListComponentFragment ...VideoComponentFragment } } fragment InfographicComponentFragment on InfographicComponent { url title width fallback { __typename ...ImageComponentFragment } altText height width } fragment ArticleDataFragment on Article { id url brand byline rubric headline layout { headerStyle } contentIdentity { __typename ...ContentIdentityFragment } dateline dateFirstPublished dateModified datePublished dateRevised estimatedReadTime narration(selectionMethod: PREFER_ACTOR_NARRATION) { __typename ...NarrationFragment } printFlyTitle printHeadline printRubric flyTitle wordCount section { tegId name articles(pagingInfo: { pagingType: OFFSET pageSize: 6 pageNumber: 1 } ) @include(if: $includeRelatedArticles) { edges { node { __typename ...ArticleTeaserFragment } } } } teaserImage { __typename type ...ImageComponentFragment } tegId leadComponent { __typename type ...ImageComponentFragment } body { __typename type ...BlockQuoteComponentFragment ...BookInfoComponentFragment ...ParagraphComponentFragment ...PullQuoteComponentFragment ...CrossheadComponentFragment ...OrderedListComponentFragment ...UnorderedListComponentFragment ...InfoboxComponentFragment ...ImageComponentFragment ...VideoComponentFragment ...InfographicComponentFragment } footer { __typename type ...ParagraphComponentFragment } tags { name } ads { adData } podcast { __typename ...PodcastAudioFragment } }', # noqa: E501 |
| 290 |
} |
| 291 |
deep_url = 'https://cp2-graphql-gateway.p.aws.economist.com/graphql?' + urlencode(query, safe='()!', quote_via=quote) |
| 292 |
raw = get_content(deep_url) |
| 293 |
return raw |
| 294 |
|
| 295 |
def print_version(self, url): |
| 296 |
art_cont = self.get_article(url) |
| 297 |
pt = PersistentTemporaryFile('.html') |
| 298 |
pt.write(art_cont) |
| 299 |
pt.close() |
| 300 |
return 'file:///' + pt.name |