home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_280 / ncrnext.recipe < prev    next >
Encoding:
Text File  |  2011-09-09  |  6.0 KB  |  135 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 = 'p', attrs = {'class' : 'meta'}))
  26.     remove_tags.append(dict(name = 'div', attrs = {'class' : 'datumlabel'}))
  27.     remove_tags.append(dict(name = 'div', attrs = {'class' : 'sharing-is-caring'}))
  28.     remove_tags.append(dict(name = 'div', attrs = {'class' : 'navigation'}))
  29.     remove_tags.append(dict(name = 'div', attrs = {'class' : 'reageer'}))
  30.     remove_tags.append(dict(name = 'div', attrs = {'class' : 'comment odd alt thread-odd thread-alt depth-1 reactie '}))
  31.     remove_tags.append(dict(name = 'div', attrs = {'class' : 'comment even thread-even depth-1 reactie '}))
  32.     remove_tags.append(dict(name = 'ul', attrs = {'class' : 'cats single'}))
  33.     remove_tags.append(dict(name = 'ul', attrs = {'class' : 'cats onderwerpen'}))
  34.     remove_tags.append(dict(name = 'ul', attrs = {'class' : 'cats rubrieken'}))
  35.     remove_tags.append(dict(name = 'h3', attrs = {'class' : 'reacties'}))
  36.  
  37.  
  38.  
  39.     extra_css = '''
  40.                 body {font-family: verdana, arial, helvetica, geneva, sans-serif; text-align: left;}
  41.                 p.wp-caption-text {font-size: x-small; color: #666666;}
  42.                 h2.sub_title {font-size: medium; color: #696969;}
  43.                 h2.vlag {font-size: small; font-weight: bold;}
  44.                 '''
  45.  
  46.     def parse_index(self) :
  47.         # Use the wesbite as an index. Their RSS feeds can be out of date.
  48.         feeds = {}
  49.         feeds[u'columnisten'] = u'http://www.nrcnext.nl/columnisten/'
  50.         feeds[u'koken'] = u'http://www.nrcnext.nl/koken/'
  51.         feeds[u'geld & werk'] = u'http://www.nrcnext.nl/geld-en-werk/'
  52.         feeds[u'vandaag'] = u'http://www.nrcnext.nl'
  53.         # feeds[u'city life in afrika']  = u'http://www.nrcnext.nl/city-life-in-afrika/'
  54.         answer = []
  55.         articles = {}
  56.         indices = []
  57.  
  58.         for index, feed in feeds.items() :
  59.             soup = self.index_to_soup(feed)
  60.             for post in soup.findAll(True, attrs={'class' : 'post '}) :
  61.                 # Find the links to the actual articles and rember the location they're pointing to and the title
  62.                 a = post.find('a', attrs={'rel' : 'bookmark'})
  63.                 href = a['href']
  64.                 title = self.tag_to_string(a)
  65.                 if index == 'columnisten' :
  66.                     # In this feed/page articles can be written by more than one author.
  67.                     # It is nice to see their names in the titles.
  68.                     flag = post.find('h2', attrs = {'class' : 'vlag'})
  69.                     author = flag.contents[0].renderContents()
  70.                     completeTitle = u''.join([author, u': ', title])
  71.                 else :
  72.                     completeTitle = title
  73.  
  74.                 # Add the article to a temporary list
  75.                 article = {'title' : completeTitle, 'date' : u'', 'url'  : href, 'description' : '<p> </p>'}
  76.                 if not articles.has_key(index) :
  77.                     articles[index] = []
  78.                 articles[index].append(article)
  79.  
  80.             # Add the index title to a temporary list
  81.             indices.append(index)
  82.  
  83.         # Now, sort the temporary list of feeds in the order they appear on the website
  84.         # 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})
  85.         indices = self.sort_index_by(indices, {u'columnisten' : 1, u'koken' : 3, u'geld & werk' : 2, u'vandaag' : 0})
  86.         # Apply this sort order to the actual list of feeds and articles
  87.         answer = [(key, articles[key]) for key in indices if articles.has_key(key)]
  88.  
  89.         return answer
  90.  
  91.     def preprocess_html(self, soup) :
  92.         if soup.find('div', attrs = {'id' : 'main', 'class' : 'single'}):
  93.             tag = soup.find('div', attrs = {'class' : 'post'})
  94.             if tag:
  95.                 h2 = tag.find('h2', 'vlag')
  96.                 if h2:
  97.                     new_h2 = Tag(soup, 'h2', attrs = [('class', 'vlag')])
  98.                     new_h2.append(self.tag_to_string(h2))
  99.                     h2.replaceWith(new_h2)
  100.                 else:
  101.                     h2 = tag.find('h2')
  102.                     if h2:
  103.                         new_h2 = Tag(soup, 'h2', attrs = [('class', 'sub_title')])
  104.                         new_h2.append(self.tag_to_string(h2))
  105.                         h2.replaceWith(new_h2)
  106.  
  107.                 h1 = tag.find('h1')
  108.                 if h1:
  109.                     new_h1 = Tag(soup, 'h1')
  110.                     new_h1.append(self.tag_to_string(h1))
  111.                     h1.replaceWith(new_h1)
  112.  
  113.                 # Slows down my reader.
  114.                 for movie in tag.findAll('span', attrs = {'class' : 'vvqbox vvqvimeo'}):
  115.                     movie.extract()
  116.                 for movie in tag.findAll('span', attrs = {'class' : 'vvqbox vvqyoutube'}):
  117.                     movie.extract()
  118.                 for iframe in tag.findAll('iframe') :
  119.                     iframe.extract()
  120.  
  121.                 fresh_soup = self.getFreshSoup(soup)
  122.                 fresh_soup.body.append(tag)
  123.  
  124.                 return fresh_soup
  125.             else:
  126.                 # This should never happen and other famous last words...
  127.                 return soup
  128.  
  129.     def getFreshSoup(self, oldSoup):
  130.         freshSoup = BeautifulSoup('<html><head><title></title></head><body></body></html>')
  131.         if oldSoup.head.title:
  132.             freshSoup.head.title.append(self.tag_to_string(oldSoup.head.title))
  133.         return freshSoup
  134.  
  135.