Extracting coords script, fall back to known

This commit is contained in:
Dan Buch 2020-03-26 09:29:31 -04:00
parent 295b37742e
commit caea8cab11
Signed by: meatballhat
GPG Key ID: 9685130D8B763EA7
2 changed files with 42 additions and 26 deletions

37
get-coords Executable file
View File

@ -0,0 +1,37 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
main() {
if [[ "${DEBUG}" ]]; then
set -o xtrace
fi
_get_lat_lon
}
_get_lat_lon() {
: "${IP_LOOKUP_URL:=https://ifconfig.co/ip}"
: "${GEOIP_LOOKUP_HOST:=http://api.geoiplookup.net}"
local ipaddr
ipaddr="$(curl -fsSL "${IP_LOOKUP_URL}")"
local geoip_xml
geoip_xml="$(curl -fsSL "${GEOIP_LOOKUP_HOST}?query=${ipaddr}")"
_extract_lat_long "${geoip_xml}"
}
_extract_lat_long() {
python <<EOPYTHON
from __future__ import print_function
import sys
from xml.dom.minidom import parseString as parse_xml_string
dom = parse_xml_string("""${1}""")
lat = dom.getElementsByTagName('latitude')[0].firstChild.data
lon = dom.getElementsByTagName('longitude')[0].firstChild.data
print('{}:{}'.format(lat, lon))
EOPYTHON
}
main "${@}"

View File

@ -1,33 +1,12 @@
#!/usr/bin/env bash
set -o errexit
set -o pipefail
main() {
exec "${REDSHIFT_EXE:-redshift}" -l "$(__get_lat_lon)" -v
}
__get_lat_lon() {
: "${IP_LOOKUP_URL:=https://ifconfig.co/ip}"
: "${GEOIP_LOOKUP_HOST:=http://api.geoiplookup.net}"
local ipaddr
ipaddr="$(curl -sSL "${IP_LOOKUP_URL}")"
local geoip_xml
geoip_xml="$(curl -sSL "${GEOIP_LOOKUP_HOST}?query=${ipaddr}")"
__extract_lat_long "${geoip_xml}"
}
__extract_lat_long() {
python <<EOPYTHON
from __future__ import print_function
import sys
from xml.dom.minidom import parseString as parse_xml_string
dom = parse_xml_string("""${1}""")
lat = dom.getElementsByTagName('latitude')[0].firstChild.data
lon = dom.getElementsByTagName('longitude')[0].firstChild.data
print('{}:{}'.format(lat, lon))
EOPYTHON
: "${FALLBACK_COORDS:=40.4325:-79.863}"
local coords
coords="$(~/.get-coords 2>/dev/null || echo "${FALLBACK_COORDS}")"
exec "${REDSHIFT_EXE:-redshift}" -l "${coords}" -v
}
main "${@}"