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

  1. from calibre.web.feeds.news import BasicNewsRecipe
  2. from calibre.ebooks.BeautifulSoup import Tag
  3. import re
  4.  
  5. class NatureNews(BasicNewsRecipe):
  6.     title          = u'Nature News'
  7.     language       = 'en'
  8.     __author__     = 'Krittika Goyal, Starson17'
  9.     oldest_article = 31 #days
  10.     remove_empty_feeds    = True
  11.     max_articles_per_feed = 50
  12.  
  13.     no_stylesheets = True
  14.     keep_only_tags = [dict(name='div', attrs={'id':'content'})]
  15. #    remove_tags_before = dict(name='h1', attrs={'class':'heading entry-title'})
  16. #    remove_tags_after  = dict(name='h2', attrs={'id':'comments'})
  17.     remove_tags = [
  18.        dict(name='h2', attrs={'id':'comments'}),
  19.        dict(attrs={'alt':'Advertisement'}),
  20.        dict(name='div', attrs={'class':'ad'}),
  21.        dict(attrs={'class':'Z3988'}),
  22.        dict(attrs={'class':['formatpublished','type-of-article','cleardiv','disclaimer','buttons','comments xoxo']}),
  23.        dict(name='a', attrs={'href':'#comments'}),
  24.        dict(name='h2',attrs={'class':'subheading plusicon icon-add-comment'})
  25.     ]
  26.  
  27.     preprocess_regexps = [
  28.         (re.compile(r'<p>ADVERTISEMENT</p>', re.DOTALL|re.IGNORECASE), lambda match: ''),
  29.         ]
  30.  
  31.     extra_css             = '''
  32.                             .author { text-align: right; font-size: small; line-height:1em; margin-top:0px; margin-left:0; margin-right:0; margin-bottom: 0; }
  33.                             .imagedescription { font-size: small; font-style:italic; line-height:1em; margin-top:5px; margin-left:0; margin-right:0; margin-bottom: 0; }
  34.                             .imagecredit { font-size: x-small; font-style: normal; font-weight: bold}
  35.                             '''
  36.  
  37.     feeds = [('Nature News', 'http://feeds.nature.com/news/rss/most_recent')]
  38.  
  39.     def preprocess_html(self,soup):
  40.         # The author name is slightly buried - dig it up
  41.         author = soup.find('p', {'class':'byline'})
  42.         if author:
  43.             # Find out the author's name
  44.             authornamediv = author.find('span',{'class':'author fn'})
  45.             authornamelink = authornamediv.find('a')
  46.             if authornamelink:
  47.                 authorname = authornamelink.contents[0]
  48.             else:
  49.                 authorname = authornamediv.contents[0]
  50.             # Stick the author's name in the byline tag
  51.             tag = Tag(soup,'div')
  52.             tag['class'] = 'author'
  53.             tag.insert(0,authorname.strip())
  54.             author.replaceWith(tag)
  55.  
  56.         # Change the intro from a p to a div
  57.         intro = soup.find('p',{'class':'intro'})
  58.         if intro:
  59.             tag = Tag(soup,'div')
  60.             tag['class'] = 'intro'
  61.             tag.insert(0,intro.contents[0])
  62.             intro.replaceWith(tag)
  63.  
  64.         # Change span class=imagedescription to div
  65.         descr = soup.find('span',{'class':'imagedescription'})
  66.         if descr:
  67.             tag = Tag(soup,'div')
  68.             tag['class'] = 'imagedescription'
  69.             tag.insert(0,descr.renderContents())
  70.             descr.replaceWith(tag)
  71.  
  72.         # The references are in a list, let's make them simpler
  73.         reflistcont =  soup.find('ul',{'id':'article-refrences'})
  74.         if reflistcont:
  75.             reflist = reflistcont.li.renderContents()
  76.             tag = Tag(soup,'div')
  77.             tag['class'] = 'article-references'
  78.             tag.insert(0,reflist)
  79.             reflistcont.replaceWith(tag)
  80.  
  81.         # Within the id=content div, we need to remove all the stuff after the end of the class=entry-content
  82.         entrycontent = soup.find('div',{'class':'entry-content'})
  83.         for nextSibling in entrycontent.findNextSiblings():
  84.             nextSibling.extract()
  85.  
  86.         return soup
  87.