Facebook
From Bulky Tortoise, 6 Years ago, written in Bash.
Embed
Download Paste or View Raw
Hits: 240
  1. #!/bin/bash
  2. Location="Poznan"
  3. Dynamic=false
  4. Fahrenheit=false
  5. Api_key="99aab0a02cc2431b910194023170912"
  6.  
  7. lb="e[94m";
  8. lc="e[96m";
  9. lw="e[97m";
  10. ly="e[33m";
  11. default="e[39m";
  12.  
  13.  
  14. while getopts "dl:f" opt; do
  15.   case $opt in
  16.     d)
  17.       Dynamic=true
  18.       ;;
  19.     l)
  20.       Location="$OPTARG"
  21.       ;;
  22.     f)
  23.       Fahrenheit=true
  24.       ;;
  25.     ?)
  26.       exit 1
  27.       ;;
  28.     :)
  29.       exit 1
  30.       ;;
  31.   esac
  32. done
  33.  
  34. function download_data {
  35.     local File="/tmp/$Location"
  36.     local CurrentTime=$(date +%s)
  37.     local FileCreationTime=0
  38.    
  39.     if [ -e "$File" ] ; then
  40.         FileCreationTime=$(stat -c "%Y" $File)
  41.     fi
  42.  
  43.     if (( ( $CurrentTime - $FileCreationTime ) >= 300 )) ; then
  44.         Data=$(curl -s "http://api.apixu.com/v1/current.json?key=$Api_key&q=$Location")
  45.         if $(echo $Data | jq 'has("error")') ; then
  46.             echo "Nie udało się znaleźć takiego miasta. Możesz je wybudować!:)"
  47.             exit 1
  48.         fi
  49.         echo "$Data" > $File
  50.     else
  51.         Data=$(cat $File)
  52.     fi
  53. }
  54.  
  55. function print_data {
  56.     echo -e "$lbu27F0 $lw Miasto $lbu27F0 $lc        $(echo $Data | jq -r '.location.name')"
  57.  
  58.     if [ "$Fahrenheit" == true ] ; then
  59.         echo -e "$lbu2600 $lw Temperatura $lbu2600  $lc  $(echo $Data | jq -r '.current.temp_f') F"
  60.     else
  61.         echo -e "$lbu2600 $lw Temperatura $lbu2600  $lc  $(echo $Data | jq -r '.current.temp_c') C"
  62.     fi
  63.  
  64.     echo -e "$lbu2B83 $lw Ciśnienie $lbu2B83  $lc    $(echo $Data | jq -r '.current.pressure_mb')"
  65.     echo -e "$lbu0f04 $lw Wiatr $lbu0f04     $lc     $(echo $Data | jq -r '.current.wind_kph')"
  66.     echo -e "$lbu2614 $lw Wilgotność $lbu2614  $lc   $(echo $Data | jq -r '.current.humidity')"
  67.     echo -e "$lbu2601 $lw Zachmurzenie $lbu2601  $lc $(echo $Data | jq -r '.current.cloud')"
  68. }
  69.  
  70. if [ "$Dynamic" == true ] ; then
  71.     while [ true ]; do
  72.         echo -e "$ly==================================================="
  73.         download_data
  74.         print_data
  75.         echo -e "$ly==================================================="
  76.         sleep 300;
  77.     done
  78. else
  79.     download_data
  80.     print_data
  81.     exit 0
  82. fi