From 6804f685c920cd654cd768f8023e504143e0a86b Mon Sep 17 00:00:00 2001 From: zdunecki Date: Fri, 15 Dec 2017 23:45:29 +0100 Subject: [PATCH] feat(weather) add weather app --- .gitignore | 1 + README | 5 ++++ weather.sh | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100644 weather.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/README b/README new file mode 100644 index 0000000..3b47953 --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +# ZSOP - zadania domowe + +* Patryk +* Zdunowski +* 1CG \ No newline at end of file diff --git a/weather.sh b/weather.sh new file mode 100644 index 0000000..2a303be --- /dev/null +++ b/weather.sh @@ -0,0 +1,81 @@ +#!/bin/bash +. ./.env + +LOCATION="Poznan" +IS_CELSIUS=true + + +get_data() { + local tmp_file="/tmp/weather_$LOCATION.json" + local last_changed=0; + + if [ -e "$tmp_file" ] + then + local last_changed=$(stat -c "%Y" $tmp_file) + fi + + local current_time=$(date +%s) + local should_request=$((($current_time - $last_changed) > 300)) + + if (($should_request)) + then + curl -s "http://api.apixu.com/v1/current.json?key=$API_KEY&q=$LOCATION" > $tmp_file; #API_KEY comes from .env + fi + WEATHER=$(cat $tmp_file) + } + + get_weather() { + local presenter=() + local temp="c" + + if [ "$IS_CELSIUS" == false ] + then + temp="f" + fi + clear + get_data + + presenter+=("City: $(echo $WEATHER | jq -r '.location.name')") + presenter+=("Country: $(echo $WEATHER | jq -r '.location.country')") + presenter+=("Temperature: $(echo $WEATHER | jq -r '.current.temp_'$temp) ${temp^}") + presenter+=("Feels like: $(echo $WEATHER | jq -r '.current.feelslike_'$temp) ${temp^}") + presenter+=("Wind: $(echo $WEATHER | jq -r '.current.wind_kph')") + presenter+=("Presure: $(echo $WEATHER | jq -r '.current.pressure_mb')") + presenter+=("Humidity: $(echo $WEATHER | jq -r '.current.humidity')") + presenter+=("Cloudy: $(echo $WEATHER | jq -r '.current.cloud')") + + echo "$(printf '%s\n' "${presenter[@]}")" +} + +while getopts "ldf" opt; do + case $opt in + l) + OPTARG=${!OPTIND} + + if [ "$#" -lt "$OPTIND" ]; then + OPTARG=$LOCATION + else + OPTIND=$(( $OPTIND + 1 )) + fi + + LOCATION="$OPTARG" + ;; + d) + IS_DYNAMIC=true + ;; + f) + IS_CELSIUS=false + ;; + esac +done + +if [ "$IS_DYNAMIC" == true ] + then + while [ true ] + do + get_weather + sleep 300; + done + else + get_weather +fi