This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
/
request.go
192 lines (164 loc) · 3.86 KB
/
request.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package goinsta
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type reqOptions struct {
// Connection is connection header. Default is "close".
Connection string
// Login process
Login bool
// Endpoint is the request path of instagram api
Endpoint string
// IsPost set to true will send request with POST method.
//
// By default this option is false.
IsPost bool
// UseV2 is set when API endpoint uses v2 url.
UseV2 bool
// Query is the parameters of the request
//
// This parameters are independents of the request method (POST|GET)
Query map[string]string
}
func (insta *Instagram) sendSimpleRequest(uri string, a ...interface{}) (body []byte, err error) {
return insta.sendRequest(
&reqOptions{
Endpoint: fmt.Sprintf(uri, a...),
},
)
}
func (insta *Instagram) sendRequest(o *reqOptions) (body []byte, err error) {
method := "GET"
if o.IsPost {
method = "POST"
}
if o.Connection == "" {
o.Connection = "keep-alive"
}
nu := goInstaAPIUrl
if o.UseV2 {
nu = goInstaAPIUrlv2
}
u, err := url.Parse(nu + o.Endpoint)
if err != nil {
return nil, err
}
vs := url.Values{}
bf := bytes.NewBuffer([]byte{})
for k, v := range o.Query {
vs.Add(k, v)
}
if o.IsPost {
bf.WriteString(vs.Encode())
} else {
for k, v := range u.Query() {
vs.Add(k, strings.Join(v, " "))
}
u.RawQuery = vs.Encode()
}
var req *http.Request
req, err = http.NewRequest(method, u.String(), bf)
if err != nil {
return
}
req.Header.Set("Connection", o.Connection)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("Accept-Language", "en-US")
req.Header.Set("User-Agent", goInstaUserAgent)
req.Header.Set("X-IG-App-ID", fbAnalytics)
req.Header.Set("X-IG-Capabilities", igCapabilities)
req.Header.Set("X-IG-Connection-Type", connType)
req.Header.Set("X-IG-Connection-Speed", fmt.Sprintf("%dkbps", acquireRand(1000, 3700)))
req.Header.Set("X-IG-Bandwidth-Speed-KBPS", "-1.000")
req.Header.Set("X-IG-Bandwidth-TotalBytes-B", "0")
req.Header.Set("X-IG-Bandwidth-TotalTime-MS", "0")
resp, err := insta.c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
u, _ = url.Parse(goInstaAPIUrl)
for _, value := range insta.c.Jar.Cookies(u) {
if strings.Contains(value.Name, "csrftoken") {
insta.token = value.Value
}
}
body, err = ioutil.ReadAll(resp.Body)
if err == nil {
err = isError(resp.StatusCode, body)
}
return body, err
}
func isError(code int, body []byte) (err error) {
switch code {
case 200:
case 503:
return Error503{
Message: "Instagram API error. Try it later.",
}
case 400:
ierr := Error400{}
err = json.Unmarshal(body, &ierr)
if err != nil {
return err
}
if ierr.Message == "challenge_required" {
return ierr.ChallengeError
}
if err == nil && ierr.Message != "" {
return ierr
}
default:
ierr := ErrorN{}
err = json.Unmarshal(body, &ierr)
if err != nil {
return err
}
return ierr
}
return nil
}
func (insta *Instagram) prepareData(other ...map[string]interface{}) (string, error) {
data := map[string]interface{}{
"_uuid": insta.uuid,
"_csrftoken": insta.token,
}
if insta.Account != nil && insta.Account.ID != 0 {
data["_uid"] = strconv.FormatInt(insta.Account.ID, 10)
}
for i := range other {
for key, value := range other[i] {
data[key] = value
}
}
b, err := json.Marshal(data)
if err == nil {
return b2s(b), err
}
return "", err
}
func (insta *Instagram) prepareDataQuery(other ...map[string]interface{}) map[string]string {
data := map[string]string{
"_uuid": insta.uuid,
"_csrftoken": insta.token,
}
for i := range other {
for key, value := range other[i] {
data[key] = toString(value)
}
}
return data
}
func acquireRand(min, max int) int {
rand.Seed(time.Now().Unix())
return rand.Intn(max-min) + min
}