midaWeb/website-size.sh

72 lines
1.8 KiB
Bash
Raw Permalink Normal View History

2024-01-17 20:40:34 +00:00
#!/bin/bash
2024-01-23 14:05:05 +00:00
resum=0
while getopts 'r:' OPTION; do
case "$OPTION" in
r)
resum=1
;;
?)
echo "utilització: ./website-size.sh [-r (crear un fitxer resum)] example.org" >&2
exit 1
;;
esac
done
# shift "$(($OPTIND -1))"
2024-01-18 13:37:17 +00:00
output="./${!#}.log"
2024-01-18 14:58:21 +00:00
sites_output="./${!#}_sites.log"
mides_output="./${!#}_mides.log"
2024-01-23 14:05:05 +00:00
resum_output="./${!#}_resum.log"
2024-01-18 14:58:21 +00:00
year=$(date +"%Y")
2024-01-17 20:40:34 +00:00
echo "Analitzant ${!#}..."
sleep 1s
echo "Pot tardar una estona..."
2024-01-18 14:58:21 +00:00
echo ""
2024-01-17 20:40:34 +00:00
wget \
--recursive --level=1 \
--spider --server-response \
--no-directories --no-parent \
2024-01-18 13:37:17 +00:00
-e robots=off \
2024-01-17 20:40:34 +00:00
--output-file="$output" "$@"
sleep 1s
if [ -f "$output" ]
then
# Calculate and print estimated website size
echo "Mida estimada de ${!#}: $(\
grep -e "Content-Length" "$output" | \
awk '{sum+=$2} END {printf("%.0f", sum / 1024)}'\
) kb"
2024-01-18 14:58:21 +00:00
echo ""
sleep 1s
2024-01-22 17:52:48 +00:00
grep -e "--$year" $output | awk '{printf("%s\n", $3)}' | sort | uniq >> $sites_output
2024-01-18 14:58:21 +00:00
sed -i '$ d' $sites_output
2024-01-22 17:52:48 +00:00
sed -i '1d' $sites_output
2024-01-23 14:05:05 +00:00
if [ $resum = 1 ]
then
echo "## Resum dels fitxers baixats ##"
sleep 1s
while read line; do
wget --server-response --no-directories --spider -e robots=off --output-file="$mides_output" "$line"
echo "$line $(grep -e "Content-Length" $mides_output | awk '{printf("%.2fKb\n", $2 / 1024)}')"
rm $mides_output
done < $sites_output >> $resum_output
fi
2024-01-22 17:52:48 +00:00
#grep -e "Content-Length" $output | awk '{printf("%.2fKb\n", $2 / 1024)}' >> $mides_output
# echo "## Resum dels fitxers baixats ##"
# sleep 1s
#echo "$(paste $sites_output $mides_output | uniq)"
2024-01-17 20:40:34 +00:00
# Delete wget output file
rm "$output"
2024-01-18 14:58:21 +00:00
rm "$sites_output"
2024-01-17 20:40:34 +00:00
else
echo "Unable to calculate estimated size."
exit 1
fi
exit 0