flokati/modules/weather.go

140 lines
3.2 KiB
Go
Raw Normal View History

2016-02-04 21:21:04 +00:00
// vi:ts=4:sts=4:sw=4:noet:tw=72
//
// flokatirc weather module
//
// Copyright (c) 2016 Daniel Aberger <da@ixab.de>
2016-02-15 23:17:42 +00:00
package modules
2016-02-04 21:21:04 +00:00
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
2016-02-04 21:21:04 +00:00
2016-03-12 12:09:25 +00:00
"code.dnix.de/an/xlog"
2016-02-04 21:21:04 +00:00
"github.com/sorcix/irc"
)
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"`
}
2016-02-15 23:17:42 +00:00
func init() {
MsgHandlers["weather"] = weatherHandleMessage
go weatherConfig()
2016-03-12 12:09:25 +00:00
xlog.Info("Weather module initialized")
}
func weatherConfig() {
time.Sleep(5 * time.Second)
owmQueryAPIKey = ModParams["weather-api-key"]
2016-02-04 21:21:04 +00:00
}
2016-02-15 23:17:42 +00:00
func weatherHandleMessage(m *irc.Message) {
2016-02-04 21:21:04 +00:00
tok := strings.Split(m.Trailing, " ")
if len(tok) < 1 {
return
}
switch tok[0] {
case "!weather", "!wetter":
2016-02-04 21:21:04 +00:00
switch len(tok) {
case 1:
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"Usage: !w <zip/city/state/country>")
2016-02-04 21:21:04 +00:00
default:
if strings.Compare(owmQueryAPIKey, "") == 0 {
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"No API Key set.")
2016-02-04 21:21:04 +00:00
} else {
go getWeather(strings.Join(tok[1:], " "))
}
}
default:
}
}
2016-02-15 23:17:42 +00:00
2016-02-04 21:21:04 +00:00
func getWeather(query string) {
q := owmQueryURLPrefix + query + owmQueryURLSuffix + owmQueryAPIKey
r, err := http.Get(q)
if err != nil {
2016-03-12 12:09:25 +00:00
xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
2016-02-04 21:21:04 +00:00
} else {
re, err := ioutil.ReadAll(r.Body)
if err != nil {
2016-03-12 12:09:25 +00:00
xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
2016-02-04 21:21:04 +00:00
} else {
defer r.Body.Close()
var wo WeatherObject
err := json.Unmarshal(re, &wo)
if err != nil {
2016-03-12 12:09:25 +00:00
xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
2016-02-04 21:21:04 +00:00
} else {
var description string
if len(wo.Weather) < 1 {
description = ""
} else {
description = ", " + wo.Weather[0].Description
}
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+wo.Name+", "+wo.Sys.Country+
2016-02-04 21:21:04 +00:00
" - 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")
}
}
}
}