python danbooru_download.py <tags> [output_path]
Tags: maximum of 2 tags Output path: defaults to a path based on the tags if not specified
import sys, os, requests, json, time, math | |
if len(sys.argv) < 2: | |
print("Tags not specified") | |
sys.exit(1) | |
elif sys.argv[1].count(" ") > 1: | |
print("Maximum of 2 arguments") | |
sys.exit(1) | |
tags = sys.argv[1] | |
tags_url = tags.replace(" ", "+") | |
output_path = "./" | |
if len(sys.argv) < 3: | |
output_path += "".join([c for c in sys.argv[1] if c.isalpha() or c.isdigit() or c == " " or c == "(" or c == ")" or c == "_"]).rstrip().replace(" ", "_") | |
else: | |
output_path = sys.argv[2] | |
try: | |
os.mkdir(output_path) | |
except FileExistsError: | |
pass | |
print("Tags:\t\t{0}".format(tags)) | |
print("Output:\t\t{0}".format(output_path)) | |
post_count = json.loads(requests.get("https://danbooru.donmai.us/counts/posts.json?tags={0}".format(tags_url)).text)["counts"]["posts"] | |
print("# of posts:\t{0}".format(post_count)) | |
page_count = math.ceil(post_count / 200) | |
count = 0 | |
start = time.time() | |
for p in range(page_count): | |
posts = json.loads(requests.get("https://danbooru.donmai.us/posts.json?tags={0}&page={1}&limit=200".format(tags_url, p + 1)).text) | |
for post in posts: | |
count += 1 | |
try: | |
print("Downloading {0}, {1} of {2}, elapsed: {3} seconds".format(post["id"], count, post_count, round(time.time() - start, 2))) | |
image_fname = post["file_url"][post["file_url"].rfind("/") + 1:] | |
image = requests.get(post["file_url"]).content | |
except KeyError: | |
print("Skipping {0}, {1} of {2}, elapsed: {3} seconds".format(post["id"], count, post_count, round(time.time() - start, 2))) | |
f = open("{0}/{1}".format(output_path, image_fname), "wb") | |
f.write(image) | |
f.close() | |
print("Downloaded {0} posts in {1} seconds".format(count, round(time.time() - start, 2))) |