Skip to content

Instantly share code, notes, and snippets.

@tastytea
Last active September 11, 2017 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tastytea/577b5066d6a74f4dd4eb57eb06df2b85 to your computer and use it in GitHub Desktop.
Save tastytea/577b5066d6a74f4dd4eb57eb06df2b85 to your computer and use it in GitHub Desktop.
Get temperature and store in file, openweathermap
#!/bin/sh
API_KEY=""
CITY="Hamburg,de"
OUTPUT_FILE="/run/user/$(id -u)/gettemp.temperature"
RESULT=""
RETRY=0
if [ "${1}" != "" ]; then
CITY="${1}"
fi
# Try 2 times to fetch temperature
while [ ${RETRY} -lt 2 ]; do
RESULT=$(curl -s http://api.openweathermap.org/data/2.5/weather\?appid\=${API_KEY}\&q\=${CITY}\&units\=metric \
| sed 's/^.*temp":\(-\?[0-9\.]*\),.*$/\1/')
# HTTP errors are returned as HTML document,
# connection failures as "failed to connect".
# If response is too long to be valid, discard.
# If it seems valid, write to file and exit.
if [ $(echo ${RESULT} | wc -c) -lt 10 ]; then
RESULT=$(echo "scale=1; x = ${RESULT}; if (x > 0) x = x + 0.05; if (x < 0) x = x - 0.05; x / 1" \
| bc | sed -r 's/^(-?)\./\10\./') # Round
echo "${RESULT}" > ${OUTPUT_FILE}
break
fi
RETRY=$(expr ${RETRY} + 1)
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment