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

  1. #!/usr/bin/env  python
  2.  
  3. __license__   = 'GPL v3'
  4.  
  5. '''
  6. www.canada.com
  7. '''
  8.  
  9. from calibre.web.feeds.recipes import BasicNewsRecipe
  10.  
  11.  
  12. class CanWestPaper(BasicNewsRecipe):
  13.  
  14.     # un-comment the following three lines for the Vancouver Sun
  15.     title = u'Vancouver Sun'
  16.     url_prefix = 'http://www.vancouversun.com'
  17.     description = u'News from Vancouver, BC'
  18.  
  19.     # un-comment the following three lines for the Edmonton Journal
  20.     #title = u'Edmonton Journal'
  21.     #url_prefix = 'http://www.edmontonjournal.com'
  22.     #description = u'News from Edmonton, AB'
  23.  
  24.     # un-comment the following three lines for the Calgary Herald
  25.     #title = u'Calgary Herald'
  26.     #url_prefix = 'http://www.calgaryherald.com'
  27.     #description = u'News from Calgary, AB'
  28.  
  29.     # un-comment the following three lines for the Regina Leader-Post
  30.     #title = u'Regina Leader-Post'
  31.     #url_prefix = 'http://www.leaderpost.com'
  32.     #description = u'News from Regina, SK'
  33.  
  34.     # un-comment the following three lines for the Saskatoon Star-Phoenix
  35.     #title = u'Saskatoon Star-Phoenix'
  36.     #url_prefix = 'http://www.thestarphoenix.com'
  37.     #description = u'News from Saskatoon, SK'
  38.  
  39.     # un-comment the following three lines for the Windsor Star
  40.     #title = u'Windsor Star'
  41.     #url_prefix = 'http://www.windsorstar.com'
  42.     #description = u'News from Windsor, ON'
  43.  
  44.     # un-comment the following three lines for the Ottawa Citizen
  45.     #title = u'Ottawa Citizen'
  46.     #url_prefix = 'http://www.ottawacitizen.com'
  47.     #description = u'News from Ottawa, ON'
  48.  
  49.     # un-comment the following three lines for the Montreal Gazette
  50.     #title = u'Montreal Gazette'
  51.     #url_prefix = 'http://www.montrealgazette.com'
  52.     #description = u'News from Montreal, QC'
  53.  
  54.  
  55.     language = 'en_CA'
  56.     __author__ = 'Nick Redding'
  57.     no_stylesheets = True
  58.     timefmt = ' [%b %d]'
  59.     extra_css = '''
  60.                 .timestamp {  font-size:xx-small; display: block; }
  61.                 #storyheader { font-size: medium; }
  62.                 #storyheader h1 { font-size: x-large; }
  63.                 #storyheader h2 { font-size: large;  font-style: italic; }
  64.                 .byline { font-size:xx-small; }
  65.                 #photocaption { font-size: small; font-style: italic }
  66.                 #photocredit { font-size: xx-small; }'''
  67.     keep_only_tags = [dict(name='div', attrs={'id':'storyheader'}),dict(name='div', attrs={'id':'storycontent'})]
  68.     remove_tags = [{'class':'comments'},
  69.                    dict(name='div', attrs={'class':'navbar'}),dict(name='div', attrs={'class':'morelinks'}),
  70.                    dict(name='div', attrs={'class':'viewmore'}),dict(name='li', attrs={'class':'email'}),
  71.                    dict(name='div', attrs={'class':'story_tool_hr'}),dict(name='div', attrs={'class':'clear'}),
  72.                    dict(name='div', attrs={'class':'story_tool'}),dict(name='div', attrs={'class':'copyright'}),
  73.                    dict(name='div', attrs={'class':'rule_grey_solid'}),
  74.                    dict(name='li', attrs={'class':'print'}),dict(name='li', attrs={'class':'share'}),dict(name='ul', attrs={'class':'bullet'})]
  75.  
  76.     def preprocess_html(self,soup):
  77.         #delete iempty id attributes--they screw up the TOC for unknow reasons
  78.         divtags = soup.findAll('div',attrs={'id':''})
  79.         if divtags:
  80.             for div in divtags:
  81.                 del(div['id'])
  82.         return soup
  83.  
  84.  
  85.     def parse_index(self):
  86.         soup = self.index_to_soup(self.url_prefix+'/news/todays-paper/index.html')
  87.  
  88.         articles = {}
  89.         key = 'News'
  90.         ans = ['News']
  91.  
  92.         # Find each instance of class="sectiontitle", class="featurecontent"
  93.         for divtag in soup.findAll('div',attrs={'class' : ["section_title02","featurecontent"]}):
  94.                 #self.log(" div class = %s" % divtag['class'])
  95.                 if divtag['class'].startswith('section_title'):
  96.                     # div contains section title
  97.                     if not divtag.h3:
  98.                         continue
  99.                     key = self.tag_to_string(divtag.h3,False)
  100.                     ans.append(key)
  101.                     self.log("Section name %s" % key)
  102.                     continue
  103.                 # div contains article data
  104.                 h1tag = divtag.find('h1')
  105.                 if not h1tag:
  106.                     continue
  107.                 atag = h1tag.find('a',href=True)
  108.                 if not atag:
  109.                     continue
  110.                 url = self.url_prefix+'/news/todays-paper/'+atag['href']
  111.                 #self.log("Section %s" % key)
  112.                 #self.log("url %s" % url)
  113.                 title = self.tag_to_string(atag,False)
  114.                 #self.log("title %s" % title)
  115.                 pubdate = ''
  116.                 description = ''
  117.                 ptag = divtag.find('p');
  118.                 if ptag:
  119.                     description = self.tag_to_string(ptag,False)
  120.                     #self.log("description %s" % description)
  121.                 author = ''
  122.                 autag = divtag.find('h4')
  123.                 if autag:
  124.                     author = self.tag_to_string(autag,False)
  125.                     #self.log("author %s" % author)
  126.                 if not articles.has_key(key):
  127.                     articles[key] = []
  128.                 articles[key].append(dict(title=title,url=url,date=pubdate,description=description,author=author,content=''))
  129.  
  130.         ans = [(key, articles[key]) for key in ans if articles.has_key(key)]
  131.         return ans
  132.