home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_3924 < prev    next >
Encoding:
Text File  |  2010-01-16  |  5.3 KB  |  127 lines

  1. from calibre.web.feeds.news import BasicNewsRecipe
  2. from calibre.ebooks.BeautifulSoup import BeautifulSoup, Tag
  3.  
  4. class NrcNextRecipe(BasicNewsRecipe):
  5.     __license__  = 'GPL v3'
  6.     __author__ = 'kwetal'
  7.     language = 'nl'
  8.     country = 'NL'
  9.     version = 2
  10.  
  11.     title = u'nrcnext'
  12.     publisher = u'NRC Media'
  13.     category = u'News, Opinion, the Netherlands'
  14.     description = u'Dutch newsblog from the Dutch daily newspaper nrcnext.'
  15.  
  16.     conversion_options = {'comments': description, 'language': language, 'publisher': publisher}
  17.  
  18.     no_stylesheets = True
  19.     remove_javascript = True
  20.  
  21.     keep_only_tags = [dict(name='div', attrs={'id' : 'main'})]
  22.  
  23.     remove_tags = []
  24.     remove_tags.append(dict(name = 'div', attrs = {'class' : 'meta'}))
  25.     remove_tags.append(dict(name = 'div', attrs = {'class' : 'datumlabel'}))
  26.     remove_tags.append(dict(name = 'ul', attrs = {'class' : 'cats single'}))
  27.     remove_tags.append(dict(name = 'ul', attrs = {'class' : 'cats onderwerpen'}))
  28.     remove_tags.append(dict(name = 'ul', attrs = {'class' : 'cats rubrieken'}))
  29.  
  30.     extra_css = '''
  31.                 body {font-family: verdana, arial, helvetica, geneva, sans-serif; text-align: left;}
  32.                 p.wp-caption-text {font-size: x-small; color: #666666;}
  33.                 h2.sub_title {font-size: medium; color: #696969;}
  34.                 h2.vlag {font-size: small; font-weight: bold;}
  35.                 '''
  36.  
  37.     def parse_index(self) :
  38.         # Use the wesbite as an index. Their RSS feeds can be out of date.
  39.         feeds = {}
  40.         feeds[u'columnisten'] = u'http://www.nrcnext.nl/columnisten/'
  41.         feeds[u'koken'] = u'http://www.nrcnext.nl/koken/'
  42.         feeds[u'geld & werk'] = u'http://www.nrcnext.nl/geld-en-werk/'
  43.         feeds[u'vandaag'] = u'http://www.nrcnext.nl'
  44.         feeds[u'city life in afrika']  = u'http://www.nrcnext.nl/city-life-in-afrika/'
  45.         answer = []
  46.         articles = {}
  47.         indices = []
  48.  
  49.         for index, feed in feeds.items() :
  50.             soup = self.index_to_soup(feed)
  51.  
  52.             for post in soup.findAll(True, attrs={'class' : 'post'}) :
  53.                 # Find the links to the actual articles and rember the location they're pointing to and the title
  54.                 a = post.find('a', attrs={'rel' : 'bookmark'})
  55.                 href = a['href']
  56.                 title = self.tag_to_string(a)
  57.  
  58.                 if index == 'columnisten' :
  59.                     # In this feed/page articles can be written by more than one author.
  60.                     # It is nice to see their names in the titles.
  61.                     flag = post.find('h2', attrs = {'class' : 'vlag'})
  62.                     author = flag.contents[0].renderContents()
  63.                     completeTitle = u''.join([author, u': ', title])
  64.                 else :
  65.                     completeTitle = title
  66.  
  67.                 # Add the article to a temporary list
  68.                 article = {'title' : completeTitle, 'date' : u'', 'url'  : href, 'description' : '<p> </p>'}
  69.                 if not articles.has_key(index) :
  70.                     articles[index] = []
  71.                 articles[index].append(article)
  72.  
  73.             # Add the index title to a temporary list
  74.             indices.append(index)
  75.  
  76.         # Now, sort the temporary list of feeds in the order they appear on the website
  77.         indices = self.sort_index_by(indices, {u'columnisten' : 1, u'koken' : 3, u'geld & werk' : 2, u'vandaag' : 0, u'city life in afrika' : 4})
  78.         # Apply this sort order to the actual list of feeds and articles
  79.         answer = [(key, articles[key]) for key in indices if articles.has_key(key)]
  80.  
  81.         return answer
  82.  
  83.     def preprocess_html(self, soup) :
  84.         if soup.find('div', attrs = {'id' : 'main', 'class' : 'single'}):
  85.             tag = soup.find('div', attrs = {'class' : 'post'})
  86.             if tag:
  87.                 h2 = tag.find('h2', 'vlag')
  88.                 if h2:
  89.                     new_h2 = Tag(soup, 'h2', attrs = [('class', 'vlag')])
  90.                     new_h2.append(self.tag_to_string(h2))
  91.                     h2.replaceWith(new_h2)
  92.                 else:
  93.                     h2 = tag.find('h2')
  94.                     if h2:
  95.                         new_h2 = Tag(soup, 'h2', attrs = [('class', 'sub_title')])
  96.                         new_h2.append(self.tag_to_string(h2))
  97.                         h2.replaceWith(new_h2)
  98.  
  99.                 h1 = tag.find('h1')
  100.                 if h1:
  101.                     new_h1 = Tag(soup, 'h1')
  102.                     new_h1.append(self.tag_to_string(h1))
  103.                     h1.replaceWith(new_h1)
  104.  
  105.                 # Slows down my reader.
  106.                 for movie in tag.findAll('span', attrs = {'class' : 'vvqbox vvqvimeo'}):
  107.                     movie.extract()
  108.                 for movie in tag.findAll('span', attrs = {'class' : 'vvqbox vvqyoutube'}):
  109.                     movie.extract()
  110.                 for iframe in tag.findAll('iframe') :
  111.                     iframe.extract()
  112.  
  113.                 fresh_soup = self.getFreshSoup(soup)
  114.                 fresh_soup.body.append(tag)
  115.  
  116.                 return fresh_soup
  117.             else:
  118.                 # This should never happen and other famous last words...
  119.                 return soup
  120.  
  121.     def getFreshSoup(self, oldSoup):
  122.         freshSoup = BeautifulSoup('<html><head><title></title></head><body></body></html>')
  123.         if oldSoup.head.title:
  124.             freshSoup.head.title.append(self.tag_to_string(oldSoup.head.title))
  125.         return freshSoup
  126.  
  127.