Advertisement
v1ral_ITS

Terminal Weather Bash Shell Command Script

May 29th, 2018
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.80 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. currentVersion="1.22.0"
  4. LANG="${LANG:-en}"
  5. locale=$(echo $LANG | cut -c1-2)
  6. unset configuredClient
  7. if [[ $(echo $locale | grep -Eo "[a-z A-Z]*" | wc -c) != 3 ]]; then locale="en"; fi
  8.  
  9. ## This function determines which http get tool the system has installed and returns an error if there isnt one
  10. getConfiguredClient()
  11. {
  12.   if command -v curl &>/dev/null; then
  13.     configuredClient="curl"
  14.   elif command -v wget &>/dev/null; then
  15.     configuredClient="wget"
  16.   elif command -v http &>/dev/null; then
  17.     configuredClient="httpie"
  18.   elif command -v fetch &>/dev/null; then
  19.     configuredClient="fetch"
  20.   else
  21.     echo "Error: This tool reqires either curl, wget, httpie or fetch to be installed\." >&2
  22.     return 1
  23.   fi
  24. }
  25.  
  26. ## Allows to call the users configured client without if statements everywhere
  27. httpGet()
  28. {
  29.   case "$configuredClient" in
  30.     curl)  curl -A curl -s "$@" ;;
  31.     wget)  wget -qO- "$@" ;;
  32.     httpie) http -b GET "$@" ;;
  33.     fetch) fetch -q "$@" ;;
  34.   esac
  35. }
  36.  
  37. getIPWeather()
  38. {
  39.   country=$(httpGet ipinfo.io/country) > /dev/null ## grab the country
  40.   if [[ $country == "US" ]]; then ## if were in the us id rather not use longitude and latitude so the output is nicer
  41.     city=$(httpGet ipinfo.io/city) > /dev/null
  42.     region=$(httpGet ipinfo.io/region) > /dev/null
  43.     if [[ $(echo $region | wc -w) == 2 ]];then
  44.       region=$(echo $region | grep -Eo "[A-Z]*" | tr -d "[:space:]")
  45.     fi
  46.     httpGet $locale.wttr.in/$city,$region$1
  47.   else ## otherwise we are going to use longitude and latitude
  48.     location=$(httpGet ipinfo.io/loc) > /dev/null
  49.     httpGet $locale.wttr.in/$location$1
  50.   fi
  51. }
  52.  
  53. getLocationWeather()
  54. {
  55.   args=$(echo "$@" | tr " " + )
  56.   httpGet $locale.wttr.in/${args}
  57. }
  58.  
  59. checkInternet()
  60. {
  61.   httpGet github.com > /dev/null 2>&1 || { echo "Error: no active internet connection" >&2; return 1; } # query github with a get request
  62. }
  63.  
  64. update()
  65. {
  66.  
  67.   # Update utility version 2.2.0
  68.   # To test the tool enter in the defualt values that are in the examples for each variable
  69.   repositoryName="Bash-Snippets" #Name of repostiory to be updated ex. Sandman-Lite
  70.   githubUserName="alexanderepstein" #username that hosts the repostiory ex. alexanderepstein
  71.   nameOfInstallFile="install.sh" # change this if the installer file has a different name be sure to include file extension if there is one
  72.   latestVersion=$(httpGet https://api.github.com/repos/$githubUserName/$repositoryName/tags | grep -Eo '"name":.*?[^\\]",'| head -1 | grep -Eo "[0-9.]+" ) #always grabs the tag without the v option
  73.  
  74.   if [[ $currentVersion == "" || $repositoryName == "" || $githubUserName == "" || $nameOfInstallFile == "" ]]; then
  75.     echo "Error: update utility has not been configured correctly." >&2
  76.     exit 1
  77.   elif [[ $latestVersion == "" ]]; then
  78.     echo "Error: no active internet connection" >&2
  79.     exit 1
  80.   else
  81.     if [[ "$latestVersion" != "$currentVersion" ]]; then
  82.       echo "Version $latestVersion available"
  83.       echo -n "Do you wish to update $repositoryName [Y/n]: "
  84.       read -r answer
  85.       if [[ "$answer" == [Yy] ]]; then
  86.         cd ~ || { echo 'Update Failed'; exit 1; }
  87.         if [[ -d  ~/$repositoryName ]]; then rm -r -f $repositoryName || { echo "Permissions Error: try running the update as sudo"; exit 1; } ; fi
  88.         echo -n "Downloading latest version of: $repositoryName."
  89.         git clone -q "https://github.com/$githubUserName/$repositoryName" && touch .BSnippetsHiddenFile || { echo "Failure!"; exit 1; } &
  90.         while [ ! -f .BSnippetsHiddenFile ]; do { echo -n "."; sleep 2; };done
  91.         rm -f .BSnippetsHiddenFile
  92.         echo "Success!"
  93.         cd $repositoryName || { echo 'Update Failed'; exit 1; }
  94.         git checkout "v$latestVersion" 2> /dev/null || git checkout "$latestVersion" 2> /dev/null || echo "Couldn't git checkout to stable release, updating to latest commit."
  95.         chmod a+x install.sh #this might be necessary in your case but wasnt in mine.
  96.         ./$nameOfInstallFile "update" || exit 1
  97.         cd ..
  98.         rm -r -f $repositoryName || { echo "Permissions Error: update succesfull but cannot delete temp files located at ~/$repositoryName delete this directory with sudo"; exit 1; }
  99.       else
  100.         exit 1
  101.       fi
  102.     else
  103.       echo "$repositoryName is already the latest version"
  104.     fi
  105.   fi
  106. }
  107.  
  108. usage()
  109. {
  110.   cat <<EOF
  111. Weather
  112. Description: Provides a 3 day forecast on your current location or a specified location.
  113.   With no flags Weather will default to your current location.
  114. Usage: weather or weather [flag] or weather [country] or weather [city] [state]
  115.   weather [i][M] get weather in imperial units, optional M means windspeed in m/s
  116.   weather [m][M] get weather in metric units, optional M means windspeed in m/s
  117.   weather [Moon] grabs the phase of the moon
  118.   -u  Update Bash-Snippet Tools
  119.   -h  Show the help
  120.   -v  Get the tool version
  121. Examples:
  122.   weather
  123.   weather Paris m
  124.   weather Tokyo
  125.   weather Moon
  126.   weather mM
  127. EOF
  128. }
  129.  
  130. getConfiguredClient || exit 1
  131.  
  132. while getopts "uvh" opt; do
  133.   case "$opt" in
  134.     \?) echo "Invalid option: -$OPTARG" >&2
  135.         exit 1
  136.         ;;
  137.     h)  usage
  138.         exit 0
  139.         ;;
  140.     v)  echo "Version $currentVersion"
  141.         exit 0
  142.         ;;
  143.     u)  checkInternet || exit 1 # check if we have a valid internet connection if this isnt true the rest of the script will not work so stop here
  144.         update || exit 1
  145.         exit 0
  146.         ;;
  147.     :)  echo "Option -$OPTARG requires an argument." >&2
  148.         exit 1
  149.         ;;
  150.   esac
  151. done
  152.  
  153. if [[ $# == "0" ]]; then
  154.   checkInternet || exit 1
  155.   getIPWeather || exit 1
  156.   exit 0
  157. elif [[ $1 == "help" || $1 == ":help" ]]; then
  158.   usage
  159.   exit 0
  160. elif [[ $1 == "update" ]]; then
  161.   checkInternet || exit 1
  162.   update || exit 1
  163.   exit 0
  164. fi
  165.  
  166. checkInternet || exit 1
  167. if [[ $1 == "m" ]]; then
  168.   getIPWeather "?m" || exit 1
  169. elif [[ "${@: -1}" == "m" ]];then
  170.   args=$( echo "${@:1:(($# - 1))}" ?m | sed s/" "//g)
  171.   getLocationWeather $args || exit 1
  172. elif [[ $1 == "M" ]]; then
  173.   getIPWeather "?M" || exit 1
  174. elif [[ "${@: -1}" == "M" ]];then
  175.   args=$( echo "${@:1:(($# - 1))}" ?M | sed s/" "//g)
  176.   getLocationWeather $args || exit 1
  177. elif [[ $1 == "mM" || $1 == "Mm" ]]; then
  178.   getIPWeather "?m?M" || exit 1
  179. elif [[ "${@: -1}" == "mM" || "${@:-1}" == "Mm" ]];then
  180.   args=$( echo "${@:1:(($# - 1))}" ?m?M | sed s/" "//g)
  181.   getLocationWeather $args || exit 1
  182. elif [[ $1 == "iM" || $1 == "Mi" ]]; then
  183.   getIPWeather "?u?M" || exit 1
  184. elif [[ "${@: -1}" == "iM" || "${@:-1}" == "Mi" ]];then
  185.   args=$( echo "${@:1:(($# - 1))}" ?u?M | sed s/" "//g)
  186.   getLocationWeather $args || exit 1
  187. elif [[ $1 == "i" ]]; then
  188.   getIPWeather "?u" || exit 1
  189. elif [[ "${@: -1}" == "i" ]];then
  190.   args=$( echo "${@:1:(($# - 1))}" ?u | sed s/" "//g)
  191.   getLocationWeather $args || exit 1
  192. else
  193.   getLocationWeather "$@" || exit 1
  194. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement