fedibooru-bot/fedibooru.sh

50 lines
1.4 KiB
Bash
Raw Normal View History

2021-04-18 12:13:16 +00:00
#!/bin/bash
json_parser(){
echo "$1" | sed \
-e "s/.*$2...//" \
-e 's/\",\".*//' \
-e "s/.\//\//g" \
-e "s/\&/\&/g"
}
#config=$(cat config)
get_conf(){
str=$(grep "$1" config)
echo ${str#$1=}
}
access_token=$(get_conf access_token)
instance=$(get_conf instance)
img_dir=$(get_conf img_dir)
taglist=$(get_conf tags)
querybase="https://gelbooru.com/index.php?page=dapi&s=post&q=index&limit=1&json=1"
# From 1 to 20.000
randnum=$[ $RANDOM % 20000 + 1 ]
json=$(curl "$querybase&tags=$taglist&pid=$randnum" 2> /dev/null)
# Get image info
imageurl=$(json_parser "$json" "file_url")
imgtags=$(json_parser "$json" "tags")
imgsource=$(json_parser "$json" "source")
[ -d "$img_dir" ] || mkdir "$img_dir" || (echo "Error creating image folder" && exit)
wget "$imageurl" -P "$img_dir" 2> /dev/null || (echo "Could not download image" && exit)
image_json=$( \
curl -X POST "https://$instance/api/v1/media" \
-H "Authorization: Bearer $access_token" \
-F "file=@$img_dir/$(basename "$imageurl")" \
-F "description=$imgtags" 2> /dev/null \
)
id=$(json_parser "$image_json" "id")
curl -X POST "https://$instance/api/v1/statuses" \
-H "Authorization: Bearer $access_token" -F "media_ids[]=$id" \
-F "status=source: $imgsource" -F "sensitive=true" \
-F "visibility=$(get_conf visibility)" > /dev/null 2> /dev/null