2016-04-15 14:14:42 +02:00
|
|
|
#!/bin/bash
|
2016-04-15 15:14:47 +02:00
|
|
|
# Bash captionbot client
|
|
|
|
# Created by Daniil Gentili
|
|
|
|
# Licensed under GPLv3
|
2016-04-15 14:14:42 +02:00
|
|
|
|
2016-04-15 15:14:47 +02:00
|
|
|
help() {
|
|
|
|
echo "captionbot.ai api client.
|
|
|
|
This script will try to recognize the content of any image you give him using Microsoft's captionbot.ai website api.
|
2016-04-15 14:14:42 +02:00
|
|
|
|
2016-04-15 15:14:47 +02:00
|
|
|
Usage: $0 file
|
|
|
|
or
|
|
|
|
Usage: $0 url
|
2016-04-18 19:46:39 +02:00
|
|
|
|
|
|
|
Add the norate parameter to avoid the rating prompt:
|
|
|
|
Usage: $0 url norate
|
|
|
|
|
|
|
|
Add the script parameter to avoid the rating prompt and delete all output apart from the image description:
|
|
|
|
Usage: $0 url script
|
2016-04-15 15:14:47 +02:00
|
|
|
"
|
|
|
|
exit 1
|
|
|
|
}
|
2016-04-15 19:56:22 +02:00
|
|
|
[ "$*" = "" ] && help
|
2016-04-15 15:14:47 +02:00
|
|
|
|
2016-04-18 19:46:39 +02:00
|
|
|
input="$1"
|
2016-04-15 15:14:47 +02:00
|
|
|
|
2016-04-18 19:46:39 +02:00
|
|
|
[ "$2" != "script" ] && echo "Connecting to captionbot.ai..."
|
2016-08-01 19:54:10 +02:00
|
|
|
conversationId=$(curl -s https://www.captionbot.ai/api/init | sed 's/"//g')
|
2016-04-15 15:54:18 +02:00
|
|
|
|
|
|
|
|
2016-04-18 19:46:39 +02:00
|
|
|
if [ ! -f "$input" ]; then
|
2016-04-18 22:11:39 +02:00
|
|
|
url="\"$(curl -w "%{url_effective}\n" -L -f -s -I -S "$input" -o /dev/null | sed 's/^HTTP/http/g')\"" 2>/dev/null || { [ "$2" != "script" ] && echo "$input isn't a valid url. Please try again."; exit 1; }
|
2016-04-15 19:56:22 +02:00
|
|
|
else
|
2016-04-18 19:46:39 +02:00
|
|
|
[ "$2" != "script" ] && echo "Uploading image..."
|
2016-04-15 15:54:18 +02:00
|
|
|
url=$(curl -s https://www.captionbot.ai/api/upload -F "image1=@$input")
|
|
|
|
fi
|
2016-04-15 15:14:47 +02:00
|
|
|
|
2016-04-18 22:11:39 +02:00
|
|
|
mediainfo $(echo "$url" | sed 's/^\"//g;s/\"$//g') 2>/dev/null | grep -q Image || { [ "$2" != "script" ] && echo "It looks like $url isn't an image."; exit 1; }
|
|
|
|
|
2016-04-18 19:46:39 +02:00
|
|
|
[ "$2" != "script" ] && echo "Analyzing image..."
|
2016-08-01 19:54:10 +02:00
|
|
|
curl 'https://www.captionbot.ai/api/message' -H 'Host: www.captionbot.ai' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: application/json; charset=utf-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Referer: https://www.captionbot.ai/' -d '{"userMessage":'$url', "conversationId":"'$conversationId'"}'
|
2016-04-15 15:14:47 +02:00
|
|
|
|
2016-08-01 19:54:10 +02:00
|
|
|
result=$(curl -s 'https://www.captionbot.ai/api/message?waterMark=&conversationId='$conversationId -H 'Host: www.captionbot.ai' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'X-Requested-With: XMLHttpRequest' -H 'Referer: https://www.captionbot.ai/' -H 'Connection: keep-alive' | sed 's/\\"/"/g;s/^"//g;s/"$//g' | ./JSON.sh)
|
2016-04-15 15:14:47 +02:00
|
|
|
watermark=$(echo "$result" | sed '/\["WaterMark"\]/!d;s/\["WaterMark"\]\t//g')
|
2016-08-01 19:54:10 +02:00
|
|
|
message=$(echo "$result" | sed '/\["BotMessages",1\]/!d;s/\["BotMessages",1\]\t//g;s/^"//g;s/"$//g;s/\\n/ /g;s/\\//g')
|
2016-04-15 15:14:47 +02:00
|
|
|
|
2016-04-19 19:52:30 +02:00
|
|
|
[ "$2" = "script" ] && echo "$message" | grep -q "I really can't describe the picture" && exit 1
|
|
|
|
|
2016-04-15 15:14:47 +02:00
|
|
|
echo $message
|
2016-04-18 19:46:39 +02:00
|
|
|
|
|
|
|
[ "$2" = "script" ] && exit
|
2016-04-15 15:14:47 +02:00
|
|
|
echo
|
2016-04-15 15:54:18 +02:00
|
|
|
|
2016-04-18 19:46:39 +02:00
|
|
|
|
|
|
|
if [ "$2" != "norate" ];then
|
|
|
|
until [ $s -gt 0 -a $s -le 5 ] 2>/dev/null ;do
|
2016-04-15 15:54:18 +02:00
|
|
|
read -p "How did I do (rate 1 to 5)? " s
|
2016-04-18 19:46:39 +02:00
|
|
|
[ $s -gt 0 -a $s -le 5 ] 2>/dev/null || echo "You didn't input a valid number. Please try again!"
|
|
|
|
done
|
2016-08-01 19:54:10 +02:00
|
|
|
result=$(curl -s 'https://www.captionbot.ai/api/message' -H 'Host: www.captionbot.ai' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: application/json; charset=utf-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Referer: https://www.captionbot.ai/' -X POST -d '{"conversationId":"'$conversationId'","waterMark":'$watermark', "userMessage":"'$s'"}')
|
2016-04-15 15:14:47 +02:00
|
|
|
|
2016-08-01 19:54:10 +02:00
|
|
|
[ "$message" = "" ] && echo "Thanks for leaving your feedback!" || echo "$message"
|
2016-04-18 19:46:39 +02:00
|
|
|
fi
|
2016-08-01 19:54:10 +02:00
|
|
|
echo "Thanks for having used captionbot.ai! Do check out my other projects @ daniil.it and my boosted version of telegram's Bot API: pwrtelegram.xyz!"
|
2016-04-15 19:56:22 +02:00
|
|
|
|
|
|
|
exit 0
|