Skip to content

Commit b9f9020

Browse files
committed
More resiliance and checks for proper city response
1 parent a51e4dd commit b9f9020

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

forecast.sh

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ colors="False"
2323
########################################################################
2424
# Reading in rc
2525
########################################################################
26+
ConfigFile="$HOME/.config/weather_sh.rc"
2627

27-
if [ -f "$HOME/.config/weather_sh.rc" ];then
28-
readarray -t line < "$HOME/.config/weather_sh.rc"
28+
if [ "$1" == "-r" ];then
29+
shift
30+
ConfigFile="$1"
31+
shift
32+
fi
33+
34+
if [ -f "$ConfigFile" ];then
35+
readarray -t line < "$ConfigFile"
2936
apiKey=${line[0]}
3037
defaultLocation=${line[1]}
3138
degreeCharacter=${line[2]}
@@ -102,7 +109,7 @@ esac
102109
if [ -z "${CachePath}" ];then
103110
dataPath="/tmp/fore-$defaultLocation.json"
104111
else
105-
dataPath="${CachePath}fore-$defaultLocation.json"
112+
dataPath="${CachePath}/fore-$defaultLocation.json"
106113
fi
107114

108115
if [ ! -e $dataPath ];then
@@ -118,6 +125,13 @@ else
118125
data=$(cat $dataPath)
119126
fi
120127

128+
check=$(echo "$data" | grep -c -e '"cod":"40')
129+
check2=$(echo "$data" | grep -c -e '"cod":"30')
130+
sum=$(( $check + $check2 ))
131+
if [ $sum -gt 0 ];then
132+
exit 99
133+
fi
134+
121135
lastUpdateTime=$(($(date +%s) -600))
122136

123137
while true; do
@@ -129,7 +143,10 @@ while true; do
129143
data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?q=$defaultLocation&units=metric&appid=$apiKey")
130144
fi
131145
echo $data > $dataPath
146+
else
147+
echo "Cache age: $(($(date +%s)-$lastfileupdate)) seconds."
132148
fi
149+
133150
if [ $(($(date +%s)-$lastUpdateTime)) -ge 600 ]; then
134151
lastUpdateTime=$(date +%s)
135152

weather.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ UseIcons="True"
1717
colors="False"
1818
CityID="True"
1919

20-
if [ -f "$HOME/.config/weather_sh.rc" ];then
21-
readarray -t line < "$HOME/.config/weather_sh.rc"
20+
ConfigFile="$HOME/.config/weather_sh.rc"
21+
22+
if [ "$1" == "-r" ];then
23+
shift
24+
ConfigFile="$1"
25+
shift
26+
fi
27+
28+
if [ -f "$ConfigFile" ];then
29+
readarray -t line < "$ConfigFile"
2230
apiKey=${line[0]}
2331
defaultLocation=${line[1]}
2432
degreeCharacter=${line[2]}
@@ -75,7 +83,7 @@ done
7583
if [ -z "${CachePath}" ];then
7684
dataPath="/tmp/wth-$defaultLocation.json"
7785
else
78-
dataPath="${CachePath}wth-$defaultLocation.json"
86+
dataPath="${CachePath}/wth-$defaultLocation.json"
7987
fi
8088

8189
if [ -z $apiKey ];then
@@ -113,6 +121,14 @@ while true; do
113121
data=$(curl "http://api.openweathermap.org/data/2.5/weather?q=$defaultLocation&units=metric&appid=$apiKey" -s )
114122
fi
115123
echo $data > $dataPath
124+
else
125+
echo "Cache age: $(($(date +%s)-$lastfileupdate)) seconds."
126+
fi
127+
check=$(echo "$data" | grep -c -e '"cod":"40')
128+
check2=$(echo "$data" | grep -c -e '"cod":"30')
129+
sum=$(( $check + $check2 ))
130+
if [ $sum -gt 0 ];then
131+
exit 99
116132
fi
117133
if [ $(($(date +%s)-$lastUpdateTime)) -ge 600 ]; then
118134
lastUpdateTime=$(date +%s)

0 commit comments

Comments
 (0)