Skip to content

Instantly share code, notes, and snippets.

@jrdnull
Created July 12, 2013 08:22
Show Gist options
  • Save jrdnull/5982779 to your computer and use it in GitHub Desktop.
Save jrdnull/5982779 to your computer and use it in GitHub Desktop.
Query wunderground api, for my irc bot
// Copyright (c) 2013 Jordon Smith <[email protected]>
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Doc: http://www.wunderground.com/weather/api/d/docs
// ToS: http://www.wunderground.com/weather/api/d/terms.html
package wunderground
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
)
type Response struct {
CurrentObservation Current `xml:"current_observation"`
Error struct {
XMLName xml.Name `xml:"error"`
Description string `xml:"description"`
}
Results []Result `xml:"results"`
}
type Result struct {
Name string `xml:"name"`
}
// partial, check the api if you want more
type Current struct {
Location struct {
XMLName xml.Name `xml:"display_location"`
Full string `xml:"full"`
}
Weather string `xml:"weather"`
Temperature string `xml:"temperature_string"`
Humidity string `xml:"relative_humidity"`
Wind string `xml:"wind_string"`
FeelsLike string `xml:"feelslike_string"`
Visibility string `xml:"visibility_mi"`
}
func GetCurrentWeather(location string) (string, error) {
addr := "http://api.wunderground.com/api/<your-key>/conditions/q/" + location + ".xml"
resp, err := http.Get(addr)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var response *Response = new(Response)
if err = xml.Unmarshal(body, response); err != nil {
return "", err
}
if len(response.Error.Description) != 0 {
return response.Error.Description, nil
}
if len(response.Results) > 0 {
return "Be more specific!", nil
}
cw := response.CurrentObservation
ret := fmt.Sprintf("\x02%s\x02: %s :: Temp: %s (Feels like %s) :: Humidity: %s :: Wind: %s :: Visibility (mi): %s",
cw.Location.Full, cw.Weather, cw.Temperature, cw.FeelsLike, cw.Humidity, cw.Wind, cw.Visibility)
return ret, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment