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