| Total Lines | 42 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | package yandexmap |
||
| 2 | |||
| 3 | import ( |
||
| 4 | "bytes" |
||
| 5 | "fmt" |
||
| 6 | "github.com/FlameInTheDark/dtbot/api/location" |
||
| 7 | "github.com/FlameInTheDark/dtbot/bot" |
||
| 8 | "io" |
||
| 9 | "net/http" |
||
| 10 | "strings" |
||
| 11 | ) |
||
| 12 | |||
| 13 | func GetMapImage(ctx *bot.Context) (buf *bytes.Buffer, err error) { |
||
|
|
|||
| 14 | var ( |
||
| 15 | city = ctx.GetGuild().WeatherCity |
||
| 16 | ) |
||
| 17 | |||
| 18 | if len(ctx.Args) > 0 { |
||
| 19 | city = strings.Join(ctx.Args, "+") |
||
| 20 | } |
||
| 21 | |||
| 22 | loc, err := location.New(ctx.Conf.General.GeonamesUsername, city) |
||
| 23 | if err != nil { |
||
| 24 | fmt.Printf("Location API: %v", err) |
||
| 25 | return |
||
| 26 | } |
||
| 27 | |||
| 28 | // Get coordinates and get weather data |
||
| 29 | newlat, newlng := loc.GetCoordinates() |
||
| 30 | resp, err := http.Get(fmt.Sprintf("https://api.openweathermap.org/data/2.5/forecast?lat=%v&lon=%v&lang=%v&units=metric&appid=%v", |
||
| 31 | newlat, newlng, ctx.Conf.General.Language, ctx.Conf.Weather.WeatherToken)) |
||
| 32 | if err != nil { |
||
| 33 | fmt.Printf("Map API: %v", err) |
||
| 34 | return |
||
| 35 | } |
||
| 36 | |||
| 37 | buf = new(bytes.Buffer) |
||
| 38 | _, err = io.Copy(buf, resp.Body)//png.Encode(buf, resp.Body) |
||
| 39 | if err != nil { |
||
| 40 | fmt.Printf("Image: %v", err.Error()) |
||
| 41 | } |
||
| 42 | return buf, err |
||
| 43 | } |