Passed
Push — master ( f479ad...d7840c )
by Viktor
02:05
created

api/yandexmap/yandexmap.go   A

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 29
dl 0
loc 42
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B yandexmap.GetMapImage 0 30 5
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) {
0 ignored issues
show
introduced by
exported function GetMapImage should have comment or be unexported
Loading history...
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
}