-
Notifications
You must be signed in to change notification settings - Fork 56
/
utils.go
309 lines (278 loc) · 6.89 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package goinsta
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"image"
"math"
"regexp"
// Required for getImageDimensionFromReader in jpg and png format
"fmt"
_ "image/jpeg"
_ "image/png"
"strconv"
"time"
)
func formatID(id interface{}) string {
switch s := id.(type) {
case string:
return s
case int64:
return strconv.FormatInt(s, 10)
case json.Number:
return string(s)
}
return ""
}
func toString(i interface{}) string {
switch s := i.(type) {
case string:
return s
case bool:
return strconv.FormatBool(s)
case float64:
return strconv.FormatFloat(s, 'f', -1, 64)
case float32:
return strconv.FormatFloat(float64(s), 'f', -1, 32)
case int:
return strconv.Itoa(s)
case int64:
return strconv.FormatInt(s, 10)
case int32:
return strconv.Itoa(int(s))
case int16:
return strconv.FormatInt(int64(s), 10)
case int8:
return strconv.FormatInt(int64(s), 10)
case uint:
return strconv.FormatInt(int64(s), 10)
case uint64:
return strconv.FormatInt(int64(s), 10)
case uint32:
return strconv.FormatInt(int64(s), 10)
case uint16:
return strconv.FormatInt(int64(s), 10)
case uint8:
return strconv.FormatInt(int64(s), 10)
case []byte:
return string(s)
case error:
return s.Error()
}
return ""
}
func prepareRecipients(cc interface{}) (bb string, err error) {
var b []byte
ids := make([][]int64, 0)
switch c := cc.(type) {
case *Conversation:
for i := range c.Users {
ids = append(ids, []int64{c.Users[i].ID})
}
case *Item:
ids = append(ids, []int64{c.User.ID})
case int64:
ids = append(ids, []int64{c})
}
b, err = json.Marshal(ids)
bb = string(b)
return
}
// getImageSize return image dimension , types is .jpg and .png
func getImageSize(b []byte) (int, int, error) {
buf := bytes.NewReader(b)
image, _, err := image.DecodeConfig(buf)
if err != nil {
return 0, 0, err
}
return image.Width, image.Height, nil
}
func getVideoInfo(b []byte) (height, width, duration int, err error) {
keys := []string{"moov", "trak", "stbl", "avc1"}
height, err = read16(b, keys, 24)
if err != nil {
return
}
width, err = read16(b, keys, 26)
if err != nil {
return
}
duration, err = getMP4Duration(b)
if err != nil {
return
}
return
}
func getMP4Duration(b []byte) (int, error) {
keys := []string{"moov", "mvhd"}
timescale, err := read32(b, keys, 12)
if err != nil {
return -1, err
}
// If timescale failed to read a value, return -1 * 1000
if timescale == 0 {
return -1000, nil
}
length, err := read32(b, keys, 12+4)
if err != nil {
return -1, err
}
return int(math.Floor(float64(length) / float64(timescale) * 1000)), nil
}
func getTimeOffset() string {
_, offset := time.Now().Zone()
return strconv.Itoa(offset)
}
func jazoest(str string) string {
b := []byte(str)
var s int
for v := range b {
s += v
}
return "2" + strconv.Itoa(s)
}
func createUserAgent(device Device) string {
// Instagram 195.0.0.31.123 Android (28/9; 560dpi; 1440x2698; LGE/lge; LG-H870DS; lucye; lucye; en_GB; 302733750)
// Instagram 195.0.0.31.123 Android (28/9; 560dpi; 1440x2872; Genymotion/Android; Samsung Galaxy S10; vbox86p; vbox86; en_US; 302733773) # version_code: 302733773
// Instagram 195.0.0.31.123 Android (30/11; 560dpi; 1440x2898; samsung; SM-G975F; beyond2; exynos9820; en_US; 302733750)
return fmt.Sprintf("Instagram %s Android (%d/%d; %s; %s; %s; %s; %s; %s; %s; %s)",
appVersion,
device.AndroidVersion,
device.AndroidRelease,
device.ScreenDpi,
device.ScreenResolution,
device.Manufacturer,
device.Model,
device.CodeName,
device.Chipset,
locale,
appVersionCode,
)
}
// ExportAsBytes exports selected *Instagram object as []byte
func (insta *Instagram) ExportAsBytes() ([]byte, error) {
buffer := &bytes.Buffer{}
err := insta.ExportIO(buffer)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// ExportAsBase64String exports selected *Instagram object as base64 encoded string
func (insta *Instagram) ExportAsBase64String() (string, error) {
bytes, err := insta.ExportAsBytes()
if err != nil {
return "", err
}
sEnc := base64.StdEncoding.EncodeToString(bytes)
return sEnc, nil
}
// ImportFromBytes imports instagram configuration from an array of bytes.
//
// This function does not set proxy automatically. Use SetProxy after this call.
func ImportFromBytes(inputBytes []byte, args ...interface{}) (*Instagram, error) {
return ImportReader(bytes.NewReader(inputBytes), args...)
}
// ImportFromBase64String imports instagram configuration from a base64 encoded string.
//
// This function does not set proxy automatically. Use SetProxy after this call.
func ImportFromBase64String(base64String string, args ...interface{}) (*Instagram, error) {
sDec, err := base64.StdEncoding.DecodeString(base64String)
if err != nil {
return nil, err
}
return ImportFromBytes(sDec, args...)
}
func MergeMapI(one map[string]interface{}, extra ...map[string]interface{}) map[string]interface{} {
for _, e := range extra {
for k, v := range e {
one[k] = v
}
}
return one
}
func MergeMapS(one map[string]string, extra ...map[string]string) map[string]string {
for _, e := range extra {
for k, v := range e {
one[k] = v
}
}
return one
}
func read16(b []byte, keys []string, offset int) (int, error) {
start, err := getStartByte(b, keys, offset)
if err != nil {
return -1, nil
}
r := binary.BigEndian.Uint16(b[start+offset:])
return int(r), nil
}
func read32(b []byte, keys []string, offset int) (int, error) {
start, err := getStartByte(b, keys, offset)
if err != nil {
return -1, nil
}
r := binary.BigEndian.Uint32(b[start+offset:])
return int(r), nil
}
func getStartByte(b []byte, keys []string, offset int) (int, error) {
start := 0
for _, key := range keys {
n := bytes.Index(b[start:], []byte(key))
if n == -1 {
return -1, ErrByteIndexNotFound
}
start += n + len(key)
}
return start, nil
}
func getSupCap() (string, error) {
query := []trayRequest{
{"SUPPORTED_SDK_VERSIONS", supportedSdkVersions},
{"FACE_TRACKER_VERSION", facetrackerVersion},
{"segmentation", segmentation},
{"COMPRESSION", compression},
{"world_tracker", worldTracker},
{"gyroscope", gyroscope},
}
data, err := json.Marshal(query)
if err != nil {
return "", err
}
return string(data), nil
}
func randNum(l int) string {
var num string
for i := 0; i < l; i++ {
num += toString(random(0, 9))
}
return num
}
// checkHeadlessErr will return a proper error if a chrome browser was not found.
func checkHeadlessErr(err error) error {
// Check if err = Chrome not found
if err != nil {
if matched, reErr := regexp.Match("executable file not found", []byte(err.Error())); reErr != nil {
return reErr
} else if matched {
return ErrChromeNotFound
}
return err
}
return nil
}
func errIsFatal(err error) bool {
switch err {
case ErrBadPassword:
fallthrough
case Err2FARequired:
fallthrough
case ErrLoggedOut:
fallthrough
case ErrLoginRequired:
return true
default:
return false
}
}