weather

package
v0.0.0-...-fb67095 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TempRangeRegex1 = regexp.MustCompile("(-?[0-9]{1,3})( ?- ?(-?[0-9]{1,3}))? ?°C")
	TempRangeRegex2 = regexp.MustCompile(`(-?\+?[0-9]{1,3})( ?\((-?[0-9]{1,3})\))? ?°C`)
	NumberRegex     = regexp.MustCompile(`-?\d{1,3}`)
)
View Source
var Command = &commands.YAGCommand{
	CmdCategory:  commands.CategoryFun,
	Name:         "Weather",
	Aliases:      []string{"w"},
	Description:  "Shows the weather somewhere",
	RunInDM:      true,
	RequiredArgs: 1,
	Arguments: []*dcmd.ArgDef{
		{Name: "Where", Type: dcmd.String},
	},
	DefaultEnabled:      true,
	SlashCommandEnabled: true,
	RunFunc: func(data *dcmd.Data) (interface{}, error) {
		where := data.Args[0].Str()

		req, err := http.NewRequest("GET", "http://wttr.in/"+where+"?m", nil)
		if err != nil {
			return nil, err
		}

		req.Header.Set("User-Agent", "curl/7.49.1")

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			return nil, err
		}

		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return nil, err
		}

		unescaped := vtclean.Clean(string(body), false)

		split := strings.Split(string(unescaped), "\n")

		for i, v := range split {
			if !strings.Contains(v, "°C") {
				continue
			}

			if newS, converted := convTempFormat(TempRangeRegex1, v); converted {
				split[i] = newS
			} else if newS, converted := convTempFormat(TempRangeRegex2, v); converted {
				split[i] = newS
			}
		}

		out := "```\n"
		for i := 0; i < 7; i++ {
			if i >= len(split) {
				break
			}
			out += strings.TrimRight(split[i], " ") + "\n"
		}
		out += "\n```"

		return out, nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL