1
0

screenshot-google.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/bin/bash
  2. set -euo pipefail
  3. # Klimbim Software collection, A bag full of things
  4. # Copyright (C) 2011-2024 Johannes 'Banana' Keßler
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. # 2024 http://www.bananas-playground.net
  19. # A simple wrapper around google pagespeed request which extracts
  20. # a page screenshot
  21. # Important note: It will not always work. This is not an error. It depends
  22. # on the way the target webpage is build.
  23. command -v curl >/dev/null 2>&1 || { echo >&2 "I require curl (https://curl.haxx.se/) but it's not installed. Aborting."; exit 1; }
  24. command -v jq >/dev/null 2>&1 || { echo >&2 "I require jq (https://jqlang.github.io/jq/) but it's not installed. Aborting."; exit 1; }
  25. command -v base64 >/dev/null 2>&1 || { echo >&2 "I require base64 (https://www.gnu.org/software/coreutils/) but it's not installed. Aborting."; exit 1; }
  26. USERAGENT="Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0"
  27. PAGESPEEDURL="https://www.googleapis.com/pagespeedonline/v5/runPagespeed?&screenshot=true&url="
  28. USAGE="screenshot-google.sh 'URL' file.webp"
  29. if [ $# -lt 2 ]; then
  30. echo "Missing required URL or file";
  31. echo ${USAGE}
  32. exit 2;
  33. fi;
  34. curl -s -H "Cache-Control: no-cache" -H "User-Agent: ${USERAGENT}" "${PAGESPEEDURL}${1}" \
  35. | jq -r '.lighthouseResult.fullPageScreenshot.screenshot.data' | cut -d "," -f 2 \
  36. | base64 --decode > ${2}