danbooru-cleaned / SUBSETS /dan-download.sh
ppbrown's picture
Rename dan-download.sh to SUBSETS/dan-download.sh
9baa005 verified
#!/bin/bash
# Replace with your danbooru login and API key
LOGIN="youridhere"
API_KEY="your_api_key_here_xxxxx"
# This script takes filenames
# on stdin, and then downloads from danbooru to local directories,
# matching the format.
# The format should be:
# data-xxxx/filename.jpg
#########################################################################
#
# script internals only, beyond this point.
#########################################################################
# This func takes an image pathname, and calculates the
# danbooru image id from the filename.
# It then grabs the original image,
# and the tag info (for .txt), if the jpg does not already exist
download_image(){
[[ -f $1 ]] && return
# strip off leading directory names, and file extensions
id=${1##*/}
id=${id%*.???}
IMAGE_ID=$id
imgfile=$IMAGE_ID.jpg
POST_DATA=$(curl -q -s -u "$LOGIN:$API_KEY" "https://danbooru.donmai.us/posts/$IMAGE_ID.json")
# debug
#echo $POST_DATA >/tmp/post_data.json
IMAGE_URL=$(echo $POST_DATA | jq -r '.file_url')
# Using _general gives us the non-artist/character tags
# Remove _general if you want that included
TAG_INFO=$(echo $POST_DATA | jq -r '.tag_string_general')
if [[ "$IMAGE_URL" == "null" ]] ; then
echo ERROR RETREIVING POST
echo DEBUG:
echo $POST_DATA
else
echo Downloading to $imgfile
curl -s "$IMAGE_URL" > $imgfile
tagfile=$IMAGE_ID.txt
[[ -f $tagfile ]] || echo "$TAG_INFO" >$tagfile
fi
}
while read oneline ; do
download_image $oneline
done