Mrowqa

TwoKinds episode downloader

May 22nd, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # TwoKinds episode downloader
  4. # Version 1.5
  5. # 2014 (C) Copyright by Mrowqa, http://mrowqa.pl
  6.  
  7. import urllib2, re, glob
  8.  
  9. ARCHIVE_BASE_URL = 'http://twokinds.keenspot.com/archive.php?p='
  10. IMG_URL_REGEX = r'http://cdn\.twokinds\.keenspot\.com/comics/[\w\-]+\.\w+'
  11. HTTP_HEADERS = {'User-Agent': 'ponies will rule the whole world!'}
  12. FILENAME_SCHEMA = 'twokinds_#%04d.%s'
  13.  
  14.  
  15.  
  16. print "Type episodes' numbers (you can use commas and dashes, for instance: 1,3-5,75,85-101): "
  17. ids_raw = raw_input() + ","
  18. while not re.match(r"^(\s*\d+(\s*\-\s*\d+)?\s*,)+$", ids_raw):
  19.     print "Invalid input. Try again: "
  20.     ids_raw = raw_input() + ","
  21.    
  22. ids = []
  23. for number in ids_raw.split(",")[:-1]:
  24.     number = number.split("-")
  25.     if len(number) == 1:
  26.         ids += [int(number[0])]
  27.     else:
  28.         from_id, to_id = int(number[0]), int(number[1])
  29.         if from_id > to_id: from_id, to_id = to_id, from_id
  30.         ids += range(from_id, to_id+1)
  31.  
  32. ids = set(ids)
  33.  
  34.  
  35. print "Starting dowloading %d episodes ..." % len(ids)
  36. downloaded_cnt = 0
  37.  
  38. for id in ids:
  39.     print "\nID = %d" % id
  40.     files = glob.glob(FILENAME_SCHEMA % (id, "*"))
  41.     if files != []:
  42.         print "Already existing files (listed below)! Skipping ...\n" + str(files)
  43.         downloaded_cnt += 1
  44.         continue
  45.     url = ARCHIVE_BASE_URL + str(id)
  46.     try:
  47.         print "Requesting %s ..." % url
  48.         page_content = urllib2.urlopen(url, timeout=5).read()
  49.         images_urls = re.findall(IMG_URL_REGEX, page_content)
  50.         if len(images_urls) != 2: # currect image + preload next
  51.             if not ( len(images_urls) == 1 and page_content.find(ARCHIVE_BASE_URL + str(id-1)) != -1 ): # if not last image
  52.                 print "Unusual number of images (listed below). Skipping ...\n" \
  53.                     "URLs: %s" % str(images_urls)
  54.                 continue
  55.            
  56.         img_url = images_urls[0]
  57.         img_extension = img_url.split('.')[-1].lower()
  58.         print "Downloading file %s ..." % img_url
  59.         req = urllib2.Request(img_url, headers=HTTP_HEADERS)
  60.         img_data = urllib2.urlopen(req, timeout=5).read()
  61.        
  62.         filename = FILENAME_SCHEMA % (id, img_extension)
  63.         print "Saving image to %s ..." % filename
  64.         file = open(filename, 'wb')
  65.         file.write(img_data)
  66.         file.close()
  67.        
  68.         downloaded_cnt += 1
  69.     except Exception, e:
  70.         print "Error occured! Message: %s" % str(e)
  71.    
  72.  
  73. print "\n"*3 + "Work done!"
  74. 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