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"
|
2017-06-25 10:03:49 +00:00
|
|
|
"flag"
|
2016-02-04 21:21:04 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-02-16 06:57:14 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2017-06-25 10:03:49 +00:00
|
|
|
var (
|
|
|
|
weatherApiKey = flag.String("weather_api_key", "", "MOD: weather: api key")
|
|
|
|
)
|
|
|
|
|
2016-02-04 21:21:04 +00:00
|
|
|
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() {
|
2016-03-19 22:49:41 +00:00
|
|
|
MsgFuncs["weather"] = weatherHandleMessage
|
|
|
|
RunFuncs["weather"] = weatherConfig
|
2016-02-16 06:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func weatherConfig() {
|
|
|
|
time.Sleep(5 * time.Second)
|
2017-06-25 10:06:41 +00:00
|
|
|
owmQueryAPIKey = *weatherApiKey
|
2016-02-04 21:21:04 +00:00
|
|
|
}
|
|
|
|
|
2017-06-28 21:28:39 +00:00
|
|
|
func weatherHandleMessage(m *Message) {
|
|
|
|
tok := strings.Split(m.Text, " ")
|
2016-02-04 21:21:04 +00:00
|
|
|
if len(tok) < 1 {
|
|
|
|
return
|
|
|
|
}
|
2016-02-26 18:00:00 +00:00
|
|
|
switch strings.ToLower(tok[0]) {
|
2016-02-06 22:30:34 +00:00
|
|
|
case "!weather", "!wetter":
|
2016-02-04 21:21:04 +00:00
|
|
|
switch len(tok) {
|
|
|
|
case 1:
|
2017-06-28 21:28:39 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n%s", m.Channel, weatherPrefix+"Usage: !w <zip/city/state/country>")
|
2016-02-04 21:21:04 +00:00
|
|
|
default:
|
|
|
|
if strings.Compare(owmQueryAPIKey, "") == 0 {
|
2017-06-28 21:28:39 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n%s", m.Channel, weatherPrefix+"No API Key set.")
|
2016-02-04 21:21:04 +00:00
|
|
|
} else {
|
2017-06-28 21:28:39 +00:00
|
|
|
go getWeather(strings.Join(tok[1:], " "), m.Channel)
|
2016-02-04 21:21:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2016-02-15 23:17:42 +00:00
|
|
|
|
2017-06-28 21:28:39 +00:00
|
|
|
func getWeather(query, channel string) {
|
2016-02-04 21:21:04 +00:00
|
|
|
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())
|
2017-06-28 21:28:39 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n%s", channel, 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())
|
2017-06-28 21:28:39 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n%s", channel, 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())
|
2017-06-28 21:28:39 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n%s", channel, 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
|
|
|
|
}
|
2017-06-28 21:28:39 +00:00
|
|
|
SayCh <- fmt.Sprintf("%s\n%s", channel, 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|