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