Skip to content

Instantly share code, notes, and snippets.

@infernvs
Last active September 15, 2021 22:16
Show Gist options
  • Save infernvs/380c04877f8aebe181d574ac701ab420 to your computer and use it in GitHub Desktop.
Save infernvs/380c04877f8aebe181d574ac701ab420 to your computer and use it in GitHub Desktop.
Bash script to get the weather from Openweathermap.org
#!/bin/bash
APIID="GET API KEY"
temp() {
python -c 'import json; import sys; print json.load(sys.stdin)["main"]["temp"]'
}
humidity() {
python -c 'import json; import sys; print json.load(sys.stdin)["main"]["humidity"]'
}
wind() {
python -c 'import json; import sys; print json.load(sys.stdin)["wind"]["speed"]'
}
city() {
python -c 'import json; import sys; print json.load(sys.stdin)["name"]'
}
condition() {
python -c 'import json; import sys; print json.load(sys.stdin)["weather"][0]["description"]'
}
TEMP=$(curl -sf "http://api.openweathermap.org/data/2.5/weather?q=$1&units=metric&appid=$APIID" | temp)
HUMIDITY=$(curl -sf "http://api.openweathermap.org/data/2.5/weather?q=$1&units=metric&appid=$APIID" | humidity)
CITY=$(curl -sf "http://api.openweathermap.org/data/2.5/weather?q=$1&units=metric&appid=$APIID" | city)
WIND=$(curl -sf "http://api.openweathermap.org/data/2.5/weather?q=$1&units=metric&appid=$APIID" | wind)
CONDITION=$(curl -sf "http://api.openweathermap.org/data/2.5/weather?q=$1&units=metric&appid=$APIID" | condition)
TEMP=$(echo "$TEMP" | awk '{ printf "%.2f\n", $1}')
WIND=$(echo "$WIND" | awk '{ printf "%.2f\n", $1 * 3.6}')
echo -e "Weather report for \e[1m$CITY\e[0m \033[0;31mTemperature:\033[0m \e[1m$TEMP Cº\e[0m \033[0;31mHumidity:\033[0m \e[1m$HUMIDITY%\e[0m \033[0;31mWind:\033[0m \e[1m$WIND km/h\e[0m \033[0;31mCondition:\033[0m \e[1m$CONDITION\e[0m "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment