Passed
Push — master ( 549bf0...6fd40e )
by Viktor
02:41
created

geoip.GetGeoIP   A

Complexity

Conditions 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nop 1
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
1
package geoip
2
3
import (
4
	"encoding/json"
5
	"fmt"
6
	"github.com/FlameInTheDark/dtbot/bot"
7
	"net/http"
8
)
9
10
type GeoIP struct {
0 ignored issues
show
introduced by
exported type GeoIP should have comment or be unexported
Loading history...
11
	IP        string       `json:"ip"`
12
	City      GeoIPCity    `json:"city"`
13
	Region    GeoIPRegion  `json:"region"`
14
	Country   GeoIPCountry `json:"country"`
15
	Error     string       `json:"error"`
16
	Requests  int          `json:"requests"`
17
	Created   string       `json:"created"`
18
	TimeStamp int          `json:"timestamp"`
19
}
20
21
type GeoIPCity struct {
0 ignored issues
show
introduced by
exported type GeoIPCity should have comment or be unexported
Loading history...
introduced by
type name will be used as geoip.GeoIPCity by other packages, and that stutters; consider calling this City
Loading history...
22
	Id         int     `json:"id"`
0 ignored issues
show
introduced by
struct field Id should be ID
Loading history...
23
	Latitude   float64 `json:"lat"`
24
	Longitude  float64 `json:"lon"`
25
	NameRU     string  `json:"name_ru"`
26
	NameEN     string  `json:"name_en"`
27
	OKATO      string  `json:"okato"`
28
	VK         int     `json:"vk"`
29
	Population int     `json:"population"`
30
	Tel        string  `json:"tel"`
31
	PostalCode string  `json:"post"`
32
}
33
34
type GeoIPRegion struct {
0 ignored issues
show
introduced by
exported type GeoIPRegion should have comment or be unexported
Loading history...
introduced by
type name will be used as geoip.GeoIPRegion by other packages, and that stutters; consider calling this Region
Loading history...
35
	Id        int     `json:"id"`
0 ignored issues
show
introduced by
struct field Id should be ID
Loading history...
36
	Latitude  float64 `json:"lat"`
37
	Longitude float64 `json:"lon"`
38
	NameRU    string  `json:"name_ru"`
39
	NameEN    string  `json:"name_en"`
40
	OKATO     string  `json:"okato"`
41
	VK        int     `json:"vk"`
42
	ISO       string  `json:"iso"`
43
	TimeZone  string  `json:"timezone"`
44
	Auto      string  `json:"auto"`
45
	UTC       int     `json:"utc"`
46
}
47
48
type GeoIPCountry struct {
0 ignored issues
show
introduced by
exported type GeoIPCountry should have comment or be unexported
Loading history...
introduced by
type name will be used as geoip.GeoIPCountry by other packages, and that stutters; consider calling this Country
Loading history...
49
	ID            int     `json:"id"`
50
	Latitude      float64 `json:"lat"`
51
	Longitude     float64 `json:"lon"`
52
	NameRU        string  `json:"name_ru"`
53
	NameEN        string  `json:"name_en"`
54
	ISO           string  `json:"iso"`
55
	Continent     string  `json:"continent"`
56
	TimeZone      string  `json:"timezone"`
57
	Area          int     `json:"area"`
58
	Population    int     `json:"population"`
59
	CapitalID     int     `json:"capital_id"`
60
	CapitalNameRU string  `json:"capital_ru"`
61
	CapitalNameEN string  `json:"capital_en"`
62
	CurrencyCode  string  `json:"cur_code"`
63
	PhonePrefix   string  `json:"phone"`
64
	Neighbours    string  `json:"neighbours"`
65
	VK            int     `json:"vk"`
66
	UTC           int     `json:"utc"`
67
}
68
69
func GetGeoIP(ctx *bot.Context) string {
0 ignored issues
show
introduced by
exported function GetGeoIP should have comment or be unexported
Loading history...
70
	resp, err := http.Get(fmt.Sprintf("http://api.sypexgeo.net/json/%v", ctx.Args[0]))
71
	if err != nil {
72
		return ctx.Loc("geoip_no_data")
73
	}
74
75
	var result GeoIP
76
	err = json.NewDecoder(resp.Body).Decode(&result)
77
	if err != nil {
78
		return ctx.Loc("error")
79
	}
80
81
	return fmt.Sprintf("IP [%v]\nCity: %v\nRegion: %v\nCountry: %v",
82
		result.IP, result.City.NameRU, result.Region.NameRU, result.Country.NameRU)
83
84
}
85