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

  1. from calibre.web.feeds.news import BasicNewsRecipe
  2. from calibre.ebooks.BeautifulSoup import BeautifulSoup, Tag, Comment
  3.  
  4. class GlennBeckRecipe(BasicNewsRecipe):
  5.     __license__   = 'GPL v3'
  6.     __author__ = 'kwetal'
  7.     language = 'en'
  8.     version = 1
  9.  
  10.     title = u'Glenn Beck'
  11.     publisher = u'Premiere Radio Networks'
  12.     category = u'News, Opinion'
  13.     description = u'The fusion of entertainment and enlightenment'
  14.  
  15.     oldest_article = 7
  16.     max_articles_per_feed = 100
  17.  
  18.     no_stylesheets = True
  19.     remove_javascript = True
  20.     use_embedded_content = False
  21.  
  22.     feeds = [(u'Glenn Beck', u'http://feeds.feedburner.com/GlennBeckArticles')]
  23.  
  24.     def preprocess_html(self, soup):
  25.         # Their html is horribly broken; if we search for the div that has the content BeatifulSoup returns the div with only the headline and no content.
  26.         # This is due to illegal nesting of tags. So we do it the hard way.
  27.  
  28.         # We can find this one, and we don't want it.
  29.         div = soup.find('div', attrs = {'id': 'extraInfo'})
  30.         if div:
  31.             div.extract()
  32.  
  33.         # Don't want these either.
  34.         iframes = soup.findAll('iframe')
  35.         [iframe.extract() for iframe in iframes]
  36.  
  37.         # Get empty document.
  38.         freshSoup = self.getFreshSoup()
  39.  
  40.         # This is the broken div; but we can find the headline.
  41.         newsDiv = soup.find('div', attrs = {'class': 'news-detail'})
  42.         if newsDiv:
  43.             if newsDiv.h1:
  44.                 freshSoup.body.append(newsDiv.h1)
  45.  
  46.         # The content is  wrapped in <p></p> tags, most of the time anyway.
  47.         counter = 0
  48.         for p in soup.findAll('p'):
  49.             if p.get('class') == 'smalltextwhite':
  50.                 # But we don't want this one.
  51.                 continue
  52.  
  53.             freshSoup.body.append(p)
  54.             counter += 1
  55.  
  56.         # Debugging block
  57.         #h3 = Tag(freshSoup, 'h3')
  58.         #h3.append('First counter: ' + str(counter))
  59.         #freshSoup.body.insert(0, h3)
  60.  
  61.         # In some articles the content is not wrapped in <p></p> tags. In that case the counter is low.
  62.         # 2 is the magic number that seems to work.
  63.         if counter <= 2:
  64.             # So they are playing hard-to-get: first throw out all comments.
  65.             comments = soup.findAll(text = lambda text: isinstance(text, Comment))
  66.             [comment.extract() for comment in comments]
  67.  
  68.             # Find all unwrapped strings.
  69.             for txt in soup.findAll(text = True):
  70.                 raw = txt.strip()
  71.                 # Debugging line
  72.                 #para.append(raw + '(parent: ' + txt.parent.name + '; length: ' + str(len(raw)) + '; start: ' + raw[0:4] + ')')
  73.  
  74.                 if (txt.parent.name == 'body' and len(raw) > 0) and not (len(raw) == 6 and raw == ' '):
  75.                     # This is our content; ignore the rest.
  76.                     para = Tag(freshSoup, 'p')
  77.                     para.append(raw)
  78.                     freshSoup.body.append(para)
  79.                     counter += 1
  80.  
  81.             # Now if the counter is still 0 or 1 they did something completely different and we still have an empty article. In a last attempt, add the whole content div, just in case.
  82.             if counter < 2:
  83.                 freshSoup.body.append(newsDiv)
  84.  
  85.             # Debugging block
  86.             #h3 = Tag(freshSoup, 'h3')
  87.             #h3.append('Second counter: ' + str(counter))
  88.             #freshSoup.body.insert(1, h3)
  89.  
  90.         return freshSoup
  91.  
  92.     def getFreshSoup(self, title = None):
  93.         if title:
  94.             return BeautifulSoup('<html><head><title>' + str(title) + '</title></head><body></body></html>')
  95.         else:
  96.             return BeautifulSoup('<html><head><title></title></head><body></body></html>')
  97.  
  98.