| 1 |
#!/usr/bin/env python |
| 2 |
# vim:fileencoding=utf-8 |
| 3 |
import json |
| 4 |
import re |
| 5 |
from urllib.parse import quote |
| 6 |
|
| 7 |
from html5_parser import parse |
| 8 |
|
| 9 |
from calibre import browser |
| 10 |
from calibre.web.feeds.news import BasicNewsRecipe, classes |
| 11 |
|
| 12 |
|
| 13 |
class ft(BasicNewsRecipe): |
| 14 |
title = 'Financial Times' |
| 15 |
language = 'en_GB' |
| 16 |
__author__ = 'Kovid Goyal, unkn0wn' |
| 17 |
description = 'The Financial Times is one of the world’s leading news organisations, recognised internationally for its authority, integrity and accuracy.' |
| 18 |
oldest_article = 1.15 |
| 19 |
max_articles_per_feed = 50 |
| 20 |
no_stylesheets = True |
| 21 |
remove_javascript = True |
| 22 |
remove_empty_feeds = True |
| 23 |
ignore_duplicate_articles = {'url'} |
| 24 |
resolve_internal_links = True |
| 25 |
remove_attributes = ['style', 'width', 'height'] |
| 26 |
masthead_url = 'https://im.ft-static.com/m/img/masthead_main.jpg' |
| 27 |
simultaneous_downloads = 1 |
| 28 |
delay = 1 |
| 29 |
|
| 30 |
extra_css = ''' |
| 31 |
.article-info__time-byline {font-size:small; font-weight:bold; } |
| 32 |
.o-topper__visual, #fig, .main-image, .n-content-image { text-align:center; font-size:small; } |
| 33 |
blockquote, i { color:#5c5c5c; } |
| 34 |
.o-topper__standfirst { font-style:italic; color:#202020; } |
| 35 |
.o-topper__topic, .article-info__time-byline-content { font-size:small; color:#5c5c5c; } |
| 36 |
''' |
| 37 |
|
| 38 |
recipe_specific_options = { |
| 39 |
'days': { |
| 40 |
'short': 'Oldest article to download from this news source. In days ', |
| 41 |
'long': 'For example, 0.5, gives you articles from the past 12 hours', |
| 42 |
'default': str(oldest_article), |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
def __init__(self, *args, **kwargs): |
| 47 |
BasicNewsRecipe.__init__(self, *args, **kwargs) |
| 48 |
d = self.recipe_specific_options.get('days') |
| 49 |
if d and isinstance(d, str): |
| 50 |
self.oldest_article = float(d) |
| 51 |
|
| 52 |
keep_only_tags = [ |
| 53 |
classes('body_json o-topper__topic o-topper__headline o-topper__standfirst article-info__time-byline-content o-topper__visual main-image'), |
| 54 |
dict(name='article', attrs={'id': 'article-body'}), |
| 55 |
] |
| 56 |
|
| 57 |
remove_tags = [ |
| 58 |
dict(name=['source', 'svg', 'button', 'aside']), |
| 59 |
dict(name='aside', attrs={'class': 'n-content-recommended--single-story'}), |
| 60 |
dict(attrs={'data-layout-name': 'card'}), |
| 61 |
classes('in-article-advert flourish-disclaimer n-myft-ui__preferences-modal n-myft-ui n-myft-ui--follow'), |
| 62 |
] |
| 63 |
|
| 64 |
def get_cover_url(self): |
| 65 |
soup = self.index_to_soup('https://www.frontpages.com/financial-newspapers/') |
| 66 |
for img in soup.findAll('img', src=True): |
| 67 |
if 'financial-times' in img['src']: |
| 68 |
return 'https://www.frontpages.com' + img['src'] |
| 69 |
|
| 70 |
# needs_subscription = 'optional' |
| 71 |
# |
| 72 |
# def get_browser(self, *args, **kw): |
| 73 |
# br = super().get_browser(*args, **kw) |
| 74 |
# if self.username and self.password: |
| 75 |
# # ft.com uses a CAPTCHA on its login page so this sadly doesn't work |
| 76 |
# br.open('https://accounts.ft.com/login?location=https%3A%2F%2Fwww.ft.com') |
| 77 |
# br.select_form(id='email-form') |
| 78 |
# br['email'] = self.username |
| 79 |
# br.submit() |
| 80 |
# br.select_form(id='login-form') |
| 81 |
# br['password'] = self.password |
| 82 |
# br.submit() |
| 83 |
# return br |
| 84 |
|
| 85 |
def get_browser(self, *args, **kwargs): |
| 86 |
return self |
| 87 |
|
| 88 |
def clone_browser(self, *args, **kwargs): |
| 89 |
return self.get_browser() |
| 90 |
|
| 91 |
def open_novisit(self, *args, **kwargs): |
| 92 |
br = browser(user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:149.0) Gecko/20100101 Firefox/149.0') |
| 93 |
br.addheaders += [('Referer', 'https://www.google.com/')] |
| 94 |
return br.open_novisit(*args, **kwargs) |
| 95 |
|
| 96 |
open = open_novisit |
| 97 |
|
| 98 |
feeds = [ |
| 99 |
('FirstFT', 'https://www.ft.com/firstft?format=rss'), |
| 100 |
('Opinion', 'https://www.ft.com/opinion?format=rss'), |
| 101 |
('World', 'https://www.ft.com/world?format=rss'), |
| 102 |
('US', 'https://www.ft.com/us?format=rss'), |
| 103 |
('Companies', 'https://www.ft.com/companies?format=rss'), |
| 104 |
('Tech', 'https://www.ft.com/technology?format=rss'), |
| 105 |
('Markets', 'https://www.ft.com/markets?format=rss'), |
| 106 |
('Climate', 'https://www.ft.com/climate-capital?format=rss'), |
| 107 |
('Life & Arts', 'https://www.ft.com/life-arts?format=rss'), |
| 108 |
('How to spend it', 'https://www.ft.com/htsi?format=rss'), |
| 109 |
('Others', 'https://www.ft.com/rss/home/uk'), |
| 110 |
] |
| 111 |
|
| 112 |
def preprocess_raw_html(self, raw, *a): |
| 113 |
# with open('/t/raw.html', 'w') as f: |
| 114 |
# f.write(raw) |
| 115 |
root = parse(raw) |
| 116 |
if root.xpath('//article[@id="article-body"]'): |
| 117 |
self.log('**has article content') |
| 118 |
return raw |
| 119 |
self.log('**no article content') |
| 120 |
m = re.search(r'type="application/ld\+json">[^<]+?"@type":"NewsArticle"', raw) |
| 121 |
raw = raw[m.start() :] |
| 122 |
raw = raw.split('>', 1)[1] |
| 123 |
# with open('/t/raw.json', 'w') as f: |
| 124 |
# f.write(raw) |
| 125 |
data = json.JSONDecoder().raw_decode(raw)[0] |
| 126 |
title = data['headline'] |
| 127 |
body = data['articleBody'] |
| 128 |
body = body.replace('\n\n', '<p>') |
| 129 |
author = '' |
| 130 |
if 'author' in data: |
| 131 |
try: |
| 132 |
author = data['author']['name'] |
| 133 |
except TypeError: |
| 134 |
author = ' and '.join(x['name'] for x in data['author']) |
| 135 |
image = desc = title_image_url = '' |
| 136 |
|
| 137 |
def resize_img(img): |
| 138 |
a = 'https://www.ft.com/__origami/service/image/v2/images/raw/' |
| 139 |
b = quote(img, safe='') |
| 140 |
c = '?dpr=2&fit=scale-down&quality=medium&source=next&width=400' |
| 141 |
# use width = 200, 300, 400,.. 700... |
| 142 |
return a + b + c |
| 143 |
|
| 144 |
if data.get('image'): |
| 145 |
image_url = data['image']['url'] |
| 146 |
if body.__contains__(image_url) is False: |
| 147 |
title_image_url = resize_img(image_url) |
| 148 |
image = '<p><img src="{}">'.format(title_image_url) |
| 149 |
# embedded image links |
| 150 |
|
| 151 |
def insert_image(m): |
| 152 |
url = m.group()[1:-1] |
| 153 |
if url.__contains__('studio') is False: |
| 154 |
url = resize_img(url) |
| 155 |
return '<span><img src="{}"></span></p><p>'.format(url) |
| 156 |
|
| 157 |
body = re.sub(r'\[https://\S+?\]', insert_image, body) |
| 158 |
if data.get('description'): |
| 159 |
desc = '<h2>' + data['description'] + '</h2>' |
| 160 |
html = '<html><body><div class="body_json"><h1>' + title + '</h1>' + desc + '<h3>' + author + '</h3>' + image + '<p>' + body |
| 161 |
return html |
| 162 |
|
| 163 |
def preprocess_html(self, soup): |
| 164 |
p = soup.find(**classes('o-topper__standfirst')) |
| 165 |
if p: |
| 166 |
p.name = 'p' |
| 167 |
div = soup.findAll(**classes('article-info__time-byline-content')) |
| 168 |
for d in div: |
| 169 |
for p_ in d.findAll('p'): |
| 170 |
p_.name = 'div' |
| 171 |
for tim in soup.findAll('time'): |
| 172 |
if tim.string: |
| 173 |
tim.string = ' ' + tim.string |
| 174 |
for table in soup.findAll('table'): |
| 175 |
if len(table.find('tbody').findAll('tr')) > 20: |
| 176 |
table.find('tbody').decompose() |
| 177 |
table.string = '** a table that was supposed to be here has been removed.' |
| 178 |
for con in soup.findAll(attrs={'class': 'n-content-layout__slot'}): |
| 179 |
if con.find('figure'): |
| 180 |
con['id'] = 'fig' |
| 181 |
return soup |