forked from ping/newsrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscmp.recipe.py
250 lines (228 loc) · 9.98 KB
/
scmp.recipe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
scmp.com
"""
import os
import sys
from datetime import datetime, timezone, timedelta
from urllib.parse import urlparse
# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe, format_title, get_datetime_format
from calibre.web.feeds.news import BasicNewsRecipe
_name = "South China Morning Post"
class SCMP(BasicNewsrackRecipe, BasicNewsRecipe):
title = _name
__author__ = "llam"
description = "SCMP.com, Hong Kong's premier online English daily provides exclusive up-to-date news, audio video news, podcasts, RSS Feeds, Blogs, breaking news, top stories, award winning news and analysis on Hong Kong and China. https://www.scmp.com/" # noqa
publisher = "South China Morning Post Publishers Ltd."
publication_type = "newspaper"
oldest_article = 1
max_articles_per_feed = 25
language = "en"
ignore_duplicate_articles = {"title", "url"}
masthead_url = (
"https://cdn.shopify.com/s/files/1/0280/0258/2595/files/SCMP_Logo_2018_540x.png"
)
timeout = 30
# used when unable to extract article from <script>, particularly in the Sports section
remove_tags = [
dict(
class_=[
"sticky-wrap",
"relative",
"social-media",
"social-media--extended__shares",
"article-body-comment",
"scmp_button_comment_wrapper",
"social-media--extended__in-site",
"footer",
"scmp-advert-tile",
"sidebar-col",
"related-article",
"topic__add",
"head__main-images",
"share-widget",
"trust-label",
"follow-topic",
"article-author__follow-button",
"piano-metering__paywall-container",
"read-more--hide",
]
),
dict(
attrs={
"data-qa": [
"GenericArticle-WidgetsTop",
"GenericArticle-Sponsor",
"GenericArticle-Follow",
"ArticleAuthorBlock-AvatarContainer",
"FollowTooltip-ChildrenContainer",
"ArticleTrustLabel-Container",
"InlineAdSlot-Container",
"AdSlot-Container",
"GenericArticle-Comment",
"GenericArticle-MoreOnThis",
"GenericArticle-Topic",
"GenericArticle-Left",
"ArticleHeaderAdSlot-CSSTransition",
"GenericArticle-MobileContentHeaderAdSlot",
"Component-SCMPYoutubeVideoContainer",
"DigitalArchiveLink-RootContainer",
]
}
),
dict(attrs={"addthis_title": True}),
dict(name=["script", "style", "svg"]),
]
remove_attributes = ["style", "font"]
extra_css = """
.headline { font-size: 1.8rem; margin-bottom: 0.4rem; }
.sub-headline { font-size: 1rem; margin-bottom: 1.5rem; }
.sub-headline ul { padding-left: 1rem; }
.sub-headline ul li { fmargin-bottom: 0.8rem; }
.article-meta, .article-header__publish { padding-bottom: 0.5rem; }
.article-meta .author { font-weight: bold; color: #444; }
.article-meta .published-dt { margin-left: 0.5rem; }
.article-img { margin-bottom: 0.8rem; max-width: 100%; }
.article-img img, .carousel__slide img {
display: block; margin-bottom: 0.3rem; max-width: 100%; height: auto;
box-sizing: border-box; }
.article-img .caption, .article-caption { font-size: 0.8rem; }
"""
# https://www.scmp.com/rss
feeds = [
("Hong Kong", "https://www.scmp.com/rss/2/feed"),
("China", "https://www.scmp.com/rss/4/feed"),
("Asia", "https://www.scmp.com/rss/3/feed"),
# ("World", "https://www.scmp.com/rss/5/feed"),
# ("Business", "https://www.scmp.com/rss/92/feed"),
# ("Tech", "https://www.scmp.com/rss/36/feed"),
# ("Life", "https://www.scmp.com/rss/94/feed"),
# ("Culture", "https://www.scmp.com/rss/322296/feed"),
# ("Sport", "https://www.scmp.com/rss/95/feed"),
# ("Post Mag", "https://www.scmp.com/rss/71/feed"),
# ("Style", "https://www.scmp.com/rss/72/feed"),
]
def _extract_child_nodes(self, children, ele, soup, level=1):
if not children:
return
child_html = ""
for child in children:
if child.get("type", "") == "text":
child_html += child["data"]
else:
if child["type"] == "iframe":
# change iframe to <span> with the src linked
new_ele = soup.new_tag("span")
new_ele["class"] = f'embed-{child["type"]}'
iframe_src = child.get("attribs", {}).get("src")
a_tag = soup.new_tag("a")
a_tag["href"] = iframe_src
a_tag.string = f"[Embed: {iframe_src}]"
new_ele.append(a_tag)
else:
new_ele = soup.new_tag(child["type"])
for k, v in child.get("attribs", {}).items():
if k.startswith("data-"):
continue
new_ele[k] = v
if child.get("children"):
self._extract_child_nodes(
child["children"], new_ele, soup, level + 1
)
child_html += str(new_ele)
if child["type"] == "img":
# generate a caption <span> tag for <img>
caption_text = child.get("attribs", {}).get("alt") or child.get(
"attribs", {}
).get("title")
if caption_text:
new_ele = soup.new_tag("span")
new_ele.append(caption_text)
new_ele["class"] = "caption"
child_html += str(new_ele)
ele["class"] = "article-img"
ele.append(self.soup(child_html))
def preprocess_raw_html(self, raw_html, url):
soup = self.soup(raw_html)
article = self.get_script_json(soup, r"window.__APOLLO_STATE__\s*=\s*")
if not article:
if os.environ.get("recipe_debug_folder", ""):
recipe_folder = os.path.join(os.environ["recipe_debug_folder"], "scmp")
if not os.path.exists(recipe_folder):
os.makedirs(recipe_folder)
debug_output_file = os.path.join(
recipe_folder, os.path.basename(urlparse(url).path)
)
if not debug_output_file.endswith(".html"):
debug_output_file += ".html"
self.log(f'Writing debug raw html to "{debug_output_file}" for {url}')
with open(debug_output_file, "w", encoding="utf-8") as f:
f.write(raw_html)
self.log(f"Unable to find article from script in {url}")
return raw_html
if not (article and article.get("contentService")):
# Sometimes the page does not have article content in the <script>
# particularly in the Sports section, so we fallback to
# raw_html and rely on remove_tags to clean it up
self.log(f"Unable to find article from script in {url}")
return raw_html
content_service = article.get("contentService")
content_node_id = None
for k, v in content_service["ROOT_QUERY"].items():
if not k.startswith("content"):
continue
content_node_id = v["id"]
break
content = content_service.get(content_node_id)
if content.get("sponsorType"):
# skip sponsored articles
err_msg = f"Sponsored article: {url}"
self.log.warning(err_msg)
self.abort_article(err_msg)
body = None
for k, v in content.items():
if (not k.startswith("body(")) or v.get("type", "") != "json":
continue
body = v
authors = [content_service[a["id"]]["name"] for a in content["authors"]]
date_published = datetime.utcfromtimestamp(
content["publishedDate"] / 1000
).replace(tzinfo=timezone.utc)
date_published_loc = date_published.astimezone(
timezone(offset=timedelta(hours=8)) # HK time
)
html_output = f"""<html><head><title>{content["headline"]}</title></head>
<body>
<article>
<h1 class="headline">{content["headline"]}</h1>
<div class="sub-headline"></div>
<div class="article-meta">
<span class="author">{", ".join(authors)}</span>
<span class="published-dt">
{date_published_loc:{get_datetime_format()}}
</span>
</div>
</article>
</body></html>
"""
new_soup = self.soup(html_output)
# sub headline
for c in content.get("subHeadline", {}).get("json", []):
ele = new_soup.new_tag(c["type"])
self._extract_child_nodes(c.get("children", []), ele, new_soup)
new_soup.find(class_="sub-headline").append(ele)
# article content
for node in body["json"]:
if node["type"] not in ["p", "div"]:
continue
new_ele = new_soup.new_tag(node["type"])
new_ele.string = ""
if node.get("children"):
self._extract_child_nodes(node["children"], new_ele, new_soup)
new_soup.article.append(new_ele)
return str(new_soup)
def populate_article_metadata(self, article, soup, _):
if (not self.pub_date) or article.utctime > self.pub_date:
self.pub_date = article.utctime
self.title = format_title(_name, article.utctime)