-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module system deep dive: minor improvements
Add more error checking to the `map.sh` and `geocode.sh` script The default zoom of 2 results in the `center` option not showing anything different. So, we change the default zoom to 10. We also clear up the fact that using an option type of `nullOr <blah>` does not set a default value of `null` for you.
- Loading branch information
1 parent
7a26361
commit b14ab7b
Showing
2 changed files
with
48 additions
and
64 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,50 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
cachedir=~/.cache/google-api/geocode | ||
mkdir -p "$cachedir" | ||
hash=$(echo "$1" | sha256sum - | cut -d' ' -f1) | ||
cachefile="$cachedir/$hash" | ||
rational_regex='-?[[:digit:]]+(\.[[:digit:]]+)?' | ||
result_regex="$rational_regex,$rational_regex" | ||
|
||
if [[ ! -f "$cachefile" ]]; then | ||
keyFile=${XDG_DATA_HOME:-~/.local/share}/google-api/key | ||
|
||
keyFile=${XDG_DATA_HOME:-~/.local/share}/google-api/key | ||
|
||
if [[ ! -f "$keyFile" ]]; then | ||
if [[ ! -f "$keyFile" ]]; then | ||
mkdir -p "$(basename "$keyFile")" | ||
echo "No Google API key found in $keyFile" >&2 | ||
echo "For getting one, see https://developers.google.com/maps/documentation/geocoding/overview#before-you-begin" >&2 | ||
exit 1 | ||
fi | ||
|
||
key=$(cat "$keyFile") | ||
fi | ||
|
||
key=$(cat "$keyFile") | ||
|
||
tmp=$(mktemp -d) | ||
trap 'rm -rf "$tmp"' exit | ||
tmp=$(mktemp -d) | ||
trap 'rm -rf "$tmp"' exit | ||
|
||
output=$tmp/output | ||
output=$tmp/output | ||
|
||
curlArgs=( | ||
curlArgs=( | ||
https://maps.googleapis.com/maps/api/geocode/json | ||
--silent --show-error --get --output "$output" --write-out '%{http_code}' | ||
--data-urlencode address="$1" | ||
) | ||
) | ||
|
||
#echo curl ''${curlArgs[@]@Q} >&2 | ||
#echo curl ''${curlArgs[@]@Q} >&2 | ||
|
||
curlArgs+=(--data-urlencode key="$key") | ||
curlArgs+=(--data-urlencode key="$key") | ||
|
||
if status=$(curl "${curlArgs[@]}"); then | ||
if status=$(curl "${curlArgs[@]}"); then | ||
if [[ "$status" == 200 ]]; then | ||
jq -r '.results[0].geometry.location as $loc | "\($loc | .lat),\($loc | .lng)"' "$output" > "$cachefile" | ||
result=$(jq -r '.results[0].geometry.location as $loc | "\($loc | .lat),\($loc | .lng)"' "$output") | ||
if ! [[ $result =~ $result_regex ]]; then | ||
echo "Got a bad result of: '$result'" >&2 | ||
exit 1 | ||
else | ||
echo "$result" | ||
fi | ||
else | ||
echo "API returned non-200 HTTP status code $status, output is" >&2 | ||
cat "$output" >&2 | ||
exit 1 | ||
echo "API returned non-200 HTTP status code $status, output is" >&2 | ||
cat "$output" >&2 | ||
exit 1 | ||
fi | ||
else | ||
code=$? | ||
echo "curl exited with code $code" >&2 | ||
else | ||
echo "curl exited with code $?" >&2 | ||
exit 1 | ||
fi | ||
fi | ||
|
||
cat "$cachefile" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,47 @@ | ||
#!/usr/bin/env bash | ||
set -euo pipefail | ||
|
||
cachedir=${XDG_CACHE_HOME:-~/.cache}/google-api/maps-static | ||
mkdir -p "$cachedir" | ||
hash=$(echo "$@" | sha256sum - | cut -d' ' -f1) | ||
cachefile="$cachedir/$hash" | ||
keyFile=${XDG_DATA_HOME:-~/.local/share}/google-api/key | ||
|
||
if [[ ! -f "$cachefile" ]]; then | ||
|
||
keyFile=${XDG_DATA_HOME:-~/.local/share}/google-api/key | ||
|
||
if [[ ! -f "$keyFile" ]]; then | ||
if [[ ! -f "$keyFile" ]]; then | ||
mkdir -p "$(basename "$keyFile")" | ||
echo "No Google API key found in $keyFile" >&2 | ||
echo "For getting one, see https://developers.google.com/maps/documentation/maps-static/start#before-you-begin" >&2 | ||
exit 1 | ||
fi | ||
|
||
key=$(cat "$keyFile") | ||
|
||
fi | ||
|
||
tmp=$(mktemp -d) | ||
trap 'rm -rf "$tmp"' exit | ||
key=$(cat "$keyFile") | ||
tmp=$(mktemp -d) | ||
trap 'rm -rf "$tmp"' exit | ||
|
||
output=$tmp/output | ||
output=$tmp/output | ||
|
||
curlArgs=( | ||
curlArgs=( | ||
https://maps.googleapis.com/maps/api/staticmap | ||
--silent --show-error --get --output "$output" --write-out %{http_code} | ||
) | ||
) | ||
|
||
for arg in "$@"; do | ||
for arg in "$@"; do | ||
curlArgs+=(--data-urlencode "$arg") | ||
done | ||
done | ||
|
||
#echo curl ''${curlArgs[@]@Q} >&2 | ||
echo curl ''${curlArgs[@]@Q} >&2 | ||
|
||
curlArgs+=(--data-urlencode key="$key") | ||
curlArgs+=(--data-urlencode key="$key") | ||
|
||
if status=$(curl "${curlArgs[@]}"); then | ||
if [[ "$status" == 200 ]]; then | ||
mv "$output" "$cachefile" | ||
else | ||
echo "API returned non-200 HTTP status code $status, output is" >&2 | ||
cat "$output" >&2 | ||
exit 1 | ||
if status=$(curl "${curlArgs[@]}"); then | ||
if [[ "$status" != 200 ]]; then | ||
echo "API returned non-200 HTTP status code $status, output is" >&2 | ||
cat "$output" >&2 | ||
exit 1 | ||
fi | ||
else | ||
code=$? | ||
echo "curl exited with code $code" >&2 | ||
else | ||
echo "curl exited with code $?" >&2 | ||
exit 1 | ||
fi | ||
fi | ||
|
||
if [[ -t 1 ]]; then | ||
echo "Successful, but won't output image to tty, pipe to a file or icat instead" >&2 | ||
echo "Successful, but won't output image to tty, pipe to a file or icat instead" >&2 | ||
else | ||
cat "$cachefile" | ||
cat "$output" | ||
fi |