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

  1. __license__   = 'GPL v3'
  2. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  3.  
  4. '''
  5. Fetch xkcd.
  6. '''
  7.  
  8. import time, re
  9. from calibre.web.feeds.news import BasicNewsRecipe
  10.  
  11. class XkcdCom(BasicNewsRecipe):
  12.     title = 'xkcd'
  13.     description = 'A webcomic of romance and math humor.'
  14.     __author__ = 'Martin Pitt'
  15.     language = 'en'
  16.  
  17.     use_embedded_content   = False
  18.     oldest_article = 60
  19.     keep_only_tags = [dict(id='middleContent')]
  20.     remove_tags = [dict(name='ul'), dict(name='h3'), dict(name='br')]
  21.     no_stylesheets = True
  22.     # turn image bubblehelp into a paragraph
  23.     preprocess_regexps = [
  24.         (re.compile(r'(<img.*title=")([^"]+)(".*>)'),
  25.          lambda m: '%s%s<p>%s</p>' % (m.group(1), m.group(3), m.group(2)))
  26.     ]
  27.  
  28.     def parse_index(self):
  29.         INDEX = 'http://xkcd.com/archive/'
  30.  
  31.         soup = self.index_to_soup(INDEX)
  32.         articles = []
  33.         for item in soup.findAll('a', title=True):
  34.             articles.append({
  35.                 'date': item['title'],
  36.                 'timestamp': time.mktime(time.strptime(item['title'], '%Y-%m-%d'))+1,
  37.                 'url': 'http://xkcd.com' + item['href'],
  38.                 'title': self.tag_to_string(item),
  39.                 'description': '',
  40.                 'content': '',
  41.             })
  42.  
  43.         return [('xkcd', articles)]
  44.