diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2016-05-23 18:00:23 +0200 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2016-05-23 18:00:23 +0200 |
| commit | 158a1cdeb94ac7d6425d2c3b2c612bf0cfc99915 (patch) | |
| tree | 73d0fb1c26f4f1cacccede3353967252047134b0 | |
| download | carnoon-master.tar.gz carnoon-master.zip | |
| -rwxr-xr-x | carnoon.py | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/carnoon.py b/carnoon.py new file mode 100755 index 0000000..45f98af --- /dev/null +++ b/carnoon.py @@ -0,0 +1,93 @@ +#/usr/bin/python2 + +import urllib2 +import random +import Image +import os.path +import re + +class Comic(object): + def __init__(self, comicurl, filenformat, updateurl, updatere): + self.comicurl = comicurl + self.filenformat = filenformat + self.updateurl = updateurl + self.updatere = updatere + self.last = 0 + + def update_last(self): + opener = urllib2.build_opener() + opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko')] + + url = opener.open(self.updateurl) + data = url.read() + url.close() + + result = re.search(self.updatere, data).group('link') # search for last image link + + if(not result.startswith('http')): + result = self.comicurl + result + + self.last = int(re.search(r'(?P<id>\d+)\.(?i)(jpg|png|gif|bmp)$', result).group('id')) # get file name's content before '.' + + def get_link(self, number = -1): + opener = urllib2.build_opener() # for a UA + opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko')] + + if(number == -1): + urlstr = self.randomurl + else: + urlstr = self.comicurl + (self.filenformat % number) + + filere = re.compile(r'.*?[^\s/]+\.(?i)(jpg|png|gif|bmp)$') + if(filere.match(urlstr)): + return urlstr + + url = opener.open(urlstr) + + data = url.read() # download web page + url.close() + + result = re.search(self.updatere, data) # search for image link + + if(result): + return result.group('link') + else: + print "Please check your regular expression: no matches found" + return None + + def display_random(self): + # urlstr: url + # picstr: random number; appended to url + # pref: prefix for file name; e.g. Comics/xkcd/ + # srcre: regular expression in case url + picstr is not a direct link + + urlstr = self.get_link(random.randrange(0, self.last)) # get direct image download link by RE + + fname = "Comics/" + self.comicurl.replace("http://", "").replace("/", "") + int(re.search(r'(?P<id>\d+)\.(?i)(jpg|png|gif|bmp)$', result).group('id')) + + opener = urllib2.build_opener() # for a UA + opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko')] + + directory = "Comics/" + if not os.path.exists(directory): # create subdirectory if needed + os.makedirs(directory) + + if not os.path.isfile(fname): + # download picture and open if not done yet + url = opener.open(urlstr) + data = url.read() + url.close() + + pic = open(fname, 'w') + pic.write(data) + pic.close() + + Image.open(fname).show() + +ruthe = Comic("http://ruthe.de/cartoons/", "strip_%d.jpg", "http://ruthe.de", r'<img src="(?P<link>.*?)" alt="Cartoon" border="0" />') +ruthe.update_last() +ruthe.display_random() + +xkcd = Comic("xkcd.com", "imgs.%s.com/comics/", r'Permanent link to this comic: (?P<link>.*?)') +xkcd.update_last() +xkcd.display_random("/1/") |
