Initial commit

This commit is contained in:
Dendy 2021-04-18 14:13:16 +02:00
commit dc0865f316
Signed by: dendy
GPG Key ID: 0168B35FFD7F608F
4 changed files with 148 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config

18
README.md Normal file
View File

@ -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

49
fedibooru.sh Executable file
View File

@ -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

80
setup.sh Executable file
View File

@ -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