Created
January 27, 2015 18:25
-
-
Save 844196/5bbc5d5f83de4561ceac to your computer and use it in GitHub Desktop.
OpenWeatherMap API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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