-
Notifications
You must be signed in to change notification settings - Fork 1
/
darksky.sh
executable file
·74 lines (68 loc) · 1.93 KB
/
darksky.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash
# $ ./darksky.sh <config.json>
# Gets the current conditions from the Forecast.io (DarkSky) API
# Pulls the JSON apart using 'jq' and dumps the temperature and
# humidity values out.
#
# Check dependencies
#
if ! command -v curl >/dev/null 2>&1; then
echo "\"cURL\" is required but not installed; try \"sudo apt-get install curl\" and try again. Abort."
exit 1
fi
if ! command -v jq >/dev/null 2>&1; then
echo "\"jq\" is required but not installed; try \"sudo apt-get install jq\" and try again. Abort."
exit 1
fi
#
# Helper Functions
#
function strip_quotes {
tmp=$1
tmp="${tmp%\"}"
tmp="${tmp#\"}"
echo $tmp
}
#
# Parse the config file
# {
# "latitude": <number>, /* 52.205316 */
# "longitude": <number>, /* 0.121627 */
# "apiKey": <string>, /* 0123456789abcdef9876543210fedcba */
# "language": <string>, /* en */
# "units": <string> /* si */
# }
#
CONFIG_FILE=$1
#echo "Config. File: $CONFIG_FILE"
if [ "$CONFIG_FILE" = "" ]; then
echo "Configuration file not specified. Abort."
exit 2
fi
CONFIG=$(cat $CONFIG_FILE)
#echo "Config.: $CONFIG"
LAT=$(echo $CONFIG | jq '.latitude')
LONG=$(echo $CONFIG | jq '.longitude')
#echo "Lat./Long.: $LAT,$LONG"
API_KEY=$(echo $CONFIG | jq '.apiKey')
API_KEY=$(strip_quotes $API_KEY)
#echo "API Key: $API_KEY"
LANG=$(echo $CONFIG | jq '.language')
LANG=$(strip_quotes $LANG)
#echo "Language: $LANG"
UNITS=$(echo $CONFIG | jq '.units')
UNITS=$(strip_quotes $UNITS)
#echo "Units: $UNITS"
#
# Query the API
#
API_URI=https://api.darksky.net/forecast
RSP=`curl -sS "${API_URI}/${API_KEY}/${LAT},${LONG}?lang=${LANG}&units=${UNITS}&exclude=minutely,hourly,daily,alerts"`
#echo "Response: $RSP"
CURRENTLY=$(echo $RSP | jq '.currently')
TEMPERATURE=$(echo $CURRENTLY | jq '.temperature')
HUMIDITY=$(echo $CURRENTLY | jq '.humidity')
HUMIDITY=$(echo $HUMIDITY*100 | bc)
#echo "Temperature: $TEMPERATURE°C"
#echo "Humidity: $HUMIDITY%RH"
echo "$TEMPERATURE,$HUMIDITY"