Advertisement
Guest User

e621.py

a guest
Aug 10th, 2012
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # usage: e621.py artist1 artist2 etc. to download images of each artist in succession
  4. #    e621.py --update to download new images for existing folders
  5.  
  6. import urllib, json, sys, os, os.path
  7. from threading import Thread
  8. from Queue import Queue
  9.  
  10. def e621(artist):
  11.     # you can edit these three variables to configure the script
  12.     # os.getenv("USERPROFILE") automagically determines where your home folder is on Windows
  13.     dir = os.getenv("USERPROFILE") + "/Furry/"
  14.     # number of CPU threads to use
  15.     threads = 4
  16.     # tags to use every time the script is invoked, max 5,
  17.     # if you don't want to use any just leave default as []
  18.     default = ["-vore", "-gore"]
  19.  
  20.     if artist:
  21.         dir += artist
  22.     else:
  23.         print "Error: no artist specified."
  24.         sys.exit()
  25.  
  26.     if len(default) > 5:
  27.         print "Error: too many default option tags. Limit is 5."
  28.         sys.exit()
  29.  
  30.     try:
  31.         verify = json.loads(urllib.urlopen("http://e621.net/tag/index.json?name=" + artist + "&type=1").read())
  32.     except IOError:
  33.         print "Error: site URL not valid."
  34.         sys.exit()
  35.     else:
  36.         if verify:
  37.             print "Downloading images for artist " + artist
  38.         else:
  39.             print "Error: first tag is not an artist."
  40.             sys.exit()
  41.  
  42.     if not os.path.exists(dir):
  43.         os.makedirs(dir)
  44.  
  45.     images = []
  46.     for page in xrange(1, 5000):
  47.         url = json.loads(urllib.urlopen("http://e621.net/post/index.json?tags=" + artist + "+" + "+".join(default) + "&page=" + str(page)).read())
  48.         if url:
  49.             for i in url:
  50.                 if not images.count(i[u'file_url']):
  51.                     images.append(i[u'file_url'])
  52.         else:
  53.             if page == 1:
  54.                 print "Error: no images found for the desired artist."
  55.                 sys.exit()
  56.             break
  57.  
  58.     images.reverse()
  59.     queue = Queue(threads)
  60.  
  61.     def download(imagelist):
  62.         queue.get()
  63.         for image in imagelist:
  64.             location = dir + "/" + artist + "_" + str(images.index(image) + 1) + "." + image.split(".")[-1]
  65.             if not os.path.isfile(location):
  66.                 urllib.urlretrieve("http://e621.net" + image, location)
  67.         queue.task_done()
  68.  
  69.     for i in [images[i:i+len(images)/threads] for i in xrange(0, len(images), len(images)/threads)]:
  70.         queue.put(Thread(target=download, args=(i,)).start())
  71.  
  72.     queue.join()
  73.     print str(len(os.listdir(dir))) + " images saved for artist " + artist
  74.  
  75. if __name__ == "__main__":
  76.     try:
  77.         if sys.argv[1] == "--update":
  78.             for tag in os.listdir(os.getenv("USERPROFILE") + "/Furry/"):
  79.                 e621(tag)
  80.         else:
  81.             for tag in sys.argv[1:]:
  82.                 e621(tag)
  83.     except IndexError:
  84.         print "Error: no tags specified."
  85.     except WindowsError:
  86.         print "Error: nothing to update."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement