-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdocs.go
119 lines (95 loc) · 2.47 KB
/
docs.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
package vkapi
import (
"encoding/json"
"io/ioutil"
"net/url"
"strconv"
"strings"
)
type DocAttachment struct {
ID int `json:"id"`
OwnerID int `json:"owner_id"`
Title string `json:"title"`
Size int `json:"size"`
Extenstion string `json:"ext"`
URL string `json:"url"`
Date int64 `json:"date"`
Type int `json:"type"`
IsLicensed int `json:"is_licensed"`
}
type Docs struct {
Count int `json:"count"`
Documents []*DocAttachment `json:"items"`
}
func (client *VKClient) docGetWallUploadServer(groupID int) (string, error) {
gidStr := strconv.Itoa(groupID)
if gidStr[0] == '-' {
gidStr = gidStr[1:]
}
params := url.Values{}
params.Set("group_id", gidStr)
resp, err := client.MakeRequest("docs.getWallUploadServer", params)
if err != nil {
return "", err
}
m := map[string]string{}
json.Unmarshal(resp.Response, &m)
return m["upload_url"], err
}
func (client *VKClient) docWallUpload(groupID int, fileName string) (string, error) {
uploadURL, err := client.docGetWallUploadServer(groupID)
if err != nil {
return "", err
}
req, err := client.getDocMultipartReq(uploadURL, fileName)
if err != nil {
return "", err
}
resp, err := client.Client.Do(req)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
m := map[string]string{}
json.Unmarshal(body, &m)
return m["file"], nil
}
func (client *VKClient) UploadGroupWallDoc(groupID int, fileName string) (*DocAttachment, error) {
file, err := client.docWallUpload(groupID, fileName)
if err != nil {
return nil, err
}
params := url.Values{}
params.Set("file", file)
resp, err := client.MakeRequest("docs.save", params)
if err != nil {
return nil, err
}
var docs []*DocAttachment
json.Unmarshal(resp.Response, &docs)
return docs[0], err
}
func (client *VKClient) GetDocsString(docs []*DocAttachment) string {
s := []string{}
for _, d := range docs {
s = append(s, "doc"+strconv.Itoa(d.OwnerID)+"_"+strconv.Itoa(d.ID))
}
return strings.Join(s, ",")
}
func (client *VKClient) DocsSearch(query string, count int, params url.Values) (int, []*DocAttachment, error) {
if params == nil {
params = url.Values{}
}
params.Set("q", query)
params.Set("count", strconv.Itoa(count))
resp, err := client.MakeRequest("docs.search", params)
if err != nil {
return 0, nil, err
}
var docs *Docs
json.Unmarshal(resp.Response, &docs)
return docs.Count, docs.Documents, nil
}