Refactor the script so it's more readable. Make image annotation multithreaded

This commit is contained in:
Kuba Orlik 2022-04-21 22:20:44 +02:00
parent 8661cf88c7
commit 9dc42f6f90
3 changed files with 155 additions and 156 deletions

View File

@ -12,9 +12,10 @@ get_width(){
annotate_header(){ annotate_header(){
header=$1 echo annotate $1
filename=$1
shift;
d=$(date "+%Y-%m-%d__%H_%M_%S") d=$(date "+%Y-%m-%d__%H_%M_%S")
filename="${d}__$header.png"
#filename="2022-03-10__19_33_55__set-cookie.png" #filename="2022-03-10__19_33_55__set-cookie.png"
cropped_filename="${filename}__cropped.png" cropped_filename="${filename}__cropped.png"
overlay_filename="${cropped_filename}__overlay.png" overlay_filename="${cropped_filename}__overlay.png"
@ -26,7 +27,6 @@ annotate_header(){
top=330 top=330
width=824 width=824
height=1260 height=1260
scrot $filename
vips extract_area "$filename" "$cropped_filename" $left $top $width $height vips extract_area "$filename" "$cropped_filename" $left $top $width $height
# prepare overlay canvases # prepare overlay canvases
@ -83,6 +83,7 @@ annotate_header(){
convert "$filename" "$overlay_filename" -compose Darken -composite "$annotated_filename.step.png" convert "$filename" "$overlay_filename" -compose Darken -composite "$annotated_filename.step.png"
convert "$annotated_filename.step.png" "$hardoverlay_filename" -compose src-over -composite "$annotated_filename" convert "$annotated_filename.step.png" "$hardoverlay_filename" -compose src-over -composite "$annotated_filename"
rm "$overlay_filename" "$annotated_filename.step.png" "$hardoverlay_filename" "$cropped_filename" "$filename" rm "$overlay_filename" "$annotated_filename.step.png" "$hardoverlay_filename" "$cropped_filename" "$filename"
echo "done annotate $filename";
} }
#annotate_header "set-cookie" "identyfikator internetowy z cookie" "Cookie" "identyfikator internetowy z cookie" #annotate_header "set-cookie" "identyfikator internetowy z cookie" "Cookie" "identyfikator internetowy z cookie"

View File

@ -2,171 +2,45 @@
source ./ephemeral-x.sh source ./ephemeral-x.sh
source ./annotate_header.sh source ./annotate_header.sh
source ./utils.sh
get_pixel_color(){ start_firefox
x=$1; prepare_firefox
y=$2; load_website "biedronka.pl"
output_path="/tmp/$(mktemp -u XXXXXX).png" open_network_inspector
scrot $output_path network_inspector_search "method:GET domain:adocean.pl" # can filter with more granularity: https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor/request_list#filtering_by_properties
magick $output_path -format "%[hex:p{$x,$y}]" info:
}
extract_text(){ index=0
output_path="/tmp/$(mktemp -u XXXXXX).png"
cropped_path=$output_path--cropped.png
scrot $output_path
left=$1
top=$2
width=$3
height=$4
# https://www.libvips.org/API/current/libvips-conversion.html#vips-extract-area
vips extract_area "$output_path" "$cropped_path" $left $top $width $height
tesseract "$cropped_path" stdout -l eng
}
click (){ declare -a pids;
xdotool mousemove $1 $2 pids=()
xdotool click 1
}
grab (){ while network_inspector_has_more_entries
NOTE="$@"
echo $@
scrot --note "-f '/usr/share/fonts/noto/NotoSansMono-Medium.ttf/40' -c 255,0,0,255 -t '$NOTE'"
}
keycombo(){
for key in "$@"
do do
xdotool keydown $key filename="${index}.png"
sleep 0.01 scrot "$filename"
done annotate_header "$filename" \
for key in "$@"
do
xdotool keyup $key
sleep 0.01
done
sleep 0.5
}
rm -rf /root/.mozilla/firefox/bifup8k5.docker/sessionstore-backups
#echo 'user_pref("layout.css.devPixelsPerPx", "1.5");' >> /root/.mozilla/firefox/bifup8k5.docker/prefs.js
firefox --devtools > /dev/null &
# test
FIREFOX_PID=$!
sleep 3
xdotool key Return # in case there's a "firefox closed unexpectedly" button
sleep 1
# click 1424 628
# grab before maximize
click 1886 13 #maximize button
sleep 1
#grab after maximize before control shift E
#keycombo Control_L Shift_L e
#sleep 1
# grab before escape
#xdotool key Escape
sleep 0.1
# grab after escape
keycombo Control_L Shift_L e
#xdotool key Escape
keycombo Shift_L c F5 #profiler
keycombo Control_L Shift_L e
# grab after control shift e before ctrl L
keycombo Control_L l
# grab after control L
xdotool type "biedronka.pl"
xdotool key Return
# grab after return
sleep 1
# scrot
echo "waiting for the website to load..."
times=0
while [ $(get_pixel_color 143 122) = "2E3436" ] # the center of the X icon that becomes a "refresh" icon once the website is finished loading
do
sleep 0.5
printf "."
#scrot
times=$((times + 1))
if [ $times -eq 10 ]
then
echo "Could not load website in time"
break;
fi
done
keycombo Shift_L F5 #profiler
echo shift f5
sleep 1
echo "sleep 1"
keycombo Control_L Shift_L e
echo ctrl shift e
scrot
keycombo Control_L f
sleep 0.2
xdotool type "method:GET domain:adocean.pl" # can filter with more granularity: https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor/request_list#filtering_by_properties
xdotool key Escape
sleep 0.3
scrot
seq 28 | xargs -I {} xdotool key Tab
xdotool key Down
sleep 0.1
xdotool key Up
sleep 0.1
#scrot
while [ $(get_pixel_color 1267 1572) = "F9F9FA" ]
do
annotate_header \
"set-cookie" "identyfikator internetowy z cookie" \ "set-cookie" "identyfikator internetowy z cookie" \
"Cookie" "identyfikator internetowy z cookie" \ "Cookie" "identyfikator internetowy z cookie" \
"Referer" "Część mojej historii przeglądania" "Referer" "Część mojej historii przeglądania" &
xdotool key Tab pids+=($!)
sleep 0.05 network_inspector_next_entry
xdotool key Down ((index++))
sleep 0.2 done
# use python script ^here to annotate the cropped png
# scrot echo "starting wait..."
# echo "########## EXTRACTED TEXT: "
# extract_text 2056 330 824 1260 | grep cookie jobs -l
for PID in "${pids[@]}"
do
wait $PID
done done
echo "Done!"
kill -2 %%; kill -2 %%;
cleanup cleanup
echo "Done!"

124
utils.sh Normal file
View File

@ -0,0 +1,124 @@
#!/bin/bash
get_pixel_color(){
x=$1;
y=$2;
output_path="/tmp/$(mktemp -u XXXXXX).png"
scrot $output_path
magick $output_path -format "%[hex:p{$x,$y}]" info:
}
extract_text(){
output_path="/tmp/$(mktemp -u XXXXXX).png"
cropped_path=$output_path--cropped.png
scrot $output_path
left=$1
top=$2
width=$3
height=$4
# https://www.libvips.org/API/current/libvips-conversion.html#vips-extract-area
vips extract_area "$output_path" "$cropped_path" $left $top $width $height
tesseract "$cropped_path" stdout -l eng
}
click (){
xdotool mousemove $1 $2
xdotool click 1
}
grab (){
NOTE="$@"
echo $@
scrot --note "-f '/usr/share/fonts/noto/NotoSansMono-Medium.ttf/40' -c 255,0,0,255 -t '$NOTE'"
}
keycombo(){
for key in "$@"
do
xdotool keydown $key
sleep 0.01
done
for key in "$@"
do
xdotool keyup $key
sleep 0.01
done
sleep 0.5
}
start_firefox(){
rm -rf /root/.mozilla/firefox/bifup8k5.docker/sessionstore-backups
#echo 'user_pref("layout.css.devPixelsPerPx", "1.5");' >> /root/.mozilla/firefox/bifup8k5.docker/prefs.js
firefox --devtools > /dev/null &
FIREFOX_PID=$!
sleep 3
}
prepare_firefox(){
xdotool key Return # in case there's a "firefox closed unexpectedly" button
sleep 1
click 1886 13 #maximize button
sleep 1
sleep 0.1
keycombo Control_L Shift_L e
keycombo Shift_L c F5 #profiler
keycombo Control_L Shift_L e
}
load_website(){
URL=$1
keycombo Control_L l
xdotool type "$URL"
xdotool key Return
sleep 1
echo "waiting for $URL to load..."
times=0
while [ $(get_pixel_color 143 122) = "2E3436" ] # the center of the X icon that becomes a "refresh" icon once the website is finished loading
do
sleep 0.5
printf "."
#scrot
times=$((times + 1))
if [ $times -eq 10 ]
then
echo "Could not load website in time"
break;
fi
done
}
open_network_inspector(){
keycombo Shift_L F5 #profiler
echo shift f5
sleep 1
echo "sleep 1"
keycombo Control_L Shift_L e
echo ctrl shift e
}
network_inspector_search(){
query=$1
keycombo Control_L f
sleep 0.2
xdotool type "$query"
xdotool key Escape
sleep 0.3
seq 28 | xargs -I {} xdotool key Tab
xdotool key Down
sleep 0.1
xdotool key Up
sleep 0.1
}
network_inspector_has_more_entries(){
[ $(get_pixel_color 1267 1572) = "F9F9FA" ]
}
network_inspector_next_entry(){
xdotool key Tab
sleep 0.05
xdotool key Down
sleep 0.2
}