mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-09-29 15:31:08 -04:00
120 lines
4.5 KiB
Plaintext
120 lines
4.5 KiB
Plaintext
from calibre.web.feeds.news import BasicNewsRecipe
|
|
from collections import defaultdict
|
|
|
|
BASE = 'https://www.newsweek.com'
|
|
|
|
|
|
def href_to_url(a, add_piano=False):
|
|
return BASE + a.get('href') + ('?piano_d=1' if add_piano else '')
|
|
|
|
|
|
def class_sels(*args):
|
|
q = set(args)
|
|
return dict(attrs={'class': lambda x: x and set(x.split()).intersection(q)})
|
|
|
|
|
|
class Newsweek(BasicNewsRecipe):
|
|
|
|
title = 'Newsweek'
|
|
__author__ = 'Kovid Goyal'
|
|
description = 'Weekly news and current affairs in the US'
|
|
language = 'en'
|
|
encoding = 'utf-8'
|
|
no_stylesheets = True
|
|
requires_version = (1, 40, 0)
|
|
|
|
keep_only_tags = [
|
|
dict(id='block-nw-magazine-article-header'),
|
|
class_sels('article-header', 'article-body')
|
|
]
|
|
remove_tags = [
|
|
dict(name=['aside', 'meta', 'source']),
|
|
class_sels(
|
|
'block-openadstream', 'block-ibtmedia-social', 'issue-next',
|
|
'most-popular', 'ibt-media-stories', 'user-btn-group',
|
|
'trial-link', 'trc_related_container',
|
|
'block-ibtmedia-top-stories', 'videocontent', 'newsletter-signup',
|
|
'in-text-slideshows', 'content-correction', 'article-navigation'
|
|
),
|
|
dict(id=['taboola-below-main-column', 'piano-root',
|
|
'block-nw-magazine-magazine-more-from-issue']),
|
|
]
|
|
remove_attributes = ['style']
|
|
|
|
def parse_index(self):
|
|
root = self.index_to_soup(
|
|
'https://www.newsweek.com/archive', as_tree=True)
|
|
li = root.xpath(
|
|
'//ul[contains(@class, "magazine-archive-items")]/li')[0]
|
|
a = li.xpath('descendant::a[@href]')[0]
|
|
url = href_to_url(a, add_piano=True)
|
|
self.timefmt = self.tag_to_string(a)
|
|
img = li.xpath('descendant::a[@href]//img[@data-src]')[0]
|
|
self.cover_url = img.get('data-src').partition('?')[0]
|
|
root = self.index_to_soup(url, as_tree=True)
|
|
features = []
|
|
try:
|
|
div = root.xpath('//div[@class="magazine-features"]')[0]
|
|
except IndexError:
|
|
pass
|
|
else:
|
|
for a in div.xpath('descendant::div[@class="h1"]//a[@href]'):
|
|
title = self.tag_to_string(a)
|
|
article = a.xpath('ancestor::article')[0]
|
|
desc = ''
|
|
s = article.xpath('descendant::div[@class="summary"]')
|
|
if s:
|
|
desc = self.tag_to_string(s[0])
|
|
features.append({'title': title, 'url': href_to_url(a), 'description': desc})
|
|
self.log(title, href_to_url(a))
|
|
|
|
index = []
|
|
if features:
|
|
index.append(('Features', features))
|
|
sections = defaultdict(list)
|
|
for widget in ('editor-pick',):
|
|
self.parse_widget(widget, sections)
|
|
for k in sorted(sections):
|
|
index.append((k, sections[k]))
|
|
return index
|
|
|
|
def parse_widget(self, widget, sections):
|
|
root = self.index_to_soup('https://d.newsweek.com/widget/' + widget, as_tree=True)
|
|
div = root.xpath('//div')[0]
|
|
href_xpath = 'descendant::*[local-name()="h1" or local-name()="h2" or local-name()="h3" or local-name()="h4"]/a[@href]'
|
|
for a in div.xpath(href_xpath):
|
|
title = self.tag_to_string(a)
|
|
article = a.xpath('ancestor::article')[0]
|
|
desc = ''
|
|
s = article.xpath('descendant::div[@class="summary"]')
|
|
if s:
|
|
desc = self.tag_to_string(s[0])
|
|
sec = article.xpath('descendant::div[@class="category"]')
|
|
if sec:
|
|
sec = self.tag_to_string(sec[0])
|
|
else:
|
|
sec = 'Articles'
|
|
sections[sec].append(
|
|
{'title': title, 'url': href_to_url(a), 'description': desc})
|
|
self.log(title, href_to_url(a))
|
|
if desc:
|
|
self.log('\t' + desc)
|
|
self.log('')
|
|
|
|
def print_version(self, url):
|
|
return url + '?piano_d=1'
|
|
|
|
def preprocess_html(self, soup):
|
|
# Parallax images in the articles are loaded as background images
|
|
# on <span> tags. Convert them to normal images.
|
|
for span in soup.findAll('span', attrs={'class': lambda x: x and 'parallax' in x.split()}):
|
|
s = span.find(style=True)
|
|
if s is not None:
|
|
url = s['style'].partition('(')[-1][:-1]
|
|
s['style'] = 'display: block'
|
|
s.name = 'img'
|
|
s['src'] = url
|
|
for img in soup.findAll('img', attrs={'data-src': True}):
|
|
img['src'] = img['data-src']
|
|
return soup
|