2 if [ "$1" = "-d" ]; then
20 alias ccurl
='curl --retry 100000 --retry-delay 4'
26 wget
-q --show-progress -O "$2".part
"$1"
27 if [ "$?" -ne 0 ]; then
28 echo "\033[31mdownload failed: ${1}\033[0m"
38 #while read -r input <&3
42 cat "$PIPE" |
xargs -r -P 4 -n 2 "$0" --get
48 echo "$2" |
grep "^${1}:" | cut
-s -d' ' -f2-
54 get_attr
"${1}_tags" "$2"
59 echo 'https://gelbooru.com/index.php?page=post&s=list&tags='"$(echo "$@
" | sed 's/ /+/g')"
65 echo "$1"'&pid='"$(($2 * 42))"
70 ccurl
-s "$1" |
egrep -o ' class="thumb">.*' | cut
-s -d'"' -f6 |
sed 's/\&/\&/g' |
sed 's/^/https:/g'
75 printf '%010u' "$(echo "$1" | egrep -o '[^=]+$')"
80 echo "$1" |
egrep -o '<[^<>]+>Original image</a></li>' | cut
-s -d'"' -f2
83 #1: tag type (general artist character copyright)
86 echo "$2" |
egrep -o '<li class="tag-type-'"$1"'"><a[^>]*>[^<>]*</a>[^<>]<a[^>]*>[^<>]+' |
87 cut
-s -d'>' -f5 |
sed 's/#gt;/>/g' |
sed "s/'/'/g" |
sed 's/ /_/g'
91 #2: selection directory
93 local content
="$(ccurl -s "$1")"
94 local image_url
="$(image_addr "$content")"
95 local id
="$(post_id "$post")"
96 local filename
="${id}.${image_url##*.}"
97 local output_file
="${IMG_DIR}/${filename}"
98 if "$WRITE_IMG" && [ ! -f "$output_file" ]; then
100 echo "${image_url} ${output_file}" >&3
102 if "$WRITE_MDATA"; then
103 local metadata_file
="${MDATA_DIR}/${id}"
104 local general_tags
="$(extract_tags general "$content")"
105 local artist_tags
="$(extract_tags artist "$content")"
106 local character_tags
="$(extract_tags character "$content")"
107 local copyright_tags
="$(extract_tags copyright "$content")"
108 local metadata_tags
="$(extract_tags metadata "$content")"
109 if [ "$JSON_FILE" ]; then
111 --arg id "$id" --arg url "$image_url" \
112 --arg gl "$general_tags" --arg at "$artist_tags" \
113 --arg cr "$character_tags" --arg ct "$copyright_tags" \
114 --arg ma "$metadata_tags" \
116 id:$id, url:$url, tags:
118 general:$gl|split("\n"), artist:$at|split("\n"),
119 character:$cr|split("\n"), copyright:$ct|split("\n"),
120 metadata:$ma|split("\n")
122 }')," >> "$JSON_FILE"
124 mkdir
-p "$MDATA_DIR"
125 echo "url: $image_url" > "$metadata_file"
126 echo "filename: $filename" >> "$metadata_file"
127 echo 'general_tags:' $general_tags >> "$metadata_file"
128 echo 'artist_tags:' $artist_tags >> "$metadata_file"
129 echo 'character_tags:' $character_tags >> "$metadata_file"
130 echo 'copyright_tags:' $copyright_tags >> "$metadata_file"
131 echo 'metadata_tags:' $metadata_tags >> "$metadata_file"
141 (download_image
"$post" "$DIR") &
148 if [ "$JSON_FILE" ]; then
149 echo -n '[' > "$JSON_FILE"
151 local list
="$(list_addr $@)"
156 posts
="$(post_addrs "$
(get_page
"$list" "$p")")"
157 (download_posts
$posts) &
161 if [ "$JSON_FILE" ]; then
162 echo -n ']' >> "$JSON_FILE"
163 sed -i 's/,]/]/g' "$JSON_FILE"
195 echo "$i" |
egrep -o '^'"${regex}"'$'
200 query_metadata_files
() {
201 local files
="$(ls "$MDATA_DIR"/*)"
203 for t
in $
(filter
'^[^-].*$' $@
)
205 files
="$(egrep '^[a-z]+_tags:' $files |
206 grep ' '"$t"'\( \|$\)' | cut -s -d ':' -f 1)"
208 for t
in $
(filter
'^-.*$' $@
)
210 files
="$(minus "$files" "$
(egrep '^[a-z]+_tags:' $files |
211 grep ' '"${t#-}"'\( \|$\)' | cut
-s -d ':' -f 1)")"
225 echo "${IMG_DIR}/$(grep '^filename:' "$f" | cut -s -d ' ' -f 2)"
231 (get_data_files $
(query_metadata_files $@
))
239 for t
in $
(grep "$class"'_tags:' $@ | cut
-s -d ':' -f 3 |
sed 's/ /\n/g' |
sort |
uniq)
247 local mfiles
="$(query_metadata_files $@)"
249 list_tag general
$mfiles
250 list_tag artist
$mfiles
251 list_tag character
$mfiles
252 list_tag copyright
$mfiles
253 list_tag metadata
$mfiles
257 *:artist
) echo "\033[31m${i}\033[0m";;
258 *:character
) echo "\033[34m${i}\033[0m";;
259 *:copyright
) echo "\033[33m${i}\033[0m";;
260 *:metadata
) echo "\033[91m${i}\033[0m";;
267 local files
="$(ls "$MDATA_DIR"/*)"
271 local metadata
="$(cat "$f")"
272 local file="${IMG_DIR}/$(get_attr filename "$metadata")"
273 [ "$file" ] && [ ! -f "$file" ] && {
274 local url
="$(get_attr url "$metadata")"
275 [ "$url" ] && echo "${url} ${file}" >&3
282 echo " $0 [-m \033[4mmetadata directory\033[0m] [-o \033[4mouput directory\033[0m] \033[4mtags...\033[0m"
283 echo " $0 --get \033[4murl\033[0m \033[4moutput filename\033[0m"
292 using_download_cluster
() {
293 if "$is_daemon" && [ -p "$PIPE" ]; then
297 trap 'rm -f "$PIPE"' EXIT
303 if [ $# -eq 3 ] && [ "$1" = '--get' ]; then
311 while getopts 'tsqho:m:nj:g' arg
314 o
) IMG_DIR
="$OPTARG";;
338 shift "$(($OPTIND - 1))"
341 tags
) (list_tags $@
);;
342 sync
) (using_download_cluster sync $@
);;
343 get
) (using_download_cluster download_posts $@
);;
344 *) (using_download_cluster download_list $@
);;