// vi:ts=4:sts=4:sw=4:noet:tw=72 // // flokatirc weather module // // Copyright (c) 2016 Daniel Aberger package modules import ( "encoding/json" "flag" "fmt" "io/ioutil" "net/http" "strconv" "strings" "time" "git.dnix.de/an/xlog" ) var ( weatherApiKey = flag.String("weather_api_key", "", "MOD: weather: api key") ) var ( owmQueryAPIKey = "" owmQueryURLPrefix = "http://api.openweathermap.org/data/2.5/weather?q=" owmQueryURLSuffix = "&appid=" weatherPrefix = "[WTHR] " ) type WeatherObject struct { Base string `json:"base"` Clouds struct { All int `json:"all"` } `json:"clouds"` Cod int `json:"cod"` Coord struct { Lat float64 `json:"lat"` Lon float64 `json:"lon"` } `json:"coord"` Dt int `json:"dt"` ID int `json:"id"` Main struct { GrndLevel float64 `json:"grnd_level"` Humidity int `json:"humidity"` Pressure float64 `json:"pressure"` SeaLevel float64 `json:"sea_level"` Temp float64 `json:"temp"` TempMax float64 `json:"temp_max"` TempMin float64 `json:"temp_min"` } `json:"main"` Name string `json:"name"` Rain struct { ThreeH float64 `json:"3h"` } `json:"rain"` Sys struct { Country string `json:"country"` Message float64 `json:"message"` Sunrise int `json:"sunrise"` Sunset int `json:"sunset"` } `json:"sys"` Weather []struct { Description string `json:"description"` Icon string `json:"icon"` ID int `json:"id"` Main string `json:"main"` } `json:"weather"` Wind struct { Deg float64 `json:"deg"` Speed float64 `json:"speed"` } `json:"wind"` } func init() { MsgFuncs["weather"] = weatherHandleMessage RunFuncs["weather"] = weatherConfig } func weatherConfig() { time.Sleep(5 * time.Second) owmQueryAPIKey = *weatherApiKey } func weatherHandleMessage(m *Message) { tok := strings.Split(m.Text, " ") if len(tok) < 1 { return } switch strings.ToLower(tok[0]) { case "!weather", "!wetter": switch len(tok) { case 1: SayCh <- fmt.Sprintf("%s\n%s", m.Channel, weatherPrefix+"Usage: !w ") default: if strings.Compare(owmQueryAPIKey, "") == 0 { SayCh <- fmt.Sprintf("%s\n%s", m.Channel, weatherPrefix+"No API Key set.") } else { go getWeather(strings.Join(tok[1:], " "), m.Channel) } } default: } } func getWeather(query, channel string) { q := owmQueryURLPrefix + query + owmQueryURLSuffix + owmQueryAPIKey r, err := http.Get(q) if err != nil { xlog.Error(err.Error()) SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+err.Error()) } else { re, err := ioutil.ReadAll(r.Body) if err != nil { xlog.Error(err.Error()) SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+err.Error()) } else { defer r.Body.Close() var wo WeatherObject err := json.Unmarshal(re, &wo) if err != nil { xlog.Error(err.Error()) SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+err.Error()) } else { var description string if len(wo.Weather) < 1 { description = "" } else { description = ", " + wo.Weather[0].Description } SayCh <- fmt.Sprintf("%s\n%s", channel, weatherPrefix+wo.Name+", "+wo.Sys.Country+ " - Temp: "+strconv.Itoa(int(wo.Main.Temp-273.15))+ "°C"+description+ ", Humidity: "+strconv.Itoa(int(wo.Main.Humidity))+ "%, Wind: "+strconv.Itoa(int(wo.Wind.Speed*3.6))+ " km/h") } } } }