openweathermap

package
v1.26.3 Latest Latest
Warning

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

Go to latest
Published: May 12, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &commands.YAGCommand{
	CmdCategory:         commands.CategoryTool,
	Name:                "OpenWeatherMap",
	Aliases:             []string{"owm", "oweather", "ow"},
	Description:         "Shows the weather using OpenWeatherMap API. \nLocation is set by city name and optional state code, country code \n eg. <prefix>owm Paris,AR,US",
	RunInDM:             true,
	RequiredArgs:        1,
	SlashCommandEnabled: true,
	DefaultEnabled:      true,
	Arguments: []*dcmd.ArgDef{
		{Name: "Location", Type: dcmd.String},
	},
	RunFunc: func(data *dcmd.Data) (interface{}, error) {
		var queryData []string
		weather := openWeatherMap{}
		where := data.Args[0].Str()

		queryURL := fmt.Sprintf(openWeatherMapAPIHost + "weather?q=" + where + "&units=" + units + "&appid=" + confOpenWeaterMapAPIKey.GetString())

		req, err := http.NewRequest("GET", queryURL, nil)
		if err != nil {
			return nil, err
		}

		req.Header.Set("User-Agent", "curlPAGST/7.65.1")

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

		if resp.StatusCode == 401 {
			return "Your API key is incorrect.", nil
		} else if resp.StatusCode == 429 {
			return "Your free tariff has made over 60 API calls per minute.", nil
		} else if resp.StatusCode != 200 {
			return "Cannot fetch weather data for the given location: " + where, nil
		}

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

		queryErr := json.Unmarshal(body, &weather)
		if queryErr != nil {
			return nil, queryErr
		}

		windDirectionInt := int((float64(weather.Wind.Deg)+11.25)/22.5) % 16
		windDirection := windDirSlice[windDirectionInt]
		queryData = []string{
			"Weather report: " + weather.Name + ", " + weather.Sys.Country + fmt.Sprintf("%s%.2f %s%.2f", "\nlat:", weather.Coord["lat"], "lon:", weather.Coord["lon"]),
			strings.Title(weather.Weather[0].Description),
			fmt.Sprintf("%.0f%s%d%s", weather.Main.Temp, "°C (", int(float64(weather.Main.Temp)*1.8+32), "°F)"),
			fmt.Sprintf("%s %.0f%s%d%s", "Feels like:", weather.Main.Feels_Like, "°C (", int(float64(weather.Main.Feels_Like)*1.8+32), "°F)"),

			fmt.Sprintf("%s %s %.1f %s %s", "Wind:", windDir[windDirection], weather.Wind.Speed, "m/s", windDirection),
			fmt.Sprintf("%s %d%s %d %s", "Humidity:", weather.Main.Humidity, "% // Pressure:", weather.Main.Pressure, "hPa"),
			fmt.Sprintf("%s %s %s %s", "Sunrise:", time.Unix(weather.Sys.Sunrise, 0).Format(time.UnixDate), "\nSunset: ", time.Unix(weather.Sys.Sunset, 0).Format(time.UnixDate)),
		}

		//Weather condition data referenced > https://openweathermap.org/weather-conditions
		var icon []string
		switch weather.Weather[0].ID {
		case 200, 210, 221, 230, 231:
			icon = iconThunderyShowers
		case 201, 202, 211, 212, 232:
			icon = iconThunderyHeavyRain
		case 300, 301, 310, 311, 321:
			icon = iconLightShowers
		case 302, 312, 313, 314, 531:
			icon = iconHeavyShowers
		case 500, 501, 520, 521:
			icon = iconLightRain
		case 502, 503, 504, 522:
			icon = iconHeavyRain
		case 511, 611, 615, 616:
			icon = iconLightSleet
		case 600:
			icon = iconLightSnow
		case 601, 602:
			icon = iconHeavySnow
		case 612, 613:
			icon = iconLightSleetShowers
		case 620:
			icon = iconLightSnowShowers
		case 621, 622:
			icon = iconHeavySnowShowers
		case 701, 711, 721, 731, 741, 751, 761, 762, 771, 781:
			icon = iconFog
		case 800:
			icon = iconSunny
		case 801, 802:
			icon = iconPartlyCloudy
		case 803, 804:
			icon = iconCloudy
		default:
			icon = iconUnknown
		}

		out := "```\n"
		out += queryData[0] + "\n\n"
		for i := 0; i < 5; i++ {
			if i >= len(icon) {
				break
			}
			out += icon[i] + "\t" + queryData[i+1] + "\n"
		}

		out += "\n" + queryData[6] + "\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