Last active
November 14, 2023 02:58
-
-
Save dotysan/a79bc4b0b5080f67b4f12d918b9227ef to your computer and use it in GitHub Desktop.
Google Forms monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.envrc | |
gotcha.*.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# | |
# Run in WSL. Wait for Google Form to become public, then open it in Chrome. | |
# | |
set -e #x | |
shopt -s nullglob | |
FORM_TITLE='' | |
CHROME_PROFILE='Default' | |
# override this as needed | |
#CHROME_PROFILE='Profile 1' | |
main() { | |
chk_env | |
set_chrome_path | |
if [[ ! -x "$CHROME" ]] | |
then | |
echo "ERROR: Couldn't find Chrome.exe." | |
exit 1 | |
fi >&2 | |
while printf -v clock '%(%H:%M)T' | |
do head_form "$GOOGLE_FORM_ID" | |
case "$clock" in | |
09:[345]*) ;; | |
09:2[56789]) ;; | |
09:2*) sleep 1 ;; | |
*) sleep 5 ;; | |
esac | |
done | |
} | |
chk_env() { | |
if ! [[ "$GOOGLE_FORM_ID" ]] | |
then | |
local envfile | |
for envfile in .env* | |
do if [[ -r "$envfile" && ! -d "$envfile" ]] | |
then source $envfile | |
fi done | |
if ! [[ "$GOOGLE_FORM_ID" ]] | |
then | |
echo "ERROR: Missing GOOGLE_FORM_ID environment variable." | |
exit 1 | |
fi >&2 | |
fi | |
if ! hash curl xmllint | |
then | |
echo "ERROR: Please install curl and xmllint." | |
exit 1 | |
fi >&2 | |
} | |
set_chrome_path() { | |
local HKLM_Win="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows" | |
local HKLM_chrome="$HKLM_Win\CurrentVersion\App Paths\chrome.exe" | |
local chrome_path=$(reg.exe query "$HKLM_chrome" /ve) | |
chrome_path="${chrome_path##*REG_SZ}" | |
read -rd$'\r' chrome_path <<<"$chrome_path" | |
local drive_letter="${chrome_path:0:1}" | |
chrome_path="/mnt/${drive_letter,,}${chrome_path:2}" | |
CHROME="${chrome_path//\\//}" | |
} | |
head_form() { | |
local id="$1" | |
local form="https://docs.google.com/forms/d/e/$id/viewform" | |
local head=$(curl --silent --head "$form") | |
local line1="${head%% $'\r\n'*}" | |
local status="${line1#* }" | |
local now | |
printf -v now "%(%Y-%m-%dT%H:%M:%S)T" | |
case "$status" in | |
200) | |
curl --silent --location "$form" >gotcha.$now.html & | |
echo -e "$now GO!\n$head" | |
"$CHROME" --profile-directory="$CHROME_PROFILE" \ | |
--new-window "$form" | |
exit 0 ;; | |
302) | |
local location | |
if [[ $head =~ location:\ ([^$'\r']+) ]] | |
then | |
location="${BASH_REMATCH[1]}" | |
fi | |
if [[ -z "$FORM_TITLE" ]] | |
# only look this up once | |
then FORM_TITLE=$(get_html_title "$location") | |
fi | |
echo "$now $location ($FORM_TITLE)" | |
;; | |
404) | |
echo -e "$now Form Not Found: $form\n$head" | |
exit 1 ;; | |
*) | |
echo -e "$now\n$head" | |
;; | |
esac | |
} | |
get_html_title() { | |
curl --silent "$1" |xmllint --html \ | |
--xpath '/html/head/title/text()' \ | |
- 2>/dev/null | |
} | |
main "$@" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment