Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # usage: e621.py artist1 artist2 etc. to download images of each artist in succession
- # e621.py --update to download new images for existing folders
- import urllib, json, sys, os, os.path
- from threading import Thread
- from Queue import Queue
- def e621(artist):
- # you can edit these three variables to configure the script
- # os.getenv("USERPROFILE") automagically determines where your home folder is on Windows
- dir = os.getenv("USERPROFILE") + "/Furry/"
- # number of CPU threads to use
- threads = 4
- # tags to use every time the script is invoked, max 5,
- # if you don't want to use any just leave default as []
- default = ["-vore", "-gore"]
- if artist:
- dir += artist
- else:
- print "Error: no artist specified."
- sys.exit()
- if len(default) > 5:
- print "Error: too many default option tags. Limit is 5."
- sys.exit()
- try:
- verify = json.loads(urllib.urlopen("http://e621.net/tag/index.json?name=" + artist + "&type=1").read())
- except IOError:
- print "Error: site URL not valid."
- sys.exit()
- else:
- if verify:
- print "Downloading images for artist " + artist
- else:
- print "Error: first tag is not an artist."
- sys.exit()
- if not os.path.exists(dir):
- os.makedirs(dir)
- images = []
- for page in xrange(1, 5000):
- url = json.loads(urllib.urlopen("http://e621.net/post/index.json?tags=" + artist + "+" + "+".join(default) + "&page=" + str(page)).read())
- if url:
- for i in url:
- if not images.count(i[u'file_url']):
- images.append(i[u'file_url'])
- else:
- if page == 1:
- print "Error: no images found for the desired artist."
- sys.exit()
- break
- images.reverse()
- queue = Queue(threads)
- def download(imagelist):
- queue.get()
- for image in imagelist:
- location = dir + "/" + artist + "_" + str(images.index(image) + 1) + "." + image.split(".")[-1]
- if not os.path.isfile(location):
- urllib.urlretrieve("http://e621.net" + image, location)
- queue.task_done()
- for i in [images[i:i+len(images)/threads] for i in xrange(0, len(images), len(images)/threads)]:
- queue.put(Thread(target=download, args=(i,)).start())
- queue.join()
- print str(len(os.listdir(dir))) + " images saved for artist " + artist
- if __name__ == "__main__":
- try:
- if sys.argv[1] == "--update":
- for tag in os.listdir(os.getenv("USERPROFILE") + "/Furry/"):
- e621(tag)
- else:
- for tag in sys.argv[1:]:
- e621(tag)
- except IndexError:
- print "Error: no tags specified."
- except WindowsError:
- print "Error: nothing to update."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement