-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
152 lines (130 loc) · 2.85 KB
/
utils.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package vkutil
import (
"encoding/json"
"fmt"
"net/url"
"reflect"
"sort"
"strconv"
"strings"
"github.com/zhuharev/vk"
"github.com/zhuharev/vkutil/util"
)
func (api *Api) UtilsResolveScreenName(name string) (ObjectType, int, error) {
type Responseo struct {
Respo struct {
Type ObjectType `json:"type"`
ObjectId int `json:"object_id"`
} `json:"response"`
}
bts, e := api.VkApi.Request(vk.METHOD_UTILS_RESOLVE_SCREEN_NAME, url.Values{
"screen_name": {name},
})
if e != nil {
return "", 0, e
}
fmt.Println(string(bts))
var res Responseo
e = json.Unmarshal(bts, &res)
return res.Respo.Type, res.Respo.ObjectId, e
}
// vkutil utils
func arrIntToStr(arr []int) (sarr []string) {
return util.ArrIntToStr(arr)
}
func arrIntToString(arr []int) string {
return strings.Join(arrIntToStr(arr), ",")
}
func toString(i int) string {
return strconv.Itoa(i)
}
func uniqStrArr(arr []string) []string {
var m = map[string]interface{}{}
for _, v := range arr {
m[v] = nil
}
arr = []string{}
for k := range m {
if k == "" || k == "0" {
continue
}
arr = append(arr, k)
}
return arr
}
func arrSplit1K(arr []int) (res [][]int) {
//res = append(res, []int)
for i, v := range arr {
if i%700 == 0 {
res = append(res, []int{})
}
res[len(res)-1] = append(res[len(res)-1], v)
}
return
}
func arrUniq(in []int) (out []int) {
var (
dup = map[int]struct{}{}
)
for _, v := range in {
if _, ok := dup[v]; !ok {
out = append(out, v)
dup[v] = struct{}{}
}
}
sort.Ints(out)
return
}
func setToUrlValues(key string, val interface{}, params ...url.Values) url.Values {
return util.SetToUrlValues(key, val, params...)
}
func ParseCallbackURL(uri string) (token string, err error) {
u, err := url.Parse(uri)
if err != nil {
return
}
vals, err := url.ParseQuery(u.Fragment)
if err != nil {
return
}
return vals.Get("access_token"), nil
}
func ParseDomain(uri string) (token string, err error) {
u, err := url.Parse(uri)
if err != nil {
return
}
return strings.TrimPrefix(u.Path, "/"), nil
}
func ParseBoardURL(uri string) (groupID, topicID, postID int, err error) {
uri = strings.TrimSuffix(uri, "?offset=last&scroll=1")
if strings.Contains(uri, "?") {
_, err = fmt.Sscanf(uri, "https://vk.com/topic-%d_%d?offset=%d", &groupID, &topicID, &postID)
if err != nil {
return
}
return
}
_, err = fmt.Sscanf(uri, "https://vk.com/topic-%d_%d", &groupID, &topicID)
if err != nil {
return
}
return
}
// IsZeroOfUnderlyingType check is x zero value
func IsZeroOfUnderlyingType(x interface{}) bool {
return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}
// add b to a
func joinMaps(a, b map[int][]int) map[int][]int {
// create new map
m := make(map[int][]int)
// copy a to m
for k, arr := range a {
m[k] = arr
}
for k, arr := range b {
m[k] = append(m[k], arr...)
}
return m
}