-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
79 lines (73 loc) · 1.63 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package vkutil
import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/zhuharev/vk"
"net/url"
"strings"
)
func (api *Api) GetCitiesById(id int, ids ...int) ([]City, error) {
form := url.Values{"city_ids": {fmt.Sprint(id)}}
if len(ids) > 0 {
ids = append([]int{id}, ids...)
form.Set("city_ids", strings.Join(arrIntToStr(ids), ","))
}
resp, e := api.VkApi.Request(vk.METHOD_DATABASE_GET_CITIES_BY_ID, form)
if e != nil {
return nil, e
}
type cities struct {
Response []City `json:"response"`
}
var c cities
e = json.Unmarshal(resp, &c)
return c.Response, e
}
func (api *Api) GetCityById(id int) (c City, e error) {
var (
cts []City
)
cts, e = api.GetCitiesById(id)
if e != nil {
return
}
if len(cts) != 1 {
e = fmt.Errorf("Unknown err expected %d, got %d", 1, len(cts))
return
}
return cts[0], nil
}
func (api *Api) GetCityId(cname string) (int, error) {
// russia only
resp, e := api.VkApi.Request(vk.METHOD_DATABASE_GET_CITIES, url.Values{"q": {cname}, "country_id": {"1"}, "count": {"1"}})
if e != nil {
return 0, e
}
var rc ResponseCities
e = json.Unmarshal(resp, &rc)
if e != nil {
return 0, e
}
color.Green("%v", rc.Response.Items)
if rc.Response.Count == 0 {
return 0, fmt.Errorf("City %s not found", cname)
}
return rc.Response.Items[0].Id, nil
}
func (api *Api) GetCities(q string) (r ResponseCities, e error) {
r = ResponseCities{}
var (
resp []byte
)
resp, e = api.VkApi.Request(vk.METHOD_DATABASE_GET_CITIES, url.Values{"q": {q}, "country_id": {"1"}})
if e != nil {
return
}
e = json.Unmarshal(resp, &r)
if e != nil {
color.Green("%s", resp)
return
}
return
}