Debian Codegraph @

recipes/volksrant.recipe

raw
1 #!/usr/bin/env python
2 import json
3 import uuid
4 from contextlib import closing
5
6 from mechanize import Request
7
8 from calibre.web.feeds.recipes import BasicNewsRecipe
9
10
11 class Volkskrant(BasicNewsRecipe):
12 title = 'Volkskrant'
13 __author__ = 'Cristi Ghera'
14 max_articles_per_feed = 100
15 language = 'nl'
16 description = 'Volkskrant - Nieuws, achtergronden en columns'
17 needs_subscription = False
18 resolve_internal_links = True
19 remove_tags_before = dict(id='main-content')
20 remove_tags_after = dict(id='main-content')
21 remove_tags = [
22 dict(
23 attrs={
24 'class': [
25 'article-footer__sharing',
26 'artstyle__editorial-tips',
27 'artstyle__advertisement',
28 'artstyle__container__icon',
29 'artstyle__disabled-embed',
30 'container__title__icon',
31 ]
32 }
33 ),
34 dict(attrs={'data-element-id': ['article-element-authors']}),
35 dict(name=['script', 'noscript', 'style']),
36 ]
37 remove_attributes = ['class', 'id', 'name', 'style']
38 encoding = 'utf-8'
39 no_stylesheets = True
40 ignore_duplicate_articles = {'url'}
41
42 def parse_index(self):
43 soup = self.index_to_soup('https://www.volkskrant.nl/privacy-wall/accept?redirectUri=%2Feditie%2Fvandaag%2F&authId=' + str(uuid.uuid4()))
44 containers = soup.findAll('section', attrs={'class': 'section--horizontal'})
45 sections = []
46 for container in containers:
47 section_title = self.tag_to_string(container.find('h2')).strip()
48 articles = []
49
50 for art in container.findAll('article'):
51 a = art.find('a')
52 url = a['href']
53 if url[0] == '/':
54 url = 'https://www.volkskrant.nl' + url
55 if '/editie/' not in url:
56 continue
57 header = a.find('header')
58 teaser_label = self.tag_to_string(header.find('h4').find('span', attrs={'class': 'teaser__label'})).strip()
59 teaser_sublabel = self.tag_to_string(header.find('h4').find('span', attrs={'class': 'teaser__sublabel'})).strip()
60 teaser_title = self.tag_to_string(header.find('h3').find('span', attrs={'class': 'teaser__title__value--short'})).strip()
61 if teaser_label.lower() == 'podcast':
62 continue
63 parts = []
64 if teaser_label:
65 parts.append(teaser_label.upper())
66 if teaser_sublabel:
67 parts.append(teaser_sublabel)
68 if teaser_title:
69 parts.append(teaser_title)
70 article_title = ' \u2022 '.join(parts)
71 pubdate = ''
72 description = ''
73 articles.append(dict(title=article_title, url=url, date=pubdate, description=description, content=''))
74
75 sections.append((section_title, articles))
76 return sections
77
78 def preprocess_html(self, soup):
79 for tag in soup():
80 if tag.name == 'img':
81 if tag['src'][0] == '/':
82 tag['src'] = 'https://www.volkskrant.nl' + tag['src']
83
84 for tag in soup():
85 if tag.name == 'picture':
86 tag.replaceWith(tag.find('img'))
87
88 comic_articles = {'Bas van der Schot', 'Poldermodellen', 'Gummbah', 'Sigmund'}
89 if self.tag_to_string(soup.find('h1')).strip() in comic_articles:
90 for node in soup.find('figure').find_next_siblings():
91 node.extract()
92 return soup
93
94 def get_cover_url(self):
95 headers = {
96 'X-Requested-With': 'XMLHttpRequest',
97 'Accept': 'application/json, text/javascript, */*; q=0.01',
98 'DNT': '1',
99 }
100 url = 'https://login-api.e-pages.dk/v1/krant.volkskrant.nl/folders'
101 with closing(self.browser.open(Request(url, None, headers))) as r:
102 folders = json.loads(r.read())
103 return folders['objects'][0]['teaser_medium']
104 return None