forked from kovidgoyal/calibre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloomberg.recipe
209 lines (183 loc) · 9.85 KB
/
bloomberg.recipe
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
import json
import random
import time
from calibre.ptempfile import PersistentTemporaryFile
from calibre.web.feeds.news import BasicNewsRecipe
from html5_parser import parse
def get_contents(x):
if x == '':
return ''
otype = x.get('type', '')
if otype == 'text':
if 'attributes' in x:
if 'strong' in x['attributes']:
return '<strong>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</strong>'
if 'emphasis' in x['attributes']:
return '<em>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</em>'
return '<i>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</i>'
return x.get('value', '') + ''.join(map(get_contents, x.get('content', '')))
elif otype == 'br':
return '<br>'
elif otype == 'paragraph':
return '<p>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</p>'
elif otype == 'heading':
return '<h3>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</h3>'
elif otype == 'list':
return '<ul>' + ''.join(map(get_contents, x.get('content', ''))) + '</ul>'
elif otype == 'listItem':
return '<li>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</li>'
elif otype == 'quote':
return '<blockquote class="col">' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</blockquote>'
elif otype == 'media':
if x['subType'] == 'photo':
return '<div><div class="img"><img src="{}"></div><div class="cap">{} <span>{}</span></div></div>'.format(
x['data']['photo']['src'], x['data']['photo']['caption'], x['data']['photo']['credit'])
elif x['subType'] == 'chart':
if x['data'] and x['data']['chart']:
return '<div class="img"><img src="{}"></div>'.format(x['data']['chart']['fallback'])
elif otype == 'link':
if 'data' in x:
if 'href' in x['data']:
return '<a href="' + x['data']['href'] + '">' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</a>'
return '<i>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</i>'
return '<i>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</i>'
elif otype == 'entity':
if x['subType'] == 'story':
if x['data'] and x['data']['link'] and x['data']['link']['destination']:
if 'web' in x['data']['link']['destination']:
return '<a href="' + x['data']['link']['destination']['web'] + '">' + x.get('value', '') + ''.join(
map(get_contents, x.get('content', ''))) + '</a>'
return '<i>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</i>'
return '<i>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</i>'
return '<i>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</i>'
elif otype in {'div', 'callout'}:
return '<div>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</div>'
elif not any(x == otype for x in ['', 'ad', 'inline-newsletter', 'tabularData']):
if any(b in x for b in ['value', 'content']):
return '<i>' + x.get('value', '') + ''.join(map(get_contents, x.get('content', ''))) + '</i>'
return ''
class Bloomberg(BasicNewsRecipe):
title = u'Bloomberg'
language = 'en'
__author__ = 'unkn0wn'
no_stylesheets = True
use_embedded_content = False
remove_attributes = ['style', 'height', 'width']
ignore_duplicate_articles = {'url', 'title'}
masthead_url = 'https://assets.bbhub.io/company/sites/70/2022/09/logoBBGblck.svg'
description = ('Bloomberg delivers business and markets news, data, analysis, and video'
' to the world, featuring stories from Businessweek and Bloomberg News.')
simultaneous_downloads = 1
extra_css = '''
.auth {font-size:small; font-weight:bold;}
.time, .chart {font-size:small;}
.subhead, .cap span {font-style:italic; color:#404040;}
em, .col {color:#202020;}
.cat {font-size:small; color:gray;}
.news-figure-caption-text, .cap, .img {font-size:small; text-align:center;}
.news-figure-credit {font-size:small; text-align:center; color:#202020;}
'''
articles_are_obfuscated = True
resolve_internal_links = True
def get_obfuscated_article(self, url):
br = self.get_browser()
try:
br.open(url)
except Exception as e:
url = e.hdrs.get('location')
soup = self.index_to_soup(url)
link = soup.find('a', attrs={'href':lambda x: x and x.startswith('https://www.bloomberg.com')})
skip_sections =[ # add sections you want to skip
'/video/', '/videos/', '/media/', 'podcast'
]
if any(x in link['href'] for x in skip_sections):
self.abort_article('Aborting Video article')
self.log('Found link: ', link['href'])
html = br.open(link['href']).read()
pt = PersistentTemporaryFile('.html')
pt.write(html)
pt.close()
return pt.name
def get_browser(self, *a, **kw):
kw['user_agent'] = 'Mozilla/5.0 (Windows NT 10.0; rv:109.0) Gecko/20100101 Firefox/119.0'
br = BasicNewsRecipe.get_browser(self, *a, **kw)
br.set_handle_redirect(False)
return br
feeds = [
('Features',
'https://news.google.com/rss/search?q=when:27h+allinurl:bloomberg.com%2Fnews%2Ffeatures%2F&hl=en-US&gl=US&ceid=US:en'),
('Opinion', 'https://news.google.com/rss/search?q=when:27h+allinurl:bloomberg.com%2Fopinion%2F&hl=en-US&gl=US&ceid=US:en'),
('Newsletters',
'https://news.google.com/rss/search?q=when:27h+allinurl:bloomberg.com%2Fnews%2Fnewsletters%2F&hl=en-US&gl=US&ceid=US:en'),
('News',
'https://news.google.com/rss/search?q=when:27h+allinurl:bloomberg.com%2Fnews%2Farticles%2F&hl=en-US&gl=US&ceid=US:en'),
('Others', 'https://news.google.com/rss/search?q=when:27h+allinurl:https%3A%2F%2Fwww.bloomberg.com&hl=en-US&gl=US&ceid=US:en')
]
def preprocess_raw_html(self, raw, *a):
root = parse(raw)
m = root.xpath('//script[@data-component-props="ArticleBody"]')
if not m:
m = root.xpath('//script[@data-component-props="FeatureBody"]')
if not m:
m2 = root.xpath('//script[@id="__NEXT_DATA__"]')
if m:
data = json.loads(m[0].text)
data = data['story']
elif m2:
data = json.loads(m2[0].text)
data = data['props']['pageProps']['story']
art_url = data['url']
if not art_url.startswith('http'):
art_url = 'https://www.bloomberg.com' + art_url
title = '<h1 title="{}">'.format(art_url) + data['headline'] + '</h1>'
cat = subhead = lede = auth = caption = ''
if 'primaryCategory' in data and data['primaryCategory'] is not None:
cat = '<p class="cat">' + data['primaryCategory'] + '</p>'
if len(data['abstract']) != 0 and len(data['abstract']) == 2:
subhead = '<div class="subhead"><p>' + data['abstract'][0] + ' </p><p>' + data['abstract'][1] + '</p></div>'
else:
if 'summary' in data:
subhead = '<div class="subhead"><p>' + data['summary'] + '</p></div>'
if 'byline' in data and data['byline'] is not None:
auth = '<div><span class="auth">' + data['byline']\
+ '</span> | <span class="time">' + data['publishedAt'][:-14] + '</span></div>'
if 'ledeImageUrl' in data and data['ledeImageUrl'] is not None:
lede = '<p class="img"><img src="{}">'.format(data['ledeImageUrl'])
if 'ledeDescription' in data and data['ledeDescription'] is not None:
caption = '<span class="cap">' + data['ledeDescription'] + '</span>'
else:
if 'lede' in data and data['lede'] is not None:
if 'alt' in data['lede'] and data['lede']['alt'] is not None:
caption = '<span class="cap">' + data['lede']['alt'] + '</span>'
if m:
time.sleep(3)
body = data['body']
elif m2:
body = ''
body_data = data['body']['content']
for x in body_data:
body += get_contents(x)
pause = random.choice((5, 6, 7, 8, 9))
self.log('Delay: ', pause, ' seconds')
time.sleep(pause)
return '<html><body>' + cat + title + subhead + auth + lede + caption + '<div>' + body + '</div></body></html>'
def preprocess_html(self, soup):
for icon in soup.findAll('img', attrs={'class':'video-player__play-icon'}):
icon.decompose()
for div in soup.findAll('div', attrs={'class':'chart'}):
nos = div.find('noscript')
if nos:
nos.name = 'span'
for img in soup.findAll('img', attrs={'data-native-src':True}):
if img['data-native-src'].__contains__('videos') is False:
img['src'] = img['data-native-src']
else:
img['src'] = ''
for img in soup.findAll('img', attrs={'src':lambda x: x and x.endswith(('-1x-1.jpg', '-1x-1.png'))}):
img['src'] = img['src'].replace('-1x-1', '750x-1')
return soup
def populate_article_metadata(self, article, soup, first):
article.url = soup.find('h1')['title']
article.summary = self.tag_to_string(soup.find('div', attrs={'class':'subhead'}))
article.text_summary = self.tag_to_string(soup.find('div', attrs={'class':'subhead'}))
article.title = article.title.replace(' - Bloomberg', '')