| 1 |
from calibre.ebooks.BeautifulSoup import BeautifulSoup, Tag |
| 2 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 3 |
|
| 4 |
|
| 5 |
def new_tag(soup, name, attrs=()): |
| 6 |
impl = getattr(soup, 'new_tag', None) |
| 7 |
if impl is not None: |
| 8 |
return impl(name, attrs=dict(attrs)) |
| 9 |
return Tag(soup, name, attrs=attrs or None) |
| 10 |
|
| 11 |
|
| 12 |
class FokkeEnSukkeRecipe(BasicNewsRecipe): |
| 13 |
__license__ = 'GPL v3' |
| 14 |
__author__ = 'kwetal' |
| 15 |
language = 'nl' |
| 16 |
country = 'NL' |
| 17 |
version = 2 |
| 18 |
|
| 19 |
title = 'Fokke en Sukke' |
| 20 |
publisher = 'Reid, Geleijnse & Van Tol' |
| 21 |
category = 'News, Cartoons' |
| 22 |
description = 'Popular Dutch daily cartoon Fokke en Sukke' |
| 23 |
|
| 24 |
conversion_options = {'comments': description, 'language': language, 'publisher': publisher} |
| 25 |
|
| 26 |
no_stylesheets = True |
| 27 |
extra_css = ''' |
| 28 |
body{font-family: verdana, arial, helvetica, geneva, sans-serif ; margin: 0em; padding: 0em;} |
| 29 |
div.title {text-align: center; margin-bottom: 1em;} |
| 30 |
''' |
| 31 |
|
| 32 |
INDEX = 'http://foksuk.nl' |
| 33 |
cover_url = 'http://foksuk.nl/content/wysiwyg/simpleimages/image350.gif' |
| 34 |
|
| 35 |
keep_only_tags = [dict(name='div', attrs={'class': 'cartoon'})] |
| 36 |
|
| 37 |
def parse_index(self): |
| 38 |
# A list with daynames as they _can_ appear in the index |
| 39 |
dayNames = ['maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag & zondag'] |
| 40 |
soup = self.index_to_soup(self.INDEX) |
| 41 |
|
| 42 |
# Find the links for the various cartoons for this week and loop |
| 43 |
# through them |
| 44 |
index = soup.find('div', attrs={'class': 'selectcartoon'}) |
| 45 |
links = index.findAll('a') |
| 46 |
maxIndex = len(links) - 1 |
| 47 |
articles = [] |
| 48 |
for i in range(1, len(links)): |
| 49 |
# There can be more than one cartoon for a given day (currently either one or two). |
| 50 |
# If there's only one, there is just a link with the dayname. |
| 51 |
# If there are two, there are three links in sequence: <a>dayname</a> <a>1</a> <a>2</a>. |
| 52 |
# In that case we're interested in the last two. |
| 53 |
if links[i].renderContents().decode('utf-8') in dayNames: |
| 54 |
# If the link is not in daynames, we processed it already, but if it is, let's see |
| 55 |
# if the next one has '1' as content |
| 56 |
if (i + 1 <= maxIndex) and (links[i + 1].renderContents().decode('utf-8') == '1'): |
| 57 |
# Got you! Add it to the list |
| 58 |
article = { |
| 59 |
'title': links[i].renderContents().decode('utf-8') + ' 1', |
| 60 |
'date': '', |
| 61 |
'url': self.INDEX + links[i + 1]['href'], |
| 62 |
'description': '', |
| 63 |
} |
| 64 |
articles.append(article) |
| 65 |
# If there is a '1', there should be a '2' as well, but |
| 66 |
# better save than sorry |
| 67 |
if (i + 2 <= maxIndex) and (links[i + 2].renderContents().decode('utf-8') == '2'): |
| 68 |
# Got you! Add it to the list |
| 69 |
article = { |
| 70 |
'title': links[i].renderContents().decode('utf-8') + ' 2', |
| 71 |
'date': '', |
| 72 |
'url': self.INDEX + links[i + 2]['href'], |
| 73 |
'description': '', |
| 74 |
} |
| 75 |
articles.append(article) |
| 76 |
else: |
| 77 |
# There is only one cartoon for this day. Add it to the |
| 78 |
# list. |
| 79 |
article = {'title': links[i].renderContents().decode('utf-8'), 'date': '', 'url': self.INDEX + links[i]['href'], 'description': ''} |
| 80 |
articles.append(article) |
| 81 |
# Might as well use the weeknumber as title |
| 82 |
week = index.find('span', attrs={'class': 'week'}).renderContents().decode('utf-8') |
| 83 |
|
| 84 |
return [[week, articles]] |
| 85 |
|
| 86 |
def preprocess_html(self, soup): |
| 87 |
cartoon = soup.find('div', attrs={'class': 'cartoon'}) |
| 88 |
|
| 89 |
title = '' |
| 90 |
img = soup.find('img', attrs={'alt': True}) |
| 91 |
if img: |
| 92 |
title = img['alt'] |
| 93 |
|
| 94 |
tag = new_tag(soup, 'div', [('class', 'title')]) |
| 95 |
tag.insert(0, title) |
| 96 |
cartoon.insert(0, tag) |
| 97 |
|
| 98 |
# We only want the cartoon, so throw out the index |
| 99 |
select = cartoon.find('div', attrs={'class': 'selectcartoon'}) |
| 100 |
if select: |
| 101 |
select.extract() |
| 102 |
|
| 103 |
freshSoup = self.getFreshSoup(soup) |
| 104 |
freshSoup.body.append(cartoon) |
| 105 |
|
| 106 |
return freshSoup |
| 107 |
|
| 108 |
def getFreshSoup(self, oldSoup): |
| 109 |
freshSoup = BeautifulSoup('<html><head><title></title></head><body></body></html>') |
| 110 |
if oldSoup.head.title: |
| 111 |
freshSoup.head.title.append(self.tag_to_string(oldSoup.head.title)) |
| 112 |
return freshSoup |