Guest User

Untitled

a guest
Apr 11th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. # This script was written to display the weather conditions on the command line using the darksky.net api
  3.  
  4. # strict mode
  5. set -Eeuo pipefail
  6. IFS=$'\n\t'
  7.  
  8. # debug mode
  9. #set -o verbose
  10.  
  11. readonly script_name=${0##*/}
  12.  
  13. readonly ip_api_host="ipinfo.io"
  14. readonly ip_api_protocol="http"
  15.  
  16. readonly ip_auth_token_file=${script_name}"_ip.token"
  17. readonly ip_auth_token=`cat ~/bin/${ip_auth_token_file}`
  18.  
  19. readonly ip_api_uri=${ip_api_protocol}"://"${ip_api_host}
  20.  
  21. # Dark Sky api host
  22. readonly ds_api_host="api.darksky.net"
  23. readonly ds_api_protocol="https"
  24.  
  25. # api auth token file
  26. readonly ds_auth_token_file=${script_name}"_ds.token"
  27.  
  28. # Dark Sky api auth token (get your own here --> https://darksky.net/dev/register)
  29. readonly ds_auth_token=`cat ~/bin/${ds_auth_token_file}`
  30.  
  31. readonly ds_api_uri=${ds_api_protocol}"://"${ds_api_host}"/forecast/"${ds_auth_token}
  32.  
  33. readonly tool_name="Weather"
  34. readonly logname="${USER}"
  35.  
  36. # function to catch error messages
  37. # ${1} = error message
  38. # ${2} = exit code
  39. function __throw_error() {
  40.  
  41. # validate arguments
  42. if [ ${#} -eq 2 ]; then
  43. local message=${1}
  44. local exit_code=${2}
  45.  
  46. # log specific error message to syslog and write to STDERR
  47. logger -s -p user.err -t ${script_name}"["${logname}"]" -- ${message}
  48.  
  49. exit ${exit_code}
  50. else
  51. # log generic error message to syslog and write to STDERR
  52. logger -s -p user.err -t ${tool_name}"["${logname}"]" -- "an unknown error occured"
  53. exit 255
  54. fi
  55. }
  56.  
  57. # validate api connectivity
  58. # ${1} = api host
  59. # ${2} = api protocol
  60. function __validate_api_connectivity() {
  61. if [ ${#} -eq 2 ]; then
  62. # test tcp connectivity on specified host and port
  63. ( >/dev/tcp/${1}/${2} ) >/dev/null 2>&1
  64. return ${?}
  65. else
  66. return 1
  67. fi
  68. }
  69.  
  70. function __get_location() {
  71. # query location api for gps coordinates
  72. local coordinates=$( curl -sH "Authorization: Bearer "${ip_auth_token} ${ip_api_uri}/loc )
  73. echo ${coordinates}
  74. }
  75.  
  76. function __get_city() {
  77. # query location api for gps coordinates
  78. local city=$( curl -sH "Authorization: Bearer "${ip_auth_token} ${ip_api_uri}/city )
  79. echo ${city}
  80. }
  81.  
  82. function __get_region {
  83. # query location api for gps coordinates
  84. local region=$( curl -sH "Authorization: Bearer "${ip_auth_token} ${ip_api_uri}/region )
  85. echo ${region}
  86. }
  87.  
  88. function __get_summary() {
  89. # query api for current summary
  90. local current_summary=$( curl -s "${ds_api_uri}/${coordinates}" | jq -r '.currently.summary' )
  91. echo ${current_summary}
  92. }
  93.  
  94. function __get_temperature() {
  95. # query api for current temperature
  96. local current_temperature=$( curl -s "${ds_api_uri}/${coordinates}" | jq -r '.currently.temperature' | awk '{print int($1+0.5)}' )
  97. echo ${current_temperature}
  98. }
  99.  
  100. # ${1} = dependency
  101. function __check_dependency() {
  102. if [ ${#} -eq 1 ]; then
  103. local dependency=${1}
  104. local exit_code=${null:-}
  105. type ${dependency} &>/dev/null; exit_code=${?}
  106.  
  107. if [ ${exit_code} -ne 0 ]; then
  108. return 255
  109. fi
  110. else
  111. return 1
  112. fi
  113. }
  114.  
  115. ####################
  116. ### main program ###
  117. ####################
  118.  
  119. # validate dependencies
  120. readonly -a dependencies=( 'awk' 'jq' 'logger' )
  121. declare -i dependency=0
  122.  
  123. while [ "${dependency}" -lt "${#dependencies[@]}" ]; do
  124. __check_dependency ${dependencies[${dependency}]} || __throw_error ${dependencies[${dependency}]}" required" ${?}
  125. (( ++dependency ))
  126. done
  127.  
  128. unset dependency
  129.  
  130. # make sure we're using least bash 4 for proper support of associative arrays
  131. [ $( echo ${BASH_VERSION} | grep -o '^[0-9]' ) -ge 4 ] || __throw_error "Please upgrade to at least bash version 4" ${?}
  132.  
  133. # make sure we have both api tokens
  134. [ -s ${ds_auth_token} ] && __throw_error "Dark Sky token file not found" ${?}
  135. [ -s ${ip_auth_token} ] && __throw_error "Ipinfo token file not found" ${?}
  136.  
  137. # make sure we can reach both apis
  138. __validate_api_connectivity ${ds_api_host} ${ds_api_protocol} || __throw_error "Unable to establish tcp connection to "${ds_api_host} ${?}
  139. __validate_api_connectivity ${ip_api_host} ${ip_api_protocol} || __throw_error "Unable to establish tcp connection to "${ip_api_host} ${?}
  140.  
  141. # get current geolocation coordinates
  142. coordinates=$( __get_location ) || __throw_error "Unable to get geolocation" ${?}
  143.  
  144. # get current city
  145. city=$( __get_city ) || __throw_error "Unable to get city" ${?}
  146.  
  147. # get current region
  148. region=$( __get_region ) || __throw_error "Unable to get region" ${?}
  149.  
  150. # get the current temperature
  151. current_temperature=$( __get_temperature ) || __throw_error "Unable to get temperature" ${?}
  152.  
  153. # get the current temperature
  154. current_summary=$( __get_summary ) || __throw_error "Unable to get summary" ${?}
  155.  
  156. echo "It is currently "${current_temperature}"° and "${current_summary}" in "${city}", "${region}
Advertisement
Add Comment
Please, Sign In to add comment