calibre/recipes/brand_eins.recipe
2015-01-23 19:08:21 +05:30

113 lines
4.2 KiB
Python

#!/usr/bin/env python2
# vim:fileencoding=utf-8
from __future__ import unicode_literals
__license__ = 'GPL v3'
__copyright__ = '2014, Nikolas Mangold-Takao <nmangold at gmail.com>'
__version__ = '0.10'
''' http://brandeins.de - Wirtschaftsmagazin '''
from collections import OrderedDict
from calibre.web.feeds.recipes import BasicNewsRecipe
class BrandEins(BasicNewsRecipe):
title = u'brand eins'
__author__ = 'Nikolas Mangold-Takao'
description = u'brand eins beschreibt den momentanen Wandel in Wirtschaft und Gesellschaft, den Übergang vom Informations- zum Wissenszeitalter.'
publisher = u'brand eins Verlag GmbH & Co. oHG'
category = 'politics, business, wirtschaft, Germany'
PREFIX = 'http://www.brandeins.de/'
INDEX = PREFIX + 'archiv/listeansicht.html'
use_embedded_content = False
lang = 'de-DE'
no_stylesheets = True
encoding = 'utf-8'
language = 'de'
publication_type = 'magazine'
needs_subscription = 'optional'
timefmt = ''
keep_only_tags = dict(name='div', attrs={'id':'content'})
remove_tags_before = dict(name='div', attrs={'class':'innerContent typeArticle'})
remove_tags_after = dict(name='div', attrs={'id':'socialshareprivacy'})
issue_url = ''
'''
brandeins.de
'''
def parse_index(self):
# Allow username/password information to access a past issue (mis)using username and password fields
# username = year [yyyy, e.g. 2012]
# password = month [MM, e.g. 10 for October]
issue = ""
if self.username is not None and self.password is not None:
try:
issue = "{}{}".format(self.username, self.password) # yyyyMM
except:
pass
soup = self.index_to_soup(self.INDEX)
issue_list = soup.findAll('div', attrs={'class': 'details'})
issue_map = {}
i = 0
for entry in issue_list:
title = self.tag_to_string(entry.find('h3', attrs={'class': 'like-h1'}))
issue_string = self.tag_to_string(entry.find('span', attrs={'class': 'meta'}))
year = issue_string[8:]
month = issue_string[5:-5]
yyyymm = "{}{}".format(year, month)
link = entry.findAll('a')[0]
issue_map[yyyymm] = link.get('href')
self.log('- ', year, month, title, link.get('href'))
# Issue 1 (most recent) has only few articles online,
# Issue 2 (2nd recent) is not completely online.
# Issue 3 (3rd recent) is completely online, hence i == 2
if issue == "" and i == 2:
issue = yyyymm
i+=1
self.log('Issue to get: ', issue, title)
url = 'http://brandeins.de/'+issue_map[issue]
self.issue_url = url # save to extract cover
return self.parse_issue(url)
def parse_issue(self, url):
soup = self.index_to_soup(url)
feeds = OrderedDict()
for item in soup.findAll(attrs={'class':lambda x:'ihv_item' in (x or '').split()}):
a = item.findParent('a', href=True)
if a is None:
continue
url = self.PREFIX + a['href']
title = self.tag_to_string(item.find(attrs={'class':'ihv_title'}))
sec = self.tag_to_string(item.find(attrs={'class':'ihv_page_category'}).findAll('span')[-1])
if sec not in feeds:
feeds[sec] = []
desc = ''
for p in item.findAll('p'):
desc += self.tag_to_string(p) + '\n'
feeds[sec].append({'title':title, 'url':url, 'description':desc})
self.log('Found article:', title, 'at', url)
return [(st, articles) for st, articles in feeds.iteritems() if articles]
def get_cover_url(self):
# the index does not contain a usable cover, but the "Welt in Zahlen"-article contains it
cover_article = "{}/{}".format(self.issue_url[:-5], 'die-welt-in-zahlen.html')
self.log(cover_article)
soup = self.index_to_soup(cover_article)
cover_meta = soup.find('meta', attrs={'property':'og:image'})
if cover_meta:
return cover_meta['content']
else:
self.log('ERROR: Could not return cover url')