Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # This script was written to display the weather conditions on the command line using the darksky.net api
- # strict mode
- set -Eeuo pipefail
- IFS=$'\n\t'
- # debug mode
- #set -o verbose
- readonly script_name=${0##*/}
- readonly ip_api_host="ipinfo.io"
- readonly ip_api_protocol="http"
- readonly ip_auth_token_file=${script_name}"_ip.token"
- readonly ip_auth_token=`cat ~/bin/${ip_auth_token_file}`
- readonly ip_api_uri=${ip_api_protocol}"://"${ip_api_host}
- # Dark Sky api host
- readonly ds_api_host="api.darksky.net"
- readonly ds_api_protocol="https"
- # api auth token file
- readonly ds_auth_token_file=${script_name}"_ds.token"
- # Dark Sky api auth token (get your own here --> https://darksky.net/dev/register)
- readonly ds_auth_token=`cat ~/bin/${ds_auth_token_file}`
- readonly ds_api_uri=${ds_api_protocol}"://"${ds_api_host}"/forecast/"${ds_auth_token}
- readonly tool_name="Weather"
- readonly logname="${USER}"
- # function to catch error messages
- # ${1} = error message
- # ${2} = exit code
- function __throw_error() {
- # validate arguments
- if [ ${#} -eq 2 ]; then
- local message=${1}
- local exit_code=${2}
- # log specific error message to syslog and write to STDERR
- logger -s -p user.err -t ${script_name}"["${logname}"]" -- ${message}
- exit ${exit_code}
- else
- # log generic error message to syslog and write to STDERR
- logger -s -p user.err -t ${tool_name}"["${logname}"]" -- "an unknown error occured"
- exit 255
- fi
- }
- # validate api connectivity
- # ${1} = api host
- # ${2} = api protocol
- function __validate_api_connectivity() {
- if [ ${#} -eq 2 ]; then
- # test tcp connectivity on specified host and port
- ( >/dev/tcp/${1}/${2} ) >/dev/null 2>&1
- return ${?}
- else
- return 1
- fi
- }
- function __get_location() {
- # query location api for gps coordinates
- local coordinates=$( curl -sH "Authorization: Bearer "${ip_auth_token} ${ip_api_uri}/loc )
- echo ${coordinates}
- }
- function __get_city() {
- # query location api for gps coordinates
- local city=$( curl -sH "Authorization: Bearer "${ip_auth_token} ${ip_api_uri}/city )
- echo ${city}
- }
- function __get_region {
- # query location api for gps coordinates
- local region=$( curl -sH "Authorization: Bearer "${ip_auth_token} ${ip_api_uri}/region )
- echo ${region}
- }
- function __get_summary() {
- # query api for current summary
- local current_summary=$( curl -s "${ds_api_uri}/${coordinates}" | jq -r '.currently.summary' )
- echo ${current_summary}
- }
- function __get_temperature() {
- # query api for current temperature
- local current_temperature=$( curl -s "${ds_api_uri}/${coordinates}" | jq -r '.currently.temperature' | awk '{print int($1+0.5)}' )
- echo ${current_temperature}
- }
- # ${1} = dependency
- function __check_dependency() {
- if [ ${#} -eq 1 ]; then
- local dependency=${1}
- local exit_code=${null:-}
- type ${dependency} &>/dev/null; exit_code=${?}
- if [ ${exit_code} -ne 0 ]; then
- return 255
- fi
- else
- return 1
- fi
- }
- ####################
- ### main program ###
- ####################
- # validate dependencies
- readonly -a dependencies=( 'awk' 'jq' 'logger' )
- declare -i dependency=0
- while [ "${dependency}" -lt "${#dependencies[@]}" ]; do
- __check_dependency ${dependencies[${dependency}]} || __throw_error ${dependencies[${dependency}]}" required" ${?}
- (( ++dependency ))
- done
- unset dependency
- # make sure we're using least bash 4 for proper support of associative arrays
- [ $( echo ${BASH_VERSION} | grep -o '^[0-9]' ) -ge 4 ] || __throw_error "Please upgrade to at least bash version 4" ${?}
- # make sure we have both api tokens
- [ -s ${ds_auth_token} ] && __throw_error "Dark Sky token file not found" ${?}
- [ -s ${ip_auth_token} ] && __throw_error "Ipinfo token file not found" ${?}
- # make sure we can reach both apis
- __validate_api_connectivity ${ds_api_host} ${ds_api_protocol} || __throw_error "Unable to establish tcp connection to "${ds_api_host} ${?}
- __validate_api_connectivity ${ip_api_host} ${ip_api_protocol} || __throw_error "Unable to establish tcp connection to "${ip_api_host} ${?}
- # get current geolocation coordinates
- coordinates=$( __get_location ) || __throw_error "Unable to get geolocation" ${?}
- # get current city
- city=$( __get_city ) || __throw_error "Unable to get city" ${?}
- # get current region
- region=$( __get_region ) || __throw_error "Unable to get region" ${?}
- # get the current temperature
- current_temperature=$( __get_temperature ) || __throw_error "Unable to get temperature" ${?}
- # get the current temperature
- current_summary=$( __get_summary ) || __throw_error "Unable to get summary" ${?}
- echo "It is currently "${current_temperature}"° and "${current_summary}" in "${city}", "${region}
Advertisement
Add Comment
Please, Sign In to add comment