Upload booru-get-json.py
Browse files- booru-get-json.py +35 -0
booru-get-json.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
import time
|
6 |
+
import argparse
|
7 |
+
import pybooru
|
8 |
+
from tqdm import tqdm
|
9 |
+
|
10 |
+
pybooru.resources.SITE_LIST["shima"] = {"url": "https://shima.donmai.us/"} # get around the government censorship
|
11 |
+
client = pybooru.Danbooru('shima')
|
12 |
+
|
13 |
+
parser = argparse.ArgumentParser(description='Get images from danbooru')
|
14 |
+
parser.add_argument('start', type=int)
|
15 |
+
parser.add_argument('end', type=int)
|
16 |
+
args = parser.parse_args()
|
17 |
+
|
18 |
+
|
19 |
+
for id in tqdm(range(args.start, args.end)):
|
20 |
+
folder = str(int(id / 10000))
|
21 |
+
json_path = os.path.join(folder, f"{id}.json")
|
22 |
+
if not (os.path.exists(json_path) and os.path.getsize(json_path) != 0):
|
23 |
+
try:
|
24 |
+
time.sleep(0.25)
|
25 |
+
json_data = client.post_show(id)
|
26 |
+
os.makedirs(folder, exist_ok=True)
|
27 |
+
with open(json_path, "w") as f:
|
28 |
+
json.dump(json_data, f)
|
29 |
+
except Exception as e:
|
30 |
+
str_e = str(e)
|
31 |
+
if not ("In _request: 404 - Not Found" in str_e and str_e.endswith(".json")):
|
32 |
+
os.makedirs("errors", exist_ok=True)
|
33 |
+
error_file = open(f"errors/errors_json{args.start}.txt", 'a')
|
34 |
+
error_file.write(f"ERROR: {id} MESSAGE: {str_e}\n")
|
35 |
+
error_file.close()
|