forked from ping/newsrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguardian.recipe.py
196 lines (176 loc) · 7.38 KB
/
guardian.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
# Copyright (c) 2022 https://github.com/ping/
#
# This software is released under the GNU General Public License v3.0
# https://opensource.org/licenses/GPL-3.0
"""
guardian.com
"""
import json
import os
import sys
# custom include to share code between recipes
sys.path.append(os.environ["recipes_includes"])
from recipes_shared import BasicNewsrackRecipe, format_title
from calibre.web.feeds.news import BasicNewsRecipe
_name = "Guardian"
class Guardian(BasicNewsrackRecipe, BasicNewsRecipe):
title = _name
description = "Latest international news, sport and comment from the Guardian https://www.theguardian.com/international"
language = "en_GB"
__author__ = "ping"
publication_type = "newspaper"
oldest_article = 1 # days
max_articles_per_feed = 60
masthead_url = "https://assets.guim.co.uk/images/guardian-logo-rss.c45beb1bafa34b347ac333af2e6fe23f.png"
remove_attributes = ["style", "width", "height"]
keep_only_tags = [dict(name=["header", "article"])]
remove_tags = [
dict(name=["svg", "input", "button", "label"]),
dict(id=["bannerandheader", "the-caption", "liveblog-navigation"]),
dict(
class_=[
"skip",
"meta__social",
"live-blog__filter-switch",
"ad-slot",
"l-footer",
"is-hidden",
"js-most-popular-footer",
"submeta",
"block-share",
"content-footer",
]
),
dict(name="div", attrs={"data-print-layout": "hide"}),
dict(attrs={"name": "FilterKeyEventsToggle"}),
dict(attrs={"aria-hidden": "true"}),
dict(
name="time", attrs={"data-relativeformat": "med"}
), # remove the relative timestamp, e.g. 8h ago
dict(attrs={"data-component": ["podcast-help", "nav2", "SupportTheG"]}),
dict(
attrs={
"data-spacefinder-type": "model.dotcomrendering.pageElements.NewsletterSignupBlockElement"
}
),
dict(
name="gu-island",
attrs={"name": ["HeaderTopBar", "Carousel", "GuideAtomWrapper"]},
),
dict(attrs={"data-link-name": "nav3 : logo"}),
]
extra_css = """
[data-gu-name="headline"] h1 { font-size: 1.8rem; margin-bottom: 0.4rem; }
[data-gu-name="standfirst"] p { font-size: 1.2rem; font-style: italic; margin-top: 0; margin-bottom: 1rem; }
[data-component="series"], [data-component="section"] { margin-right: 0.5rem; }
[data-gu-name="meta"] { margin-bottom: 1.5rem; }
[data-component="meta-byline"] {
margin-top: 1rem; margin-bottom: 1rem;
font-weight: bold; color: #444; font-style: normal;
}
[data-component="meta-byline"] div { display: inline-block; margin-right: 0.5rem; }
[data-component="meta-byline"] a { color: #444; }
*[data-gu-name="media"] span, *[item-prop="description"],
div[data-spacefinder-type$=".ImageBlockElement"] > div,
div.caption { font-size: 0.8rem; margin-bottom: 0.5rem; }
img { max-width: 100%; height: auto; margin-bottom: 0.2rem; }
[data-name="placeholder"] { color: #444; font-style: italic; }
[data-name="placeholder"] a { color: #444; }
blockquote { font-size: 1.2rem; color: #222; margin-left: 0; text-align: center; }
time { margin-right: 0.5rem; }
.embed { color: #444; font-size: 0.8rem; }
"""
feeds = [
(_name, "https://www.theguardian.com/international/rss"),
]
def preprocess_html(self, soup):
live_blog = self.get_ld_json(
soup, lambda d: d[0].get("@type", "") == "LiveBlogPosting"
)
if live_blog:
err_msg = "Exclude live postings"
self.log.warning(err_msg)
self.abort_article(err_msg)
meta = soup.find(attrs={"data-gu-name": "meta"})
if meta:
# remove author image
for img in meta.find_all("img"):
img.decompose()
# reformat the displayed date
details = meta.find_all("details")
for detail in details:
summary = detail.find("summary")
update_date = None
if len(detail.contents) > 1:
update_date = detail.contents[1]
published = soup.new_tag("div", attrs={"class": "published-date"})
published.append(summary.string)
detail.clear()
detail.append(published)
if update_date:
update = soup.new_tag("div", attrs={"class": "last-updated-date"})
update.append(update_date)
detail.append(update)
detail.name = "div"
detail["class"] = "meta-date"
# re-position lede image
media = soup.find(attrs={"data-gu-name": "media"})
if media and meta:
meta.insert_after(media.extract())
# search for highest resolution image
for picture in soup.find_all("picture"):
source = picture.find("source") # use first one
if not source:
continue
max_img_url = source["srcset"]
for source in picture.find_all("source"):
source.decompose()
img = picture.find("img")
if not img:
img = soup.new_tag("img", attrs={"class": "custom-added"})
picture.append(img)
img["src"] = max_img_url
# remove share on social media links for live articles
for unordered_list in soup.find_all("ul"):
is_social_media = False
for list_item in unordered_list.find_all("li"):
a_link = list_item.find("a", attrs={"aria-label": True})
if a_link and a_link["aria-label"] in [
"Share on Facebook",
"Share on Twitter",
]:
is_social_media = True
break
if is_social_media:
unordered_list.decompose()
# Patch Key Events ul in live articles
# The div forces a linebreak in the li > a looks bad in the Kindle
for unordered_list in soup.find_all("ul"):
for link_item in unordered_list.find_all("a"):
if link_item["href"].startswith("?filterKeyEvents"):
link_text = self.tag_to_string(link_item)
link_item.parent.string = link_text
# embed YT blocks
for yt in soup.find_all(
attrs={
"data-spacefinder-type": "model.dotcomrendering.pageElements.YoutubeBlockElement"
}
):
info_ele = yt.find(name="gu-island")
if not info_ele:
continue
info = json.loads(info_ele["props"])
link = f'https://www.youtube.com/watch?v={info["assetId"]}'
yt.clear()
yt["class"] = "embed"
yt.append(f'{info["mediaTitle"]} ')
a_link = soup.new_tag("a", href=link)
a_link.append(link)
yt.append(a_link)
return soup
def populate_article_metadata(self, article, __, _):
if (not self.pub_date) or article.utctime > self.pub_date:
self.pub_date = article.utctime
self.title = format_title(_name, article.utctime)
def parse_feeds(self):
return self.group_feeds_by_date(timezone_offset_hours=1) # UK time