Debian Codegraph @

recipes/nrc_next.recipe

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Based on veezh's original recipe, Kovid Goyal's New York Times
4 # recipe and Snaabs nrc Handelsblad recipe
5
6 __license__ = 'GPL v3'
7 __copyright__ = '2014 Niels Giesen'
8
9 '''
10 www.nrc.nl
11 '''
12 import os
13 import re
14 import zipfile
15 from datetime import date, timedelta
16 from io import BytesIO
17
18 from calibre.web.feeds.news import BasicNewsRecipe
19
20
21 class NRCNext(BasicNewsRecipe):
22 title = 'nrc•next'
23 description = 'De ePaper-versie van nrc•next'
24 language = 'nl'
25 needs_subscription = True
26 requires_version = (1, 24, 0)
27
28 __author__ = 'Niels Giesen'
29
30 conversion_options = {'no_default_epub_cover': True}
31
32 def build_index(self):
33 import mechanize
34
35 br = mechanize.Browser()
36 br.open('https://login.nrc.nl/login', timeout=60)
37 br.select_form(nr=0)
38 br['username'] = self.username
39 br['password'] = self.password
40 response2 = br.submit()
41 raw = response2.get_data()
42 if b'niet ingelogd' in raw: # in body class
43 raise ValueError('Failed to login, check username and password')
44 epubraw = None
45 for today in (
46 date.today(),
47 date.today() - timedelta(days=1),
48 ):
49 url = 'http://digitaleeditie.nrc.nl/digitaleeditie/helekrant/epub/nn_%s.epub' % today.strftime('%Y%m%d')
50 self.log('Trying to download epub from:', url)
51 try:
52 response3 = br.open(url, timeout=60)
53 epubraw = response3.get_data()
54 break
55 except mechanize.HTTPError:
56 self.log('%r not available yet' % url)
57 continue
58
59 if epubraw is None:
60 raise ValueError('Krant van vandaag nog niet beschikbaar')
61
62 zfile = zipfile.ZipFile(BytesIO(epubraw), 'r')
63 zfile.extractall(self.output_dir)
64 namelist = zfile.namelist()
65 emre = re.compile(r'<em(?:.*)>(.*)</em>')
66 subst = '\\1'
67 for name in namelist:
68 _, ext = os.path.splitext(name)
69 if ext in {'.html', '.ncx'}:
70 fname = os.path.join(self.output_dir, name)
71 with open(fname) as f:
72 s = f.read()
73 s = emre.sub(subst, s)
74 with open(fname, 'w') as f:
75 f.write(s)
76 index = os.path.join(self.output_dir, 'metadata.opf')
77 return index