Debian Codegraph @

recipes/xkcd.recipe

raw
1 __license__ = 'GPL v3'
2 __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
3 '''
4 Changelog:
5 2015-07-29
6 Fixed issue with 'a' tags outside of list of articles (cjp)
7 2012-04-06
8 Fixed empty articles, added masthead img (NiLuJe)
9 2011-09-24
10 Changed cover (drMerry)
11 '''
12 '''
13 Fetch xkcd.
14 '''
15
16 import re
17 import time
18
19 from calibre.web.feeds.news import BasicNewsRecipe
20
21
22 class XkcdCom(BasicNewsRecipe):
23 cover_url = 'http://imgs.xkcd.com/static/terrible_small_logo.png'
24 masthead_url = 'http://imgs.xkcd.com/static/terrible_small_logo.png'
25 title = 'xkcd'
26 description = 'A webcomic of romance and math humor.'
27 __author__ = 'Martin Pitt updated by DrMerry.'
28 language = 'en'
29
30 use_embedded_content = False
31 oldest_article = 60
32 keep_only_tags = [dict(id='comic')]
33 no_stylesheets = True
34 # turn image bubblehelp into a paragraph, and put alt in a heading
35 preprocess_regexps = [
36 (
37 re.compile(r'(<img.*title=")([^"]+)(".alt=")([^"]+)(".*>)'),
38 lambda m: '<h1>%s</h1>%s%s%s<p>%s</p>' % (m.group(4), m.group(1), m.group(3), m.group(5), m.group(2)),
39 )
40 ]
41
42 def parse_index(self):
43 INDEX = 'http://xkcd.com/archive/'
44
45 soup = self.index_to_soup(INDEX)
46 articles = []
47 for item in soup.find('div', attrs={'id': 'middleContainer'}).findAll('a', title=True):
48 articles.append({
49 'date': item['title'],
50 'timestamp': time.mktime(time.strptime(item['title'], '%Y-%m-%d')) + 1,
51 'url': 'http://xkcd.com' + item['href'],
52 'title': self.tag_to_string(item),
53 'description': '',
54 'content': '',
55 })
56
57 return [('xkcd', articles)]