Skip to content

Instantly share code, notes, and snippets.

@844196
Created January 27, 2015 18:25
Show Gist options
  • Save 844196/5bbc5d5f83de4561ceac to your computer and use it in GitHub Desktop.
Save 844196/5bbc5d5f83de4561ceac to your computer and use it in GitHub Desktop.
OpenWeatherMap API
#!/bin/bash
city="${1}"
IFS=','
mkdir -p "/tmp/${0##*/}" \
&& tmpdir="/tmp/${0##*/}" \
&& tmpfile="${tmpdir}/weather_${args}"
function getWeather() {
curl -s "http://api.openweathermap.org/data/2.5/weather?q=${1},jp&units=metric" \
| jq -r '. | "\(.dt),\(.name),\(.weather[].description),\(.weather[].icon),\(.main.temp)"' \
>"${tmpfile}"
}
function updateWeather() {
echo "Update..."
getWeather "${1}"
}
[[ "$(date +"%M%S")" =~ [135]00[0-2] || ! -e "${tmpfile}" ]] && updateWeather "${city}"
json="$(cat "${tmpfile}")"
# shellcheck disable=SC2086
set -- ${json}
[[ -z "${1}" ]] && updateWeather "${city}"
case "${4}" in
'01d')
icon='☼ '
;;
'01n')
icon='〇'
;;
'02d')
icon='☁ /☼ '
;;
'02n')
icon='☁ /〇'
;;
'03d'|'03n')
icon='☁ '
;;
'04d'|'04n')
icon='☁☁ '
;;
'09d'|'09n'|'11d'|'11n')
icon='☂ '
;;
'10d')
icon='☂ /☼ '
;;
'10n')
icon='☂ /〇'
;;
'13d'|'13n')
icon='☃ '
;;
'50d'|'50n')
icon='〰'
;;
*)
icon='?'
;;
esac
temp="$(echo "${5}" | awk '{c = int($1); printf("%d\n",c)}')℃ "
echo "${icon} ${3}, ${temp}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment