fedibooru-bot/fedibooru.sh

53 lines
1.4 KiB
Bash
Executable File

#!/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 % 6300 + 1 ]
query="$querybase&tags=$taglist&pid=$randnum"
json=$(curl "$query" 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=$([ -z $imgsource ] || echo source: $imgsource)" -F "sensitive=$(get_conf sensitive)" \
-F "visibility=$(get_conf visibility)" > /dev/null 2> /dev/null