home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_280 / calgary_herald.recipe < prev    next >
Text File  |  2011-09-09  |  5KB  |  125 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 Calgary Herald
  15.     title = u'Calgary Herald'
  16.     url_prefix = 'http://www.calgaryherald.com'
  17.     description = u'News from Calgary, AB'
  18.  
  19.     # un-comment the following three lines for the Regina Leader-Post
  20.     #title = u'Regina Leader-Post'
  21.     #url_prefix = 'http://www.leaderpost.com'
  22.     #description = u'News from Regina, SK'
  23.  
  24.     # un-comment the following three lines for the Saskatoon Star-Phoenix
  25.     #title = u'Saskatoon Star-Phoenix'
  26.     #url_prefix = 'http://www.thestarphoenix.com'
  27.     #description = u'News from Saskatoon, SK'
  28.  
  29.     # un-comment the following three lines for the Windsor Star
  30.     #title = u'Windsor Star'
  31.     #url_prefix = 'http://www.windsorstar.com'
  32.     #description = u'News from Windsor, ON'
  33.  
  34.     # un-comment the following three lines for the Ottawa Citizen
  35.     #title = u'Ottawa Citizen'
  36.     #url_prefix = 'http://www.ottawacitizen.com'
  37.     #description = u'News from Ottawa, ON'
  38.  
  39.     # un-comment the following three lines for the Montreal Gazette
  40.     #title = u'Montreal Gazette'
  41.     #url_prefix = 'http://www.montrealgazette.com'
  42.     #description = u'News from Montreal, QC'
  43.  
  44.  
  45.     language = 'en_CA'
  46.     __author__ = 'Nick Redding'
  47.     encoding = 'latin1'
  48.     no_stylesheets = True
  49.     timefmt = ' [%b %d]'
  50.     extra_css = '''
  51.                 .timestamp {  font-size:xx-small; display: block; }
  52.                 #storyheader { font-size: medium; }
  53.                 #storyheader h1 { font-size: x-large; }
  54.                 #storyheader h2 { font-size: large;  font-style: italic; }
  55.                 .byline { font-size:xx-small; }
  56.                 #photocaption { font-size: small; font-style: italic }
  57.                 #photocredit { font-size: xx-small; }'''
  58.     keep_only_tags = [dict(name='div', attrs={'id':'storyheader'}),dict(name='div', attrs={'id':'storycontent'})]
  59.     remove_tags = [{'class':'comments'},
  60.                    dict(name='div', attrs={'class':'navbar'}),dict(name='div', attrs={'class':'morelinks'}),
  61.                    dict(name='div', attrs={'class':'viewmore'}),dict(name='li', attrs={'class':'email'}),
  62.                    dict(name='div', attrs={'class':'story_tool_hr'}),dict(name='div', attrs={'class':'clear'}),
  63.                    dict(name='div', attrs={'class':'story_tool'}),dict(name='div', attrs={'class':'copyright'}),
  64.                    dict(name='div', attrs={'class':'rule_grey_solid'}),
  65.                    dict(name='li', attrs={'class':'print'}),dict(name='li', attrs={'class':'share'}),dict(name='ul', attrs={'class':'bullet'})]
  66.  
  67.     def preprocess_html(self,soup):
  68.         #delete iempty id attributes--they screw up the TOC for unknow reasons
  69.         divtags = soup.findAll('div',attrs={'id':''})
  70.         if divtags:
  71.             for div in divtags:
  72.                 del(div['id'])
  73.         return soup
  74.  
  75.  
  76.     def parse_index(self):
  77.         soup = self.index_to_soup(self.url_prefix+'/news/todays-paper/index.html')
  78.  
  79.         articles = {}
  80.         key = 'News'
  81.         ans = ['News']
  82.  
  83.         # Find each instance of class="sectiontitle", class="featurecontent"
  84.         for divtag in soup.findAll('div',attrs={'class' : ["section_title02","featurecontent"]}):
  85.                 #self.log(" div class = %s" % divtag['class'])
  86.                 if divtag['class'].startswith('section_title'):
  87.                     # div contains section title
  88.                     if not divtag.h3:
  89.                         continue
  90.                     key = self.tag_to_string(divtag.h3,False)
  91.                     ans.append(key)
  92.                     self.log("Section name %s" % key)
  93.                     continue
  94.                 # div contains article data
  95.                 h1tag = divtag.find('h1')
  96.                 if not h1tag:
  97.                     continue
  98.                 atag = h1tag.find('a',href=True)
  99.                 if not atag:
  100.                     continue
  101.                 url = atag['href']
  102.                 if not url.startswith('http:'):
  103.                     url = self.url_prefix+'/news/todays-paper/'+atag['href']
  104.                 #self.log("Section %s" % key)
  105.                 #self.log("url %s" % url)
  106.                 title = self.tag_to_string(atag,False)
  107.                 #self.log("title %s" % title)
  108.                 pubdate = ''
  109.                 description = ''
  110.                 ptag = divtag.find('p');
  111.                 if ptag:
  112.                     description = self.tag_to_string(ptag,False)
  113.                     #self.log("description %s" % description)
  114.                 author = ''
  115.                 autag = divtag.find('h4')
  116.                 if autag:
  117.                     author = self.tag_to_string(autag,False)
  118.                     #self.log("author %s" % author)
  119.                 if not articles.has_key(key):
  120.                     articles[key] = []
  121.                 articles[key].append(dict(title=title,url=url,date=pubdate,description=description,author=author,content=''))
  122.  
  123.         ans = [(key, articles[key]) for key in ans if articles.has_key(key)]
  124.         return ans
  125.