| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
|
| 4 |
import json |
| 5 |
import time |
| 6 |
from datetime import datetime |
| 7 |
|
| 8 |
from calibre.ebooks.BeautifulSoup import BeautifulSoup |
| 9 |
from calibre.web.feeds.news import BasicNewsRecipe, classes |
| 10 |
|
| 11 |
|
| 12 |
def get_contents(x): |
| 13 |
if x == '': |
| 14 |
return '' |
| 15 |
otype = x.get('role', '') |
| 16 |
if otype == 'p': |
| 17 |
return '<p>' + ''.join(map(get_contents, x.get('parts', ''))) + '</p>' |
| 18 |
elif otype == 'text': |
| 19 |
if 'style' in x: |
| 20 |
return '<' + x['style'] + '>' + ''.join(map(get_contents, x.get('parts', ''))) + '</' + x['style'] + '>' |
| 21 |
return x.get('text', '') + ''.join(map(get_contents, x.get('parts', ''))) |
| 22 |
elif otype == 'br': |
| 23 |
return '<br>' |
| 24 |
elif otype == 'anchor': |
| 25 |
return '<span>' + ''.join(map(get_contents, x.get('parts', ''))) + '</span>' |
| 26 |
elif otype == 'h3': |
| 27 |
return '<h4>' + ''.join(map(get_contents, x.get('parts', ''))) + '</h4>' |
| 28 |
elif otype == 'ul': |
| 29 |
return '<ul>' + ''.join(map(get_contents, x.get('parts', ''))) + '</ul>' |
| 30 |
elif otype == 'li': |
| 31 |
return '<li>' + ''.join(map(get_contents, x.get('parts', ''))) + '</li>' |
| 32 |
elif otype == 'webview': |
| 33 |
return '<br>' + x['html'] + ''.join(map(get_contents, x.get('parts', ''))) |
| 34 |
elif otype == 'blockquote': |
| 35 |
return '<blockquote>' + ''.join(map(get_contents, x.get('parts', ''))) + '</blockquote>' |
| 36 |
elif otype in {'image', 'video'}: |
| 37 |
return '<br><img src="{}"><div class="img">{}</div>\n'.format(x['imageURLs']['default'], x['caption'] + '<i> ' + x['credit'] + '</i>') |
| 38 |
elif otype in {'correction', 'disclaimer'}: |
| 39 |
return '<p class="corr">' + ''.join(map(get_contents, x.get('parts', ''))) + '</p>' |
| 40 |
|
| 41 |
elif not any(x == otype for x in ['', 'ad', 'inline-newsletter', 'tabularData']): |
| 42 |
return '<i>' + ''.join(map(get_contents, x.get('parts', ''))) + '</i>' |
| 43 |
return '' |
| 44 |
|
| 45 |
|
| 46 |
class Bloomberg(BasicNewsRecipe): |
| 47 |
title = 'Bloomberg Businessweek' |
| 48 |
language = 'en_US' |
| 49 |
__author__ = 'unkn0wn' |
| 50 |
no_stylesheets = True |
| 51 |
remove_attributes = ['style', 'height', 'width'] |
| 52 |
encoding = 'utf-8' |
| 53 |
ignore_duplicate_articles = {'url'} |
| 54 |
masthead_url = 'https://assets.bwbx.io/s3/javelin/public/hub/images/BW-Logo-Black-cc9035fbb3.svg' |
| 55 |
description = ( |
| 56 |
'Bloomberg Businessweek helps global leaders stay ahead with insights and in-depth analysis on the people,' |
| 57 |
" companies, events, and trends shaping today's complex, global economy." |
| 58 |
) |
| 59 |
remove_empty_feeds = True |
| 60 |
|
| 61 |
recipe_specific_options = { |
| 62 |
'issue': { |
| 63 |
'short': 'The ID of the edition to download (YY_XX format)', |
| 64 |
'long': 'For example, 24_17\nHint: Edition ID can be found at the end of its URL', |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
remove_tags = [ |
| 69 |
dict(name=['button', 'svg', 'meta', 'iframe']), |
| 70 |
dict(name='div', attrs={'id': ['bb-that', 'bb-nav']}), |
| 71 |
dict(attrs={'data-image-type': 'audio'}), |
| 72 |
classes('twitter-logo bb-global-footer __sticky__audio__bar__portal__ css--social-wrapper-outer bplayer-container'), |
| 73 |
] |
| 74 |
|
| 75 |
extra_css = ''' |
| 76 |
.auth { font-size:small; font-weight:bold; } |
| 77 |
.subhead, .cap span { font-style:italic; color:#202020; } |
| 78 |
em, blockquote { color:#202020; } |
| 79 |
.cat { font-size:small; color:gray; } |
| 80 |
.img, .news-figure-caption-text { font-size:small; text-align:center; } |
| 81 |
.corr { font-size:small; font-style:italic; color:#404040; } |
| 82 |
.chart { font-size:small; } |
| 83 |
.news-figure-credit {font-size:small; text-align:center; color:#202020;} |
| 84 |
''' |
| 85 |
|
| 86 |
def parse_index(self): |
| 87 |
inx = 'https://cdn-mobapi.bloomberg.com' |
| 88 |
sec = self.index_to_soup(inx + '/wssmobile/v1/bw/news/list?limit=1', raw=True) |
| 89 |
id = json.loads(sec)['magazines'][0]['id'] |
| 90 |
past_edition = self.recipe_specific_options.get('issue') |
| 91 |
if past_edition and isinstance(past_edition, str): |
| 92 |
id = past_edition |
| 93 |
edit = self.index_to_soup(inx + '/wssmobile/v1/bw/news/week/' + id, raw=True) |
| 94 |
d = json.loads(edit) |
| 95 |
self.timefmt = ' [' + d['date'] + ']' |
| 96 |
self.cover_url = d['image']['thumbUrl'] |
| 97 |
|
| 98 |
feeds = [] |
| 99 |
|
| 100 |
for i in d['modules']: |
| 101 |
section = i['title'] |
| 102 |
self.log(section) |
| 103 |
|
| 104 |
articles = [] |
| 105 |
|
| 106 |
for x in i['articles']: |
| 107 |
title = x['title'] |
| 108 |
url = inx + '/wssmobile/v1/stories/' + x['id'] |
| 109 |
self.log('\t', title) |
| 110 |
articles.append({'title': title, 'url': url}) |
| 111 |
feeds.append((section, articles)) |
| 112 |
return feeds |
| 113 |
|
| 114 |
def preprocess_raw_html(self, raw, url): |
| 115 |
data = json.loads(raw) |
| 116 |
|
| 117 |
title = '<h1 title="{}">'.format(data['longURL']) + data['title'] + '</h1>' |
| 118 |
|
| 119 |
cat = subhead = lede = auth = caption = '' |
| 120 |
|
| 121 |
if 'primaryCategory' in data and data['primaryCategory'] is not None: |
| 122 |
cat = '<div class="cat">' + data['primaryCategory'] + '</div>' |
| 123 |
|
| 124 |
if 'abstract' in data and data['abstract'] and data['abstract'] is not None: |
| 125 |
subhead = '<div class="subhead"><ul><li>' + '</li><li>'.join(list(data['abstract'])) + '</li></ul></div>' |
| 126 |
elif data.get('summary'): |
| 127 |
subhead = '<div class="subhead"><p>' + data['summary'] + '</p></div>' |
| 128 |
|
| 129 |
if 'byline' in data and data['byline'] is not None: |
| 130 |
dt = datetime.fromtimestamp(data['updatedAt'] + time.timezone) |
| 131 |
auth = '<p class="auth">' + 'By ' + data['byline'] + ' | Updated on ' + dt.strftime('%b %d, %Y at %I:%M %p') + '</p>' |
| 132 |
|
| 133 |
body = '' |
| 134 |
if data.get('type', '') == 'interactive': |
| 135 |
body += '<p><em>' + 'This is an interactive article, which is supposed to be read in a browser.' + '</p></em>' |
| 136 |
# body_data = data['components'] |
| 137 |
# for x in body_data: |
| 138 |
# body += get_contents(x) |
| 139 |
|
| 140 |
b_data = self.index_to_soup('https://cdn-mobapi.bloomberg.com/wssmobile/v1/bw/news/stories/' + url.split('/')[-1], raw=True) |
| 141 |
body += json.loads(b_data)['html'] |
| 142 |
|
| 143 |
if 'ledeImage' in data and data['ledeImage'] is not None: |
| 144 |
x = data['ledeImage'] |
| 145 |
if x['imageURLs']['default'].rsplit('/', 1)[0] not in body: |
| 146 |
lede = '<br><img src="{}"><div class="img">{}</div>\n'.format(x['imageURLs']['default'], x['caption'] + '<i> ' + x['credit'] + '</i>') |
| 147 |
|
| 148 |
html = '<html><body>' + cat + title + subhead + auth + lede + caption + '<div>' + body + '</div></body></html>' |
| 149 |
return BeautifulSoup(html).prettify() |
| 150 |
|
| 151 |
def preprocess_html(self, soup): |
| 152 |
for h3 in soup.findAll(['h2', 'h3']): |
| 153 |
h3.name = 'h4' |
| 154 |
for icon in soup.findAll('img', attrs={'class': 'video-player__play-icon'}): |
| 155 |
icon.decompose() |
| 156 |
for div in soup.findAll('div', attrs={'class': 'chart'}): |
| 157 |
nos = div.find('noscript') |
| 158 |
if nos: |
| 159 |
nos.name = 'span' |
| 160 |
for img in soup.findAll('img', attrs={'data-native-src': True}): |
| 161 |
if img['data-native-src'].__contains__('videos') is False: |
| 162 |
img['src'] = img['data-native-src'] |
| 163 |
else: |
| 164 |
img['src'] = '' |
| 165 |
for img in soup.findAll('img', attrs={'src': lambda x: x and x.endswith(('-1x-1.jpg', '-1x-1.png'))}): |
| 166 |
img['src'] = img['src'].replace('-1x-1', '750x-1') |
| 167 |
return soup |
| 168 |
|
| 169 |
def get_browser(self, *args, **kwargs): |
| 170 |
kwargs['user_agent'] = 'BloombergHorseshoe|Android|16|6.66.0' |
| 171 |
br = BasicNewsRecipe.get_browser(self, *args, **kwargs) |
| 172 |
return br |
| 173 |
|
| 174 |
def populate_article_metadata(self, article, soup, first): |
| 175 |
article.url = soup.find('h1')['title'] |
| 176 |
article.summary = self.tag_to_string(soup.find('div', attrs={'class': 'subhead'})) |
| 177 |
article.text_summary = article.summary |