| 1 |
#!/usr/bin/env python |
| 2 |
import random |
| 3 |
|
| 4 |
from calibre.ebooks.BeautifulSoup import BeautifulSoup |
| 5 |
from calibre.web.feeds.news import BasicNewsRecipe, prefixed_classes |
| 6 |
|
| 7 |
|
| 8 |
def resize(x): |
| 9 |
if 'resize' in x: |
| 10 |
return x.split('&resize')[0] + '&resize=600' |
| 11 |
elif '?crop=' in x: |
| 12 |
return x + '&resize=600' |
| 13 |
|
| 14 |
|
| 15 |
def absurl(url): |
| 16 |
if url.startswith('/'): |
| 17 |
url = 'https://www.thetimes.com' + url |
| 18 |
return url |
| 19 |
|
| 20 |
|
| 21 |
class times(BasicNewsRecipe): |
| 22 |
title = 'The Times and Sunday Times' |
| 23 |
__author__ = 'unkn0wn' |
| 24 |
description = ( |
| 25 |
'The Times, founded in 1785 as the Daily Universal Register, is the oldest national daily newspaper ' |
| 26 |
'in the UK and holds an important place as the “paper of record” on public life, from politics and world ' |
| 27 |
'affairs to business and sport.' |
| 28 |
) |
| 29 |
language = 'en_GB' |
| 30 |
encoding = 'utf-8' |
| 31 |
no_stylesheets = True |
| 32 |
remove_javascript = True |
| 33 |
remove_attributes = ['width', 'height', 'style'] |
| 34 |
masthead_url = 'https://www.thetimes.com/d/img/logos/times-black-ee1e0ce4ed.png' |
| 35 |
|
| 36 |
ignore_duplicate_articles = {'title', 'url'} |
| 37 |
remove_empty_feeds = True |
| 38 |
resolve_internal_links = True |
| 39 |
simultaneous_downloads = 1 |
| 40 |
browser_type = 'webengine' |
| 41 |
|
| 42 |
def get_cover_url(self): |
| 43 |
soup = self.index_to_soup('https://www.frontpages.com/uk-newspapers/') |
| 44 |
for img in soup.findAll('img', src=True): |
| 45 |
if 'the-times' in img['src']: |
| 46 |
return 'https://www.frontpages.com' + img['src'] |
| 47 |
|
| 48 |
extra_css = ''' |
| 49 |
.tc-view__TcView-nuazoi-0, [class^="keylines__KeylineItem-"], .sub { font-size:small; } |
| 50 |
[class^="responsive__StandfirstContainer-"] { font-style:italic; } |
| 51 |
''' |
| 52 |
|
| 53 |
keep_only_tags = [ |
| 54 |
prefixed_classes( |
| 55 |
'responsive__HeadlineContainer- keylines__KeylineItem- responsive__StandfirstContainer- responsive__LeadAsset- responsive__ArticleContent-' |
| 56 |
) |
| 57 |
] |
| 58 |
|
| 59 |
remove_tags = [ |
| 60 |
dict(name=['svg', 'times-datawrapper']), |
| 61 |
dict(attrs={'id': 'iframe-wrapper'}), |
| 62 |
dict(attrs={'old-position': 'sticky'}), |
| 63 |
prefixed_classes('responsive__InlineAdWrapper-'), |
| 64 |
] |
| 65 |
|
| 66 |
remove_tags_after = [dict(name='div', attrs={'id': 'paywall-portal-article-footer'})] |
| 67 |
|
| 68 |
def preprocess_html(self, soup): |
| 69 |
h2 = soup.find(**prefixed_classes('responsive__StandfirstContainer-')) |
| 70 |
if h2: |
| 71 |
h2.name = 'p' |
| 72 |
for h2 in soup.findAll('h2'): |
| 73 |
if h2.text == 'Advertisement': |
| 74 |
div = h2.findParent('div') |
| 75 |
if div: |
| 76 |
div.extract() |
| 77 |
for img in soup.findAll('img', src=True): |
| 78 |
img['src'] = resize(img['src']) |
| 79 |
for img in soup.findAll('img', attrs={'old-src': True}): |
| 80 |
img['src'] = resize(img['old-src']) |
| 81 |
for a in soup.findAll('a', href=True): |
| 82 |
a['href'] = 'http' + a['href'].split('http')[-1] |
| 83 |
div = soup.findAll(attrs={'style': lambda x: x and x.startswith('color:rgb(51, 51, 51);font-family:TimesDigitalW04-Regular')}) |
| 84 |
for p in div: |
| 85 |
p.name = 'p' |
| 86 |
for d in soup.findAll(attrs={'id': lambda x: x and '.' in x}): |
| 87 |
d['class'] = 'sub' |
| 88 |
for fig in soup.findAll('figure'): |
| 89 |
fig['class'] = 'sub' |
| 90 |
return soup |
| 91 |
|
| 92 |
def parse_index(self): |
| 93 |
soup = self.index_to_soup('https://www.thetimes.com/') |
| 94 |
main = soup.find('div', attrs={'id': 'main-container', 'data-edition-date': True}) |
| 95 |
self.timefmt = ' [%s]' % main['data-edition-date'] |
| 96 |
|
| 97 |
feeds = [] |
| 98 |
|
| 99 |
for sec in main.findAll('section', attrs={'id': lambda x: x and x.startswith('section-')}, recursive=False): |
| 100 |
section = sec['id'].replace('section-', '').capitalize() |
| 101 |
self.log(section) |
| 102 |
|
| 103 |
articles = [] |
| 104 |
|
| 105 |
for a in sec.findAll(**prefixed_classes('Item-headline')): |
| 106 |
if not a.find('a'): |
| 107 |
continue |
| 108 |
url = absurl(a.a['href']).split('?')[0] |
| 109 |
title = self.tag_to_string(a) |
| 110 |
self.log(' ', title, '\n\t', url) |
| 111 |
articles.append({'title': title, 'url': url}) |
| 112 |
feeds.append((section, articles)) |
| 113 |
return feeds |
| 114 |
|
| 115 |
def preprocess_raw_html(self, raw, url): |
| 116 |
access = '"userState":{"isLoggedIn":false,"isMetered":false,"hasAccess":true}' |
| 117 |
if access not in raw and 'comment/cartoons' not in url: |
| 118 |
dom = random.choice(('fo', 'is', 'li', 'md', 'ph', 'vn')) |
| 119 |
raw_ar = self.index_to_soup('https://archive.' + dom + '/latest/' + url) |
| 120 |
archive = BeautifulSoup(str(raw_ar)) |
| 121 |
if archive.find('div', attrs={'id': 'top'}): |
| 122 |
content = archive.find('article', attrs={'id': False}) |
| 123 |
soup = BeautifulSoup(raw) |
| 124 |
article = soup.find(**prefixed_classes('responsive__ArticleContent-')) |
| 125 |
if article and content: |
| 126 |
self.log('**fetching archive content') |
| 127 |
article.clear() |
| 128 |
article.append(content) |
| 129 |
return str(soup) |
| 130 |
return raw |
| 131 |
return raw |
| 132 |
return raw |
| 133 |
|
| 134 |
def populate_article_metadata(self, article, soup, first): |
| 135 |
desc = soup.find(**prefixed_classes('responsive__StandfirstContainer-')) |
| 136 |
if desc: |
| 137 |
article.summary = self.tag_to_string(desc) |
| 138 |
article.text_summary = article.summary |