Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # TwoKinds episode downloader
- # Version 1.5
- # 2014 (C) Copyright by Mrowqa, http://mrowqa.pl
- import urllib2, re, glob
- ARCHIVE_BASE_URL = 'http://twokinds.keenspot.com/archive.php?p='
- IMG_URL_REGEX = r'http://cdn\.twokinds\.keenspot\.com/comics/[\w\-]+\.\w+'
- HTTP_HEADERS = {'User-Agent': 'ponies will rule the whole world!'}
- FILENAME_SCHEMA = 'twokinds_#%04d.%s'
- print "Type episodes' numbers (you can use commas and dashes, for instance: 1,3-5,75,85-101): "
- ids_raw = raw_input() + ","
- while not re.match(r"^(\s*\d+(\s*\-\s*\d+)?\s*,)+$", ids_raw):
- print "Invalid input. Try again: "
- ids_raw = raw_input() + ","
- ids = []
- for number in ids_raw.split(",")[:-1]:
- number = number.split("-")
- if len(number) == 1:
- ids += [int(number[0])]
- else:
- from_id, to_id = int(number[0]), int(number[1])
- if from_id > to_id: from_id, to_id = to_id, from_id
- ids += range(from_id, to_id+1)
- ids = set(ids)
- print "Starting dowloading %d episodes ..." % len(ids)
- downloaded_cnt = 0
- for id in ids:
- print "\nID = %d" % id
- files = glob.glob(FILENAME_SCHEMA % (id, "*"))
- if files != []:
- print "Already existing files (listed below)! Skipping ...\n" + str(files)
- downloaded_cnt += 1
- continue
- url = ARCHIVE_BASE_URL + str(id)
- try:
- print "Requesting %s ..." % url
- page_content = urllib2.urlopen(url, timeout=5).read()
- images_urls = re.findall(IMG_URL_REGEX, page_content)
- if len(images_urls) != 2: # currect image + preload next
- if not ( len(images_urls) == 1 and page_content.find(ARCHIVE_BASE_URL + str(id-1)) != -1 ): # if not last image
- print "Unusual number of images (listed below). Skipping ...\n" \
- "URLs: %s" % str(images_urls)
- continue
- img_url = images_urls[0]
- img_extension = img_url.split('.')[-1].lower()
- print "Downloading file %s ..." % img_url
- req = urllib2.Request(img_url, headers=HTTP_HEADERS)
- img_data = urllib2.urlopen(req, timeout=5).read()
- filename = FILENAME_SCHEMA % (id, img_extension)
- print "Saving image to %s ..." % filename
- file = open(filename, 'wb')
- file.write(img_data)
- file.close()
- downloaded_cnt += 1
- except Exception, e:
- print "Error occured! Message: %s" % str(e)
- print "\n"*3 + "Work done!"
- print "Downloaded %d episodes from %d (%.2f%%)!" % (downloaded_cnt, len(ids), float(downloaded_cnt) / len(ids) * 100.0)
Add Comment
Please, Sign In to add comment