Issues (115)

api/geoip/geoip.go (7 issues)

Severity
1
package geoip
2
3
import (
4
	"encoding/json"
5
	"fmt"
6
	"github.com/FlameInTheDark/dtbot/bot"
7
	"net/http"
8
)
9
10
// GeoIP main geoip structure. Contains geographic data about ip address
11
type GeoIP struct {
12
	IP        string       `json:"ip"`
13
	City      GeoIPCity    `json:"city"`
14
	Region    GeoIPRegion  `json:"region"`
15
	Country   GeoIPCountry `json:"country"`
16
	Error     string       `json:"error"`
17
	Requests  int          `json:"requests"`
18
	Created   string       `json:"created"`
19
	TimeStamp int          `json:"timestamp"`
20
}
21
22
// GeoIPCity contains data about city
23
type GeoIPCity struct {
0 ignored issues
show
type name will be used as geoip.GeoIPCity by other packages, and that stutters; consider calling this City
Loading history...
24
	Id         int     `json:"id"`
0 ignored issues
show
struct field Id should be ID
Loading history...
25
	Latitude   float64 `json:"lat"`
26
	Longitude  float64 `json:"lon"`
27
	NameRU     string  `json:"name_ru"`
28
	NameEN     string  `json:"name_en"`
29
	OKATO      string  `json:"okato"`
30
	VK         int     `json:"vk"`
31
	Population int     `json:"population"`
32
	Tel        string  `json:"tel"`
33
	PostalCode string  `json:"post"`
34
}
35
36
// GeoIPRegion contains data about region
37
type GeoIPRegion struct {
0 ignored issues
show
type name will be used as geoip.GeoIPRegion by other packages, and that stutters; consider calling this Region
Loading history...
38
	Id        int     `json:"id"`
0 ignored issues
show
struct field Id should be ID
Loading history...
39
	Latitude  float64 `json:"lat"`
40
	Longitude float64 `json:"lon"`
41
	NameRU    string  `json:"name_ru"`
42
	NameEN    string  `json:"name_en"`
43
	OKATO     string  `json:"okato"`
44
	VK        int     `json:"vk"`
45
	ISO       string  `json:"iso"`
46
	TimeZone  string  `json:"timezone"`
47
	Auto      string  `json:"auto"`
48
	UTC       int     `json:"utc"`
49
}
50
51
// GeoIPCountry contains data about country
52
type GeoIPCountry struct {
0 ignored issues
show
type name will be used as geoip.GeoIPCountry by other packages, and that stutters; consider calling this Country
Loading history...
53
	ID            int     `json:"id"`
54
	Latitude      float64 `json:"lat"`
55
	Longitude     float64 `json:"lon"`
56
	NameRU        string  `json:"name_ru"`
57
	NameEN        string  `json:"name_en"`
58
	ISO           string  `json:"iso"`
59
	Continent     string  `json:"continent"`
60
	TimeZone      string  `json:"timezone"`
61
	Area          int     `json:"area"`
62
	Population    int     `json:"population"`
63
	CapitalID     int     `json:"capital_id"`
64
	CapitalNameRU string  `json:"capital_ru"`
65
	CapitalNameEN string  `json:"capital_en"`
66
	CurrencyCode  string  `json:"cur_code"`
67
	PhonePrefix   string  `json:"phone"`
68
	Neighbours    string  `json:"neighbours"`
69
	VK            int     `json:"vk"`
70
	UTC           int     `json:"utc"`
71
}
72
73
// GetGeoIP makes request to API and returns geoip data formatted to string
74
func GetGeoIP(ctx *bot.Context) string {
75
	resp, err := http.Get(fmt.Sprintf("http://api.sypexgeo.net/json/%v", ctx.Arg(0)))
76
	if err != nil {
77
		return ctx.Loc("geoip_no_data")
78
	}
79
80
	var result GeoIP
81
	err = json.NewDecoder(resp.Body).Decode(&result)
82
	if err != nil {
83
		return ctx.Loc("error")
84
	}
85
86
	if result.City.NameEN == "" || result.Region.NameEN == "" || result.Country.NameEN == "" {
87
		return ctx.Loc("geoip_no_data")
88
	}
89
90
	if ctx.Conf.General.Language == "ru" {
91
		return fmt.Sprintf(ctx.Loc("geoip_format_string"),
0 ignored issues
show
can't check non-constant format in call to Sprintf
Loading history...
92
			result.IP, result.City.NameRU, result.Region.NameRU, result.Country.NameRU)
93
	}
94
95
	return fmt.Sprintf(ctx.Loc("geoip_format_string"),
0 ignored issues
show
can't check non-constant format in call to Sprintf
Loading history...
96
		result.IP, result.City.NameEN, result.Region.NameEN, result.Country.NameEN)
97
}
98