commit dc0865f31692defc895f9f3c897beefae9f34331 Author: Dendy Date: Sun Apr 18 14:13:16 2021 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..04204c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config diff --git a/README.md b/README.md new file mode 100644 index 0000000..5b043fb --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# fedibooru image random image poster bot + +It uses Gelbooru JSON API and some dirty tricks to get the job done. Compatible with any mastodon-api v1 instances (like, for instance, mastodon or pleroma) + +## Dependencies + +A shell and cURL. + +On Ubuntu/Debian you can do +``` +sudo apt install curl +``` +or on arch +``` +sudo pacman -S curl +``` + +To make it post every X minutes or hours, use cron diff --git a/fedibooru.sh b/fedibooru.sh new file mode 100755 index 0000000..08ed018 --- /dev/null +++ b/fedibooru.sh @@ -0,0 +1,49 @@ +#!/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 diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..6ce0fb3 --- /dev/null +++ b/setup.sh @@ -0,0 +1,80 @@ +#!/bin/sh + +json_parser(){ + echo "$1" | sed \ + -e "s/.*$2...//" \ + -e 's/\",\".*//' \ + -e "s/.\//\//g" \ + -e "s/\&/\&/g" +} + +# Initial config + +echo "- Input the instance: (ex: example.instance.xyz):" +read instance +base_url="https://$instance" +perm="read write" +echo "instance=$instance" > config + +echo "\n- Input the name for your bot (ex: Totally SFW image posting bot):" +read botname + + +# Registering the app + +app_json=$( + curl -X POST "$base_url/api/v1/apps" \ + -F "client_name=\"$botname\"" \ + -F "redirect_uris=urn:ietf:wg:oauth:2.0:oob" \ + -F "scope=$perm" \ + 2> /dev/null +) + +client_id=$(json_parser "$app_json" client_id) +client_secret=$(json_parser "$app_json" client_secret) + +echo "client_id=$client_id" >> config +echo "client_secret=$client_secret" >> config + + +# Authorizing the user for the app to use + +echo "\n- Please open the URL below:" +auth_url="$base_url/oauth/authorize?resonse_type=code&client_id=$client_id&redirect_uri=urn:ietf:wg:oauth:2.0:oob&force_login&scope=$perm" +echo $auth_url + +echo "\n- After logging in, paste the code below:" +read code + +auth_json=$( + curl -X POST \ + -F "client_id=$client_id" \ + -F "client_secret=$client_secret" \ + -F 'redirect_uri=urn:ietf:wg:oauth:2.0:oob' \ + -F 'grant_type=authorization_code' \ + -F "code=$code" \ + -F "scope=$perm" \ + "$base_url/oauth/token" \ + 2> /dev/null +) + +access_token=$(json_parser "$auth_json" access_token) +echo $access_token + +echo "access_token=$access_token" >> config + +echo "\n- Which visibility you want the published statuses to be? (public, unlisted, private) [public]:" +read visibility +echo "visiblity=$visibility" >> config + +echo "\n- Mark images as sensitive? (yes, no) [no]" +read sensitive +[ sesnsitive = yes ] && sensitive="true" || sensitive="false" +echo "sensitive=$sensitive" >> config + +echo "tags=yuki_nagato rating:safe" >> config +echo "img_dir=/tmp/imgbot" >> config + +echo "\nAll done! You can now test if it works with './fedibooru.sh'" +echo "Don't forget to change the default tags and review your config in the 'config' file" +chmod +x ./fedibooru.sh