2016-04-12 21:11:39 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-20 20:41:04 +00:00
|
|
|
# Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs
|
2014-01-05 15:50:57 +00:00
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2016-04-12 21:11:39 +00:00
|
|
|
# Copyright (C) 2015-2016 Tobias Gruetzmacher
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
2012-11-21 20:57:26 +00:00
|
|
|
|
2012-06-20 19:58:13 +00:00
|
|
|
from re import compile
|
|
|
|
|
2012-10-11 10:03:12 +00:00
|
|
|
from ..scraper import _BasicScraper
|
|
|
|
from ..helpers import bounceStarter
|
2012-11-26 06:13:32 +00:00
|
|
|
from ..util import tagre
|
2012-10-11 10:03:12 +00:00
|
|
|
|
2012-06-20 19:58:13 +00:00
|
|
|
|
2016-04-05 22:47:47 +00:00
|
|
|
class Xkcd(_BasicScraper):
|
|
|
|
name = 'xkcd'
|
2013-02-04 20:00:26 +00:00
|
|
|
url = 'http://xkcd.com/'
|
2016-04-13 18:01:51 +00:00
|
|
|
starter = bounceStarter
|
2013-02-04 20:00:26 +00:00
|
|
|
stripUrl = url + '%s/'
|
2013-04-10 21:57:09 +00:00
|
|
|
firstStripUrl = stripUrl % '1'
|
2016-04-05 22:47:47 +00:00
|
|
|
imageSearch = compile(tagre("img", "src",
|
|
|
|
r'(//imgs\.xkcd\.com/comics/[^"]+)'))
|
2012-11-26 06:13:32 +00:00
|
|
|
prevSearch = compile(tagre("a", "href", r'(/\d+/)', before="prev"))
|
2016-04-12 21:11:39 +00:00
|
|
|
nextSearch = compile(tagre("a", "href", r'(/\d+/)', before="next"))
|
2012-06-20 19:58:13 +00:00
|
|
|
help = 'Index format: n (unpadded)'
|
2016-04-05 22:47:47 +00:00
|
|
|
textSearch = compile(tagre("img", "title", r'([^"]+)',
|
|
|
|
before=r'//imgs\.xkcd\.com/comics/'))
|
2012-06-20 19:58:13 +00:00
|
|
|
|
|
|
|
@classmethod
|
2016-04-05 22:47:47 +00:00
|
|
|
def namer(cls, image_url, page_url):
|
|
|
|
index = int(page_url.rstrip('/').rsplit('/', 1)[-1])
|
|
|
|
name = image_url.rsplit('/', 1)[-1].split('.')[0]
|
2012-11-26 06:13:32 +00:00
|
|
|
return '%03d-%s' % (index, name)
|
2013-12-04 16:56:54 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def imageUrlModifier(cls, url, data):
|
|
|
|
if url and '/large/' in data:
|
|
|
|
return url.replace(".png", "_large.png")
|
|
|
|
return url
|
2016-04-05 22:47:47 +00:00
|
|
|
|
|
|
|
def shouldSkipUrl(self, url, data):
|
|
|
|
return url in (
|
|
|
|
self.stripUrl % '1663', # Garden
|
|
|
|
)
|