| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# Based on veezh's original recipe and Kovid Goyal's New York Times recipe |
| 4 |
|
| 5 |
__license__ = 'GPL v3' |
| 6 |
__copyright__ = '2011, Snaab' |
| 7 |
|
| 8 |
''' |
| 9 |
www.nrc.nl |
| 10 |
''' |
| 11 |
import os |
| 12 |
import zipfile |
| 13 |
from datetime import date, timedelta |
| 14 |
from io import BytesIO |
| 15 |
|
| 16 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 17 |
|
| 18 |
|
| 19 |
class NRCHandelsblad(BasicNewsRecipe): |
| 20 |
title = 'NRC Handelsblad' |
| 21 |
description = 'De ePaper-versie van NRC' |
| 22 |
language = 'nl' |
| 23 |
needs_subscription = True |
| 24 |
requires_version = (1, 24, 0) |
| 25 |
|
| 26 |
__author__ = 'Kovid Goyal' |
| 27 |
|
| 28 |
conversion_options = {'no_default_epub_cover': True} |
| 29 |
|
| 30 |
def build_index(self): |
| 31 |
from calibre.web.jsbrowser.browser import Browser, ElementNotFound |
| 32 |
|
| 33 |
br = Browser() |
| 34 |
br.visit('http://login.nrc.nl/login', timeout=60) |
| 35 |
f = br.select_form('#fm1') |
| 36 |
f['username'] = self.username |
| 37 |
f['password'] = self.password |
| 38 |
br.submit('button[type="submit"]') |
| 39 |
raw = br.html |
| 40 |
if '>Uitloggen<' not in raw: |
| 41 |
raise ValueError('Failed to login, check username and password') |
| 42 |
epubraw = None |
| 43 |
for today in ( |
| 44 |
date.today(), |
| 45 |
date.today() - timedelta(days=1), |
| 46 |
): |
| 47 |
url = 'http://digitaleeditie.nrc.nl/digitaleeditie/NH/%s/%d/%s___/downloads.html' % ( |
| 48 |
today.strftime('%Y'), |
| 49 |
today.month - 1, |
| 50 |
today.strftime('%Y%m%d'), |
| 51 |
) |
| 52 |
self.log('Trying to download epub from:', url) |
| 53 |
br.start_load(url, timeout=60) |
| 54 |
try: |
| 55 |
epubraw = br.download_file('#CompleteDownloads .download-list .download-button') |
| 56 |
break |
| 57 |
except ElementNotFound: |
| 58 |
self.log('%r not available yet' % url) |
| 59 |
continue |
| 60 |
|
| 61 |
if epubraw is None: |
| 62 |
raise ValueError('Krant van vandaag nog niet beschikbaar') |
| 63 |
|
| 64 |
zfile = zipfile.ZipFile(BytesIO(epubraw), 'r') |
| 65 |
zfile.extractall(self.output_dir) |
| 66 |
index = os.path.join(self.output_dir, 'metadata.opf') |
| 67 |
return index |