Debian Codegraph @

recipes/the_federalist.recipe

raw
1 #!/usr/bin/env python
2 # vim:fileencoding=utf-8
3 from __future__ import absolute_import, division, print_function, unicode_literals
4
5 from calibre.web.feeds.news import BasicNewsRecipe
6
7
8 def classes(classes):
9 q = frozenset(classes.split(' '))
10 return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})
11
12
13 class Federalist(BasicNewsRecipe):
14 title = 'The Federalist'
15 __author__ = 'Kovid Goyal'
16 language = 'en'
17 oldest_article = 10
18 max_articles_per_feed = 100
19 no_stylesheets = True
20 encoding = 'utf-8'
21 use_embedded_content = False
22 remove_attributes = ['xmlns', 'lang', 'style', 'width', 'height']
23
24 extra_css = '''
25 .shortbio,.article-excerpt{font-style: italic}
26 .article-author-details,.article-author-description,.article-meta-author,.article-meta-date,.article-thumbnail-caption{font-size: small}
27 '''
28
29 keep_only_tags = [
30 classes(
31 'title-lg article-thumbnail post-categories article-excerpt article-author-details'
32 ' article-meta-author article-meta-date article-content article-body shortbio entry-header'
33 ' byline-month byline-standard alpha-byline article-author-description article-author-details'
34 ),
35 ]
36 remove_tags = [
37 dict(name=['meta', 'link']),
38 classes('auth-ad article-share article-tags attachment-post-thumbnail attachment-author-bio'),
39 ]
40
41 feeds = [
42 ('All', 'https://thefederalist.com/feed/'),
43 ]
44
45 # def parse_index(self):
46 # return [('Articles', [
47 # {
48 # 'title': 'test',
49 # 'url': 'https://thefederalist.com/2022/03/09/propaganda-press-wield-bidens-russia-blame-game-to-gaslight-americans-about-expensive-gas/'},
50 # {
51 # 'title': 'test2',
52 # 'url': 'https://thefederalist.com/2022/03/10/white-house-will-blame-anyone-but-biden-for-februarys-7-9-inflation-jump/',
53 # }
54 # ])]
55
56 def preprocess_raw_html_(self, raw_html, url):
57 soup = self.index_to_soup(raw_html)
58 # this website puts article-thumbnail images inside article-body in
59 # some articles and outside it in others, so we have to special case it
60 for ab in soup.findAll(**classes('article-body')):
61 for img in ab.findAll(**classes('article-thumbnail')):
62 del img['class']
63 return str(soup)
64
65 def preprocess_html(self, soup):
66 for img in soup.findAll('img', attrs={'data-lazy-src': True}):
67 img['src'] = img['data-lazy-src']
68 seen = set()
69 for img in soup.findAll('img', src=True):
70 src = img['src']
71 if src in seen:
72 img.extract()
73 seen.add(src)
74 return soup