Implement time plugin
Anirudh Oppiliappan x@icyphox.sh
Sun, 21 Nov 2021 12:36:27 +0530
4 files changed,
59 insertions(+),
4 deletions(-)
M
go.mod
→
go.mod
@@ -21,6 +21,7 @@ github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v1.12.1 // indirect github.com/klauspost/compress v1.12.3 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/zsefvlol/timezonemapper v1.0.0 // indirect go.opencensus.io v0.22.5 // indirect golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect )
M
go.sum
→
go.sum
@@ -75,6 +75,8 @@ github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/zsefvlol/timezonemapper v1.0.0 h1:HXqkOzf01gXYh2nDQcDSROikFgMaximnhE8BY9SyF6E= +github.com/zsefvlol/timezonemapper v1.0.0/go.mod h1:cVUCOLEmc/VvOMusEhpd2G/UBtadL26ZVz2syODXDoQ= go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0= go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
A
plugins/time/time.go
@@ -0,0 +1,36 @@
+package time + +import ( + "encoding/json" + "fmt" + "net/http" + "path" + "time" + + "github.com/zsefvlol/timezonemapper" +) + +func getTimezone(lonlat [2]float64) string { + tz := timezonemapper.LatLngToTimezoneString(lonlat[1], lonlat[0]) + return tz +} + +func GetTime(lonlat [2]float64, label string) (string, error) { + url := path.Join( + "worldtimeapi.org/api/timezone/", + getTimezone(lonlat), + ) + + m := make(map[string]string) + r, err := http.Get("https://" + url) + if err != nil { + return "", err + } + + json.NewDecoder(r.Body).Decode(&m) + timestamp := m["datetime"] + t, _ := time.Parse(time.RFC3339Nano, timestamp) + pretty := t.Format("2006-02-01 03:04 PM") + out := fmt.Sprintf("\x02%s\x02: %s", label, pretty) + return out, nil +}
M
plugins/weather.go
→
plugins/weather.go
@@ -4,6 +4,7 @@ import (
"strings" "git.icyphox.sh/paprika/plugins/location" + "git.icyphox.sh/paprika/plugins/time" "git.icyphox.sh/paprika/plugins/weather" "github.com/dgraph-io/badger/v3" "gopkg.in/irc.v3"@@ -19,11 +20,14 @@ func (Weather) Triggers() []string {
return []string{ ".w", ".weather", + ".t", + ".time", } } func (Weather) Execute(m *irc.Message) (string, error) { parsed := strings.SplitN(m.Trailing(), " ", 2) + trigger := parsed[0] var loc string if len(parsed) != 2 { var err error@@ -62,9 +66,21 @@ return "Error getting location info. Try again.", nil
} coordinates := li.Features[0].Geometry.Coordinates label := li.Features[0].Properties.Geocoding.Label - info, err := weather.GetWeather(coordinates, label) - if err != nil { - return "Error getting weather data", err + + switch trigger { + case ".t", ".time": + time, err := time.GetTime(coordinates, label) + if err != nil { + return "Error getting time data", err + } + return time, nil + case ".w", ".weather": + + info, err := weather.GetWeather(coordinates, label) + if err != nil { + return "Error getting weather data", err + } + return info, nil } - return info, nil + return "", nil }