| 1 |
#!/usr/bin/env python |
| 2 |
""" |
| 3 |
https://www.economist.com/the-world-in-brief |
| 4 |
""" |
| 5 |
|
| 6 |
import re |
| 7 |
|
| 8 |
from calibre.ebooks.BeautifulSoup import Tag |
| 9 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 10 |
|
| 11 |
|
| 12 |
def new_tag(soup, name, attrs=()): |
| 13 |
impl = getattr(soup, 'new_tag', None) |
| 14 |
if impl is not None: |
| 15 |
return impl(name, attrs=dict(attrs)) |
| 16 |
return Tag(soup, name, attrs=attrs or None) |
| 17 |
|
| 18 |
|
| 19 |
class Espresso(BasicNewsRecipe): |
| 20 |
title = 'The Economist Espresso' |
| 21 |
language = 'en_GB' |
| 22 |
__author__ = 'unkn0wn' |
| 23 |
description = ( |
| 24 |
'Espresso is a rich, full-flavoured shot of daily global analysis ' |
| 25 |
'from the editors of The Economist to get you up to speed, fast. ' |
| 26 |
'Maximise your understanding of the most significant business, ' |
| 27 |
'economic, political and cultural developments globally.' |
| 28 |
) |
| 29 |
cover_url = 'https://downloadr2.apkmirror.com/wp-content/uploads/2021/10/75/615777cc6611b.png' |
| 30 |
no_stylesheets = True |
| 31 |
remove_attributes = ['height', 'width'] |
| 32 |
use_embedded_content = False |
| 33 |
masthead_url = 'https://www.livemint.com/lm-img/dev/economist-logo-oneline.png' |
| 34 |
browser_type = 'webengine' |
| 35 |
|
| 36 |
extra_css = ''' |
| 37 |
h1 { text-align:center; } |
| 38 |
figcaption, figcaption span { text-align:center; font-size:small; color:#606060; } |
| 39 |
blockquote { font-size:x-large; font-style:italic; color:#202020; } |
| 40 |
figure { text-align:center; } |
| 41 |
img { display:block; margin:0 auto; max-width:100%; } |
| 42 |
''' |
| 43 |
|
| 44 |
keep_only_tags = [dict(name='main', attrs={'id': 'content'})] |
| 45 |
|
| 46 |
remove_tags = [ |
| 47 |
dict(attrs={'data-testid': 'twib-page-navigation-bar'}), |
| 48 |
dict(attrs={'class': lambda c: c and 'adComponent' in (' '.join(c) if isinstance(c, list) else c)}), |
| 49 |
dict(attrs={'class': lambda c: c and 'teaser' in (' '.join(c) if isinstance(c, list) else c)}), |
| 50 |
dict(name='time'), |
| 51 |
dict(attrs={'data-testid': 'progress-bar'}), |
| 52 |
dict(attrs={'data-testid': 'rule-standard'}), |
| 53 |
] |
| 54 |
|
| 55 |
def parse_index(self): |
| 56 |
soup = self.index_to_soup('https://www.economist.com/the-world-in-brief') |
| 57 |
articles = [] |
| 58 |
|
| 59 |
main_title = 'The World in Brief' |
| 60 |
title_tag = soup.find('title', attrs={'data-next-head': True}) |
| 61 |
if title_tag: |
| 62 |
main_title = self.tag_to_string(title_tag).strip() |
| 63 |
|
| 64 |
articles.append({ |
| 65 |
'title': main_title, |
| 66 |
'url': 'https://www.economist.com/the-world-in-brief', |
| 67 |
'description': 'Catch up quickly on the global stories that matter', |
| 68 |
}) |
| 69 |
|
| 70 |
visited = {'https://www.economist.com/the-world-in-brief'} |
| 71 |
current_soup = soup |
| 72 |
|
| 73 |
for _ in range(20): |
| 74 |
next_link = current_soup.find('a', attrs={'aria-label': 'Go to next page'}) |
| 75 |
if not next_link or not next_link.get('href'): |
| 76 |
break |
| 77 |
next_url = 'https://www.economist.com' + next_link['href'] |
| 78 |
if next_url in visited: |
| 79 |
break |
| 80 |
visited.add(next_url) |
| 81 |
|
| 82 |
try: |
| 83 |
next_soup = self.index_to_soup(next_url) |
| 84 |
except Exception: |
| 85 |
break |
| 86 |
|
| 87 |
title = next_url |
| 88 |
title_tag = next_soup.find('title', attrs={'data-next-head': True}) |
| 89 |
if title_tag: |
| 90 |
title = self.tag_to_string(title_tag).strip() |
| 91 |
|
| 92 |
articles.append({'title': title, 'url': next_url}) |
| 93 |
current_soup = next_soup |
| 94 |
|
| 95 |
return [('Espresso', articles)] |
| 96 |
|
| 97 |
def preprocess_html(self, soup): |
| 98 |
for style in soup.findAll('style'): |
| 99 |
style.decompose() |
| 100 |
|
| 101 |
for figcaption in soup.findAll('figcaption'): |
| 102 |
text = figcaption.get_text(separator=' ', strip=True) |
| 103 |
figcaption.clear() |
| 104 |
figcaption['style'] = 'text-align:center; font-size:small; color:#606060;' |
| 105 |
figcaption.append(text) |
| 106 |
|
| 107 |
for tag in soup.findAll(True): |
| 108 |
if tag.name not in ('figcaption', 'figure', 'img') and tag.get('style'): |
| 109 |
del tag['style'] |
| 110 |
|
| 111 |
for img in soup.findAll('img', src=True): |
| 112 |
img['src'] = re.sub(r'width=\d+', 'width=600', img['src']) |
| 113 |
img['style'] = 'display:block; margin:0 auto; max-width:100%;' |
| 114 |
figure = img.find_parent('figure') |
| 115 |
if figure: |
| 116 |
figure['style'] = 'text-align:center;' |
| 117 |
|
| 118 |
for p in soup.findAll('p', attrs={'data-component': 'paragraph'}): |
| 119 |
if p.find('a', href=lambda h: h and 'newsletter' in h): |
| 120 |
p.decompose() |
| 121 |
|
| 122 |
for audio in soup.findAll('audio'): |
| 123 |
parent = audio.find_parent('figure') |
| 124 |
if parent: |
| 125 |
grandparent = parent.find_parent('div') |
| 126 |
if grandparent: |
| 127 |
grandparent.decompose() |
| 128 |
else: |
| 129 |
parent.decompose() |
| 130 |
else: |
| 131 |
audio.decompose() |
| 132 |
|
| 133 |
return soup |
| 134 |
|
| 135 |
def get_browser(self, *args, **kwargs): |
| 136 |
from calibre.utils.random_ua import common_english_word_ua |
| 137 |
|
| 138 |
kwargs['user_agent'] = common_english_word_ua() |
| 139 |
return BasicNewsRecipe.get_browser(self, *args, **kwargs) |