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

  1. #!/usr/bin/env  python
  2. __license__   = 'GPL v3'
  3. __copyright__ = '2009, Matthew Briggs <hal.sulphur@gmail.com>'
  4. __docformat__ = 'restructuredtext en'
  5.  
  6. '''
  7. theage.com.au
  8. '''
  9. from calibre import strftime
  10. from calibre.web.feeds.news import BasicNewsRecipe
  11. from calibre.ebooks.BeautifulSoup import BeautifulSoup
  12. import re
  13.  
  14. class TheAge(BasicNewsRecipe):
  15.  
  16.     title            = 'The Age'
  17.     description      = 'Business News, World News and Breaking News in Melbourne, Australia'
  18.     publication_type = 'newspaper'
  19.     __author__       = 'Matthew Briggs'
  20.     language         = 'en_AU'
  21.     
  22.     max_articles_per_feed = 1000
  23.     recursions        = 0
  24.     remove_tags       = [dict(name=['table', 'script', 'noscript', 'style']), dict(name='a', attrs={'href':'/'}), dict(name='a', attrs={'href':'/text/'})]
  25.  
  26.     def get_browser(self):
  27.         br = BasicNewsRecipe.get_browser()
  28.         br.set_handle_refresh(False)
  29.         return br
  30.  
  31.     def parse_index(self):
  32.  
  33.         soup = BeautifulSoup(self.browser.open('http://www.theage.com.au/text/').read())
  34.  
  35.         section = None
  36.         sections = {}
  37.  
  38.         for tag in soup.findAll(['h3', 'a']):
  39.             if tag.name == 'h3':
  40.                 section = self.tag_to_string(tag)
  41.                 sections[section] = []
  42.  
  43.             # Make sure to skip: <a href="/">TheAge</a>
  44.  
  45.             elif section and tag.has_key('href') and len(tag['href'].strip())>1:
  46.                 url = tag['href'].strip()
  47.                 if url.startswith('/'):
  48.                     url = 'http://www.theage.com.au' + url
  49.                 title = self.tag_to_string(tag)
  50.                 sections[section].append({
  51.                                  'title': title,
  52.                                  'url'  : url,
  53.                                  'date' : strftime('%a, %d %b'),
  54.                                  'description' : '',
  55.                                  'content'     : '',
  56.                                  })
  57.                                  
  58.         feeds = []
  59.  
  60.         # Insert feeds in specified order, if available
  61.         
  62.         feedSort = [ 'National', 'World', 'Opinion', 'Columns', 'Business', 'Sport', 'Entertainment' ]
  63.         for i in feedSort:
  64.           if i in sections:
  65.             feeds.append((i,sections[i]))
  66.  
  67.         # Done with the sorted feeds
  68.  
  69.         for i in feedSort:
  70.           del sections[i]
  71.         
  72.         # Append what is left over...
  73.  
  74.         for i in sections:
  75.           feeds.append((i,sections[i]))
  76.             
  77.         return feeds
  78.  
  79.     def get_cover_url(self):
  80.  
  81.         soup = BeautifulSoup(self.browser.open('http://www.theage.com.au/todays-paper').read())
  82.  
  83.         for i in soup.findAll('a'):
  84.           href = i['href']
  85.           if href and re.match('http://www.theage.com.au/frontpage/[0-9]+/[0-9]+/[0-9]+/frontpage.pdf',href):
  86.             return href
  87.  
  88.         return None
  89.  
  90.     def preprocess_html(self,soup):
  91.         
  92.         for p in soup.findAll('p'):
  93.         
  94.           # Collapse the paragraph by joining the non-tag contents
  95.  
  96.           contents = [i for i in p.contents if isinstance(i,unicode)]
  97.           if len(contents):
  98.             contents = ''.join(contents)
  99.  
  100.             # Filter out what's left of the text-mode navigation stuff
  101.  
  102.             if re.match('((\s)|(\ \;))*\[[\|\s*]*\]((\s)|(\ \;))*$',contents):
  103.               p.extract()
  104.               continue
  105.  
  106.             # Shrink the fine print font 
  107.  
  108.             if contents=='This material is subject to copyright and any unauthorised use, copying or mirroring is prohibited.':
  109.               p['style'] = 'font-size:small'
  110.               continue        
  111.         
  112.         return soup
  113.