mirror of
https://code.dnix.de/da/flokatirc
synced 2026-07-27 08:59:44 +02:00
Added module configuration params: -params=<key>:<val>(!<key>:<val>])*
This commit is contained in:
+14
-7
@@ -14,6 +14,7 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sorcix/irc"
|
||||
)
|
||||
@@ -69,8 +70,14 @@ type WeatherObject struct {
|
||||
}
|
||||
|
||||
func init() {
|
||||
msgHandlers["weather"] = weatherHandleMessage
|
||||
MsgHandlers["weather"] = weatherHandleMessage
|
||||
log.Println("Initializing weather module")
|
||||
go weatherConfig()
|
||||
}
|
||||
|
||||
func weatherConfig() {
|
||||
time.Sleep(5 * time.Second)
|
||||
owmQueryAPIKey = ModParams["weather-api-key"]
|
||||
}
|
||||
|
||||
func weatherHandleMessage(m *irc.Message) {
|
||||
@@ -82,10 +89,10 @@ func weatherHandleMessage(m *irc.Message) {
|
||||
case "!weather", "!wetter":
|
||||
switch len(tok) {
|
||||
case 1:
|
||||
sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"Usage: !w <zip/city/state/country>")
|
||||
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"Usage: !w <zip/city/state/country>")
|
||||
default:
|
||||
if strings.Compare(owmQueryAPIKey, "") == 0 {
|
||||
sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"No API Key set.")
|
||||
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+"No API Key set.")
|
||||
} else {
|
||||
go getWeather(strings.Join(tok[1:], " "))
|
||||
}
|
||||
@@ -99,19 +106,19 @@ func getWeather(query string) {
|
||||
r, err := http.Get(q)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
|
||||
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
|
||||
} else {
|
||||
re, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
|
||||
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
|
||||
} else {
|
||||
defer r.Body.Close()
|
||||
var wo WeatherObject
|
||||
err := json.Unmarshal(re, &wo)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
|
||||
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
|
||||
} else {
|
||||
var description string
|
||||
if len(wo.Weather) < 1 {
|
||||
@@ -119,7 +126,7 @@ func getWeather(query string) {
|
||||
} else {
|
||||
description = ", " + wo.Weather[0].Description
|
||||
}
|
||||
sayCh <- fmt.Sprintf("*\n%s", weatherPrefix+wo.Name+", "+wo.Sys.Country+
|
||||
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+wo.Name+", "+wo.Sys.Country+
|
||||
" - Temp: "+strconv.Itoa(int(wo.Main.Temp-273.15))+
|
||||
"°C"+description+
|
||||
", Humidity: "+strconv.Itoa(int(wo.Main.Humidity))+
|
||||
|
||||
Reference in New Issue
Block a user