all repos — paprika @ master

go rewrite of taigabot

plugins/weather.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
package plugins

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"
)

func init() {
	Register(Weather{})
}

type Weather struct{}

func (Weather) Triggers() []string {
	return []string{
		".w",
		".weather",
		".t",
		".time",
	}
}

func (Weather) Execute(m *irc.Message) (string, error) {
	parsed := strings.SplitN(m.Trailing(), " ", 2)

	// TODO: AAAAAAAAAAAAAAAAAAAAAAAAAAA
	found := false
	trigger := parsed[0]
	for _, v := range (Weather{}).Triggers() {
		if trigger == v {
			found = true
			break
		}
	}
	if !found {
		return "", NoReply
	}

	var loc string
	if len(parsed) != 2 {
		var err error
		// Check if they've already set their location
		loc, err = location.GetLocation(m.Prefix.Name)
		if err == badger.ErrKeyNotFound {
			return "Location not set. Use '.loc <location>' to set it.", nil
		} else if err != nil {
			return "Error getting location", err
		}
	}

	// They're either querying for a location or @nick.
	if len(loc) == 0 {
		if strings.HasPrefix(parsed[1], "@") {
			// Strip '@'
			var err error
			loc, err = location.GetLocation(parsed[1][1:])
			if err == badger.ErrKeyNotFound {
				return "Location not set. Use '.loc <location>' to set it.", nil
			} else if err != nil {
				return "Error getting location. Try again.", err
			}
		} else {
			loc = parsed[1]
		}
	}

	li, err := location.GetLocationInfo(loc)
	if err != nil {
		return "Error getting location info. Try again.", err
	}

	if len(li.Features) == 0 {
		return "Error getting location info. Try again.", nil
	}
	coordinates := li.Features[0].Geometry.Coordinates
	label := li.Features[0].Properties.Geocoding.Label

	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 "", nil
}