Last Updated: August 29, 2016
·
13.74K
· donutdan4114

Getting weather via your command line

This is a slightly better version of another script I wrote: http://coderwall.com/p/gbajfa

This script relies on you having PHP installed and the PATH setup properly.

This script will do the geolocation for you, but you need to have a Wunderground developers API key to use it: http://api.wunderground.com/weather/api/

Put this in your .bashrc file:

current_weather(){

  local API_KEY='put_your_key_here'

  php -r '$json = json_decode(file_get_contents("http://api.wunderground.com/api/'${API_KEY}'/geolookup/q/autoip.json")); $zip = $json->location->zip; $json = json_decode(file_get_contents("http://api.wunderground.com/api/'${API_KEY}'/conditions/forecast/q/$zip.json")); $w = $json->current_observation; $location = $w->display_location->full; $f = $json->forecast->txt_forecast; echo "Weather for $location: " . PHP_EOL; echo "----------------------------" . PHP_EOL; echo "Current temperature: {$w->temp_f}F ({$w->temp_c}C)" . PHP_EOL . PHP_EOL; foreach($f->forecastday as $day){ echo "{$day->title}: {$day->fcttext}". PHP_EOL . PHP_EOL; }'

}

Now, just type this into the command line:

$> current_weather

You will get the current weather conditions as well as future conditions geolocated by your IP address.

4 Responses
Add your response

This is the output I get

bash-3.2$ current_weather
Weather for San Francisco, CA: 
----------------------------
Current temperature: 85.8F (29.9C)

Wednesday: Clear. High of 81F. Winds from the ENE at 5 to 10 mph shifting to the West in the afternoon.

Wednesday Night: Clear. Low of 61F. Winds less than 5 mph.

Thursday: Clear. High of 86F. Winds from the NE at 5 to 10 mph shifting to the WSW in the afternoon.

Thursday Night: Clear. Low of 59F. Winds from the SW at 5 to 10 mph.

Friday: Clear. High of 73F. Winds from the SW at 5 to 10 mph.

Friday Night: Overcast. Low of 61F. Winds from the West at 5 to 10 mph.

Saturday: Partly cloudy in the morning, then clear. High of 66F. Winds from the WNW at 5 to 20 mph.

Saturday Night: Clear. Low of 54F. Winds from the WNW at 10 to 15 mph.
over 1 year ago ·

another way (posted on HN):

telnet rainmaker.wunderground.com

gives something like this:

Press Return for menu
or enter 3 letter forecast city code-- SFO
Weather Conditions at 12:56 PM PDT on 17 Oct 2012 for San Francisco, CA.
Temp(F)    Humidity(%)    Wind(mph)    Pressure(in)    Weather
========================================================================
  75          43%         NORTH at 0       29.88      Clear

Forecast for San Francisco, CA
120 PM PDT Wed Oct 17 2012

.Tonight...Clear. Lows in the upper 50s. Southwest winds 5 to
10 mph in the evening...becoming light. 
.Thursday...Sunny. Highs in the mid 70s. Northeast winds around
5 mph...becoming south in the afternoon. 
.Thursday night...Partly cloudy. Patchy fog after midnight. Lows
in the upper 50s. Southwest winds 5 to 10 mph. 
.Friday...Cloudy in the morning then becoming sunny. Patchy fog
in the morning. Highs in the mid 60s to lower 70s. Southwest
winds 5 to 10 mph. 
.Friday night...Mostly clear in the evening then becoming cloudy.
Patchy fog after midnight. Lows in the mid 50s. Southwest winds
around 10 mph. 
.Saturday...Mostly cloudy. Patchy fog. Highs in the lower 60s. 
.Saturday night...Partly cloudy. Lows in the lower 50s. 
.Sunday and Sunday night...Mostly cloudy. Highs in the lower 60s.
   Press Return to continue, M to return to menu, X to exit:  x
Lows in the lower 50s. 
.Monday...Mostly cloudy. A chance of rain. Highs in the upper
50s. 
.Monday night and Tuesday...Rain likely. Lows in the lower 50s.
Highs around 60. 
.Tuesday night and Wednesday...Mostly cloudy. A chance of rain.
Lows in the lower 50s. Highs around 60. 

&&
                 Temperature        /   precipitation

San Francisco    60  75  59  70     /   0   0   0   0 
Ocean Beach      58  74  58  65     /   0   0   0   0 
over 1 year ago ·

@bashir Very cool, didn't know wunderground had a telnet server setup.

over 1 year ago ·

Another way would be:

weather()
{
    curl http://wttr\.in/$1
}

So you can now use just your city as paramter:
weather Ansbach

wttr.in

over 1 year ago ·