-
Notifications
You must be signed in to change notification settings - Fork 0
/
vk.go
111 lines (90 loc) · 2.15 KB
/
vk.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
package tgBotVkPostSendler
import (
"errors"
"net/url"
"time"
)
const (
version = "5.102"
vkUrl = "https://vk.com/"
reqUrl = "https://api.vk.com/method/wall.get?"
)
var mapFilter = map[string]struct{}{
"suggestions": struct{}{},
"postponed": struct{}{},
"owner": struct{}{},
"others": struct{}{},
"all": struct{}{},
}
type Message struct {
ID string
Text string
Error error
}
func (h *Handler) GetVkPosts(groupID, vkServiceKey string) <-chan Message {
out := make(chan Message)
if _, ok := mapFilter[h.Options.Filter]; !ok {
h.ErrChan <- errors.New("unexpected vk-filter in the request")
return out
}
u := url.Values{}
u.Set("count", h.Options.Count)
u.Set("offset", h.Options.Offset)
u.Set("filter", h.Options.Filter)
u.Set("owner_id", groupID)
u.Set("access_token", vkServiceKey)
u.Set("v", version)
u.Set("extended", "1") // is it really important?
path := reqUrl + u.Encode()
go h.loop(groupID, path, out)
return out
}
// restrictions:
// wall.get — 5000 requests per day. -> ~1 req per 20seconds
// https://vk.com/dev/data_limits
var timeout time.Duration = 30 * time.Second // TODO: tmp solution
func (h *Handler) loop(groupID, path string, out chan Message) {
var isFirstReq = true
for {
if !isFirstReq {
time.Sleep(timeout)
}
if isFirstReq {
isFirstReq = false
}
body, err := getPosts(path)
if err != nil {
out <- Message{Error: err}
continue
}
ids, err := h.DbWriter.SelectCompletedRows()
if err != nil {
out <- Message{Error: err}
continue
}
posts := getDiffPosts(ids, body.Items)
// send posts from the latest to the earliest
for i := len(posts) - 1; i >= 0; i-- {
h.DbWriter.id = string(posts[i].ID)
h.DbWriter.text = makeMessage(posts[i], groupID)
if err := h.DbWriter.InsertToDb(); err != nil {
out <- Message{Error: err}
continue
}
out <- Message{
ID: h.DbWriter.id,
Text: h.DbWriter.text,
}
}
}
}
func getDiffPosts(ids map[string]struct{}, input []data) []data {
out := make([]data, 0, len(input))
for _, v := range input {
if _, ok := ids[string(v.ID)]; ok {
continue
}
out = append(out, v)
}
return out
}