home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_280 / fokkeensukke.recipe < prev    next >
Text File  |  2011-09-09  |  4KB  |  95 lines

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