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

  1. from calibre.web.feeds.recipes import BasicNewsRecipe
  2. from calibre.ebooks.BeautifulSoup import BeautifulSoup
  3. from urllib import quote
  4.  
  5. class SportsIllustratedRecipe(BasicNewsRecipe) :
  6.     __author__  = 'kwetal'
  7.     __copyright__ = 'kwetal'
  8.     __license__ = 'GPL v3'
  9.     language = 'en'
  10.     description = 'Sports Illustrated'
  11.     version = 3
  12.     title          = u'Sports Illustrated'
  13.  
  14.     no_stylesheets = True
  15.     remove_javascript = True
  16.     use_embedded_content   = False
  17.  
  18.     INDEX = 'http://sportsillustrated.cnn.com/'
  19.  
  20.     def parse_index(self):
  21.         answer = []
  22.         soup = self.index_to_soup(self.INDEX)
  23.         # Find the link to the current issue on the front page. SI Cover
  24.         cover = soup.find('img', attrs = {'alt' : 'Read All Articles', 'style' : 'vertical-align:bottom;'})
  25.         if cover:
  26.             currentIssue = cover.parent['href']
  27.             if currentIssue:
  28.                 # Open the index of current issue
  29.  
  30.                 index = self.index_to_soup(currentIssue)
  31.                 self.log('\tLooking for current issue in: ' + currentIssue)
  32.                 # Now let us see if they updated their frontpage
  33.                 nav = index.find('div', attrs = {'class': 'siv_trav_top'})
  34.                 if nav:
  35.                     img = nav.find('img', attrs = {'src': 'http://i.cdn.turner.com/sivault/.element/img/1.0/btn_next_v2.jpg'})
  36.                     if img:
  37.                         parent = img.parent
  38.                         if parent.name == 'a':
  39.                             # They didn't update their frontpage; Load the next issue from here
  40.                             href = self.INDEX + parent['href']
  41.                             index = self.index_to_soup(href)
  42.                             self.log('\tLooking for current issue in: ' + href)
  43.  
  44.                 if index.find('div', 'siv_noArticleMessage'):
  45.                     nav = index.find('div', attrs = {'class': 'siv_trav_top'})
  46.                     if nav:
  47.                     # Their frontpage points to an issue without any articles; Use the previous issue
  48.                         img = nav.find('img', attrs = {'src': 'http://i.cdn.turner.com/sivault/.element/img/1.0/btn_previous_v2.jpg'})
  49.                         if img:
  50.                             parent = img.parent
  51.                             if parent.name == 'a':
  52.                                 href = self.INDEX + parent['href']
  53.                                 index = self.index_to_soup(href)
  54.                                 self.log('\tLooking for current issue in: ' + href)
  55.  
  56.  
  57.                 # Find all articles.
  58.                 list = index.find('div', attrs = {'class' : 'siv_artList'})
  59.                 if list:
  60.                     articles = []
  61.                     # Get all the artcles ready for calibre.
  62.                     for headline in list.findAll('div', attrs = {'class' : 'headline'}):
  63.                         title = self.tag_to_string(headline.a) + '\n' + self.tag_to_string(headline.findNextSibling('div', attrs = {'class' : 'info'}))
  64.                         url = self.INDEX + headline.a['href']
  65.                         description = self.tag_to_string(headline.findNextSibling('a').div)
  66.                         article = {'title' : title, 'date' : u'', 'url'  : url, 'description' : description}
  67.  
  68.                         articles.append(article)
  69.  
  70.                     # See if we can find a meaningfull title
  71.                     feedTitle = 'Current Issue'
  72.                     hasTitle = index.find('div', attrs = {'class' : 'siv_imageText_head'})
  73.                     if hasTitle :
  74.                         feedTitle = self.tag_to_string(hasTitle.h1)
  75.  
  76.                     answer.append([feedTitle, articles])
  77.  
  78.         return answer
  79.  
  80.  
  81.     def print_version(self, url) :
  82.         # This is the url and the parameters that work to get the print version.
  83.         printUrl = 'http://si.printthis.clickability.com/pt/printThis?clickMap=printThis'
  84.         printUrl += '&fb=Y&partnerID=2356&url=' + quote(url)
  85.  
  86.         return printUrl
  87.  
  88.         # However the original javascript also uses the following parameters, but they can be left out:
  89.         #   title : can be some random string
  90.         #   random : some random number, but I think the number of digits is important
  91.         #   expire : no idea what value to use
  92.         # All this comes from the Javascript function that redirects to the print version. It's called PT() and is defined in the file 48.js
  93.  
  94.     def preprocess_html(self, soup):
  95.         header = soup.find('div', attrs = {'class' : 'siv_artheader'})
  96.         homeMadeSoup = BeautifulSoup('<html><head></head><body></body></html>')
  97.         body = homeMadeSoup.body
  98.  
  99.         # Find the date, title and byline
  100.         temp = header.find('td', attrs = {'class' : 'title'})
  101.         if temp :
  102.             date = temp.find('div', attrs = {'class' : 'date'})
  103.             if date:
  104.                 body.append(date)
  105.             if temp.h1:
  106.                 body.append(temp.h1)
  107.             if temp.h2 :
  108.                 body.append(temp.h2)
  109.             byline = temp.find('div', attrs = {'class' : 'byline'})
  110.             if byline:
  111.                 body.append(byline)
  112.  
  113.         # Find the content
  114.         for para in soup.findAll('div', attrs = {'class' : 'siv_artpara'}) :
  115.             body.append(para)
  116.  
  117.         return homeMadeSoup
  118.  
  119.