#!/bin/bash
echo -e "\e]0;up_imagebam.sh\a"
file="$1"
filename="$(basename "$file")"
cookies=$(mktemp /tmp/_imagebam_session.XXXXXXX)
TorPort=${TorPort:-9050}
cpass=$RANDOM$RANDOM:$RANDOM
torcurl() {
curl -x "socks5h://${cpass}@127.0.0.1:${TorPort}" -Ls \
-A "Mozilla/5.0 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0" \
-H "Referer: https://www.imagebam.com/" \
-H "Origin: https://www.imagebam.com" \
-H 'X-Requested-With: XMLHttpRequest' \
--connect-timeout 20 --max-time 35 \
"$@"
}
init=$(torcurl -c "$cookies" -i -X GET \
-H "Content-Type: application/x-www-form-urlencoded" \
"https://www.imagebam.com/")
csrf_token=$(grep -Po '(?<="csrf-token" content=").*(?=">)' <<< "${init}" )
session=$(torcurl -b "$cookies" -c "$cookies" -X POST \
-H 'Accept: application/json, text/javascript, */*; q=0.01' \
-H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
-H "X-CSRF-TOKEN: ${csrf_token}" \
-d 'thumbnail_size=1&content_type=nsfw&comments_enabled=false' \
"https://www.imagebam.com/upload/session")
data=$(jq -r '.data' <<< "$session")
post=$(torcurl -b "$cookies" -X POST \
-H 'Accept: application/json' \
-H 'Content-Type: multipart/form-data' \
-F "files[0]=@${file}" \
-F "data=${data}" \
-F "_token=${csrf_token}" \
"https://www.imagebam.com/upload")
# url to 'upload complete' page
success=$(jq -r '.success' <<< "$post" | sed 's/\\\\//')
# visit success, find url to viewer page
viewer_url=$(torcurl -X GET "$success" |
grep -Po '(?=https://www.imagebam.com/view/).*(?= ">)' -m1)
# visit viewer page (+set cookie value to bypass javascript block)
# find url to /actual/ image
direct_url=$(torcurl -X GET "$viewer_url" -b nsfw_inter="1" |
grep -Po '(?=https://images4.i).*(?=" alt=")')
# "bam"!
printf "\\n"
echo "Direct URL: ${direct_url}"