Debian Codegraph @

recipes/nzherald.recipe

raw
1 #!/usr/bin/env python
2 from calibre.web.feeds.recipes import BasicNewsRecipe
3
4
5 def classes(classes):
6 q = frozenset(classes.split(' '))
7 return dict(attrs={'class': lambda x: x and frozenset(x.split()).intersection(q)})
8
9
10 class NewZealandHerald(BasicNewsRecipe):
11 title = 'New Zealand Herald'
12 __author__ = 'unkn0wn'
13 description = 'Daily news'
14 timefmt = ' [%d %b, %Y]'
15 language = 'en_NZ'
16 oldest_article = 1
17 remove_attributes = ['style', 'height', 'width']
18 use_embedded_content = False
19 encoding = 'utf-8'
20 ignore_duplicate_articles = {'url'}
21 no_stylesheets = True
22 resolve_internal_links = True
23 remove_empty_feeds = True
24
25 def get_cover_url(self):
26 soup = self.index_to_soup('https://www.frontpages.com/new-zealand-newspapers/')
27 for img in soup.findAll('img', src=True):
28 if 'new-zealand-herald' in img['src']:
29 return 'https://www.frontpages.com' + img['src']
30
31 extra_css = '''
32 [data-test-ui="author--text--body"], .article-media__caption {font-size: small;}
33 '''
34
35 keep_only_tags = [
36 dict(
37 attrs={
38 'data-test-ui': [
39 'article__heading',
40 'author--text--body',
41 'article-top-body',
42 'article-bottom-body',
43 ]
44 }
45 ),
46 ]
47
48 remove_tags = [classes('related-articles article__ad-wrapper article__action-bar')]
49
50 feeds = [
51 ('Business', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000003.xml'),
52 ('World', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000002.xml'),
53 ('National', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000001.xml'),
54 ('Entertainment', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_001501119.xml'),
55 ('Travel', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000007.xml'),
56 ('Opinion', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000466.xml'),
57 ('Life & Style', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000006.xml'),
58 ('Technology', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000005.xml'),
59 ('Sport', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000004.xml'),
60 ('Motoring', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000009.xml'),
61 ('Property', 'http://rss.nzherald.co.nz/rss/xml/nzhrsscid_000000008.xml'),
62 ]
63
64 def preprocess_html(self, soup, *a):
65 for img in soup.findAll('img', attrs={'data-srcset': True}):
66 for x in img['data-srcset'].split(','):
67 if '768w' in x:
68 img['src'] = x.split()[0]
69 break
70 else:
71 img['src'] = img['data-srcset'].split(',')[-1].split()[0]
72 return soup