-
Notifications
You must be signed in to change notification settings - Fork 8
/
gel_fetch.py
196 lines (137 loc) · 5.56 KB
/
gel_fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import shutil
from GelbooruViewer import GelbooruViewer
from urllib.request import urlretrieve, build_opener, install_opener
from PIL import Image
def is_video(url):
return (url.endswith(".webm") or
url.endswith(".gif"))
def split_animation(url, image_id, output, fps):
import subprocess
try:
subprocess.run("ffmpeg -i '{}' -vf fps={} '{}/{}_frame%05d.png' -hide_banner".format(
url, fps, output, image_id), shell=True, check=True)
except Exception:
return []
frame_files = [f for f in os.listdir(
output) if f.endswith('.png') and image_id in f]
return frame_files
def get_artists_from_path(path):
dirs = next(os.walk(path, followlinks=True))[1]
return dirs
def get_images(txt_out, img_out, tags, num=1000, start_id=0, no_animated=False, fps=5,
danbooru=False):
opener = build_opener()
opener.addheaders = [
('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
install_opener(opener)
if txt_out is None:
txt_out = os.path.join(img_out, "tags")
os.makedirs(txt_out, exist_ok=True)
os.makedirs(img_out, exist_ok=True)
count = 0
posts = []
if not danbooru:
viewer = GelbooruViewer()
pictures = viewer.get_all(tags=tags, num=num, pid=start_id, limit=100)
if pictures is None:
print(f"no images for tags: {tags}")
return
for pic in pictures:
img_id = pic.picture_id
tags = ", ".join(pic.tags)
url = pic.file_url
url = url.replace("https:https", "https")
posts.append((url, pic.tags, img_id))
else:
from pybooru import Danbooru
client = Danbooru('danbooru')
pictures = client.post_list(tags=tags, limit=num)
if pictures is None:
print(f"no images for tags: {tags}")
return
for pic in pictures:
if "large_file_url" not in pic:
continue
img_id = pic["id"]
tags = pic["tag_string"].split(" ")
posts.append((pic['large_file_url'],
", ".join(tags),
img_id))
for url, tags, img_id in posts:
print(url)
fn, ext = os.path.splitext(url)
img_out_tmp = os.path.join(img_out, "{}{}".format(img_id, ext))
txt_out_tmp = os.path.join(txt_out, "{}.txt".format(img_id))
if os.path.exists(txt_out_tmp):
print('skipping {} because it already exists'.format(img_id))
continue
count += 1
with open(txt_out_tmp, 'w') as f:
if isinstance(tags, list):
tags = ", ".join(tags)
f.write(tags)
if not no_animated and ("animated" in tags or is_video(url)):
frames = split_animation(url, img_id, img_out, fps)
count += len(frames)
for frame in frames:
bn = os.path.basename(frame)
bn = bn.replace("png", "txt")
shutil.copy2(txt_out_tmp, os.path.join(txt_out, bn))
elif not is_video(url):
try:
urlretrieve(url, img_out_tmp)
# Verify the image is actually an image...
try:
Image.open(img_out_tmp)
except:
print(f"Image {img_out_tmp} probably didn't download correctly. Removing...")
os.remove(img_out_tmp)
os.remove(txt_out_tmp)
except Exception:
pass
return count
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--txt', dest='txt_out', help='txt output directory')
parser.add_argument('--img', dest='img_out', help='image output directory')
parser.add_argument('--num', dest="num", type=int,
help='number of images to fetch',
default=1000)
parser.add_argument('--start_id', dest='start_id', type=int, default=0)
parser.add_argument('--url', default=None)
parser.add_argument('--tags', nargs="+")
parser.add_argument('--no_animated', action="store_true")
parser.add_argument('--fps', type=int, default=5,
help="number of frames for animation")
parser.add_argument('--update_artists', action='store_true')
parser.add_argument('--danbooru', action='store_true')
args = parser.parse_args()
tags = args.tags
jobs = [(args.txt_out, args.img_out, tags)]
GelbooruViewer.API_URL = args.url
if args.url == "realbooru":
GelbooruViewer.API_URL = "https://realbooru.com/index.php?page=dapi&s=post&q=index"
elif args.url is None:
GelbooruViewer.API_URL = "https://rule34.xxx/index.php?page=dapi&s=post&q=index"
if args.update_artists:
artists = get_artists_from_path(args.img_out)
print(artists)
jobs = []
for artist in artists:
txt_out = os.path.join(args.img_out, artist, "txts")
img_out = os.path.join(args.img_out, artist, "imgs")
job = (txt_out, img_out, [artist])
print(job)
jobs.append(job)
count = 0
for job in jobs:
txt_out, img_out, tags = job
added = get_images(txt_out, img_out, tags, args.num, args.start_id,
args.no_animated, args.fps, args.danbooru)
if added is not None:
count += added
print(f"added {count} images")
if __name__ == '__main__':
main()