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
/
goinsta.go
537 lines (487 loc) · 11.5 KB
/
goinsta.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
package goinsta
import (
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
neturl "net/url"
"os"
"path/filepath"
"strconv"
"time"
)
// Instagram represent the main API handler
//
// Profiles: Represents instragram's user profile.
// Account: Represents instagram's personal account.
// Search: Represents instagram's search.
// Timeline: Represents instagram's timeline.
// Activity: Represents instagram's user activity.
// Inbox: Represents instagram's messages.
// Location: Represents instagram's locations.
//
// See Scheme section in README.md for more information.
//
// We recommend to use Export and Import functions after first Login.
//
// Also you can use SetProxy and UnsetProxy to set and unset proxy.
// Golang also provides the option to set a proxy using HTTP_PROXY env var.
type Instagram struct {
user string
pass string
// device id: android-1923fjnma8123
dID string
// uuid: 8493-1233-4312312-5123
uuid string
// rankToken
rankToken string
// token
token string
// phone id
pid string
// ads id
adid string
// challenge URL
challengeURL string
// Instagram objects
// Challenge controls security side of account (Like sms verify / It was me)
Challenge *Challenge
// Profiles is the user interaction
Profiles *Profiles
// Account stores all personal data of the user and his/her options.
Account *Account
// Search performs searching of multiple things (users, locations...)
Search *Search
// Timeline allows to receive timeline media.
Timeline *Timeline
// Activity are instagram notifications.
Activity *Activity
// Inbox are instagram message/chat system.
Inbox *Inbox
// Feed for search over feeds
Feed *Feed
// User contacts from mobile address book
Contacts *Contacts
// Location instance
Locations *LocationInstance
c *http.Client
}
// SetHTTPClient sets http client. This further allows users to use this functionality
// for HTTP testing using a mocking HTTP client Transport, which avoids direct calls to
// the Instagram, instead of returning mocked responses.
func (inst *Instagram) SetHTTPClient(client *http.Client) {
inst.c = client
}
// SetHTTPTransport sets http transport. This further allows users to tweak the underlying
// low level transport for adding additional fucntionalities.
func (inst *Instagram) SetHTTPTransport(transport http.RoundTripper) {
inst.c.Transport = transport
}
// SetDeviceID sets device id
func (inst *Instagram) SetDeviceID(id string) {
inst.dID = id
}
// SetUUID sets uuid
func (inst *Instagram) SetUUID(uuid string) {
inst.uuid = uuid
}
// SetPhoneID sets phone id
func (inst *Instagram) SetPhoneID(id string) {
inst.pid = id
}
// SetCookieJar sets the Cookie Jar. This further allows to use a custom implementation
// of a cookie jar which may be backed by a different data store such as redis.
func (inst *Instagram) SetCookieJar(jar http.CookieJar) error {
url, err := neturl.Parse(goInstaAPIUrl)
if err != nil {
return err
}
// First grab the cookies from the existing jar and we'll put it in the new jar.
cookies := inst.c.Jar.Cookies(url)
inst.c.Jar = jar
inst.c.Jar.SetCookies(url, cookies)
return nil
}
// New creates Instagram structure
func New(username, password string) *Instagram {
// this call never returns error
jar, _ := cookiejar.New(nil)
inst := &Instagram{
user: username,
pass: password,
dID: generateDeviceID(
generateMD5Hash(username + password),
),
uuid: generateUUID(), // both uuid must be differents
pid: generateUUID(),
c: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
Jar: jar,
},
}
inst.init()
return inst
}
func (inst *Instagram) init() {
inst.Challenge = newChallenge(inst)
inst.Profiles = newProfiles(inst)
inst.Activity = newActivity(inst)
inst.Timeline = newTimeline(inst)
inst.Search = newSearch(inst)
inst.Inbox = newInbox(inst)
inst.Feed = newFeed(inst)
inst.Contacts = newContacts(inst)
inst.Locations = newLocation(inst)
}
// SetProxy sets proxy for connection.
func (inst *Instagram) SetProxy(url string, insecure bool) error {
uri, err := neturl.Parse(url)
if err == nil {
inst.c.Transport = &http.Transport{
Proxy: http.ProxyURL(uri),
TLSClientConfig: &tls.Config{
InsecureSkipVerify: insecure,
},
}
}
return err
}
// UnsetProxy unsets proxy for connection.
func (inst *Instagram) UnsetProxy() {
inst.c.Transport = nil
}
// Save exports config to ~/.goinsta
func (inst *Instagram) Save() error {
home := os.Getenv("HOME")
if home == "" {
home = os.Getenv("home") // for plan9
}
return inst.Export(filepath.Join(home, ".goinsta"))
}
// Export exports *Instagram object options
func (inst *Instagram) Export(path string) error {
url, err := neturl.Parse(goInstaAPIUrl)
if err != nil {
return err
}
config := ConfigFile{
ID: inst.Account.ID,
User: inst.user,
DeviceID: inst.dID,
UUID: inst.uuid,
RankToken: inst.rankToken,
Token: inst.token,
PhoneID: inst.pid,
Cookies: inst.c.Jar.Cookies(url),
}
bytes, err := json.Marshal(config)
if err != nil {
return err
}
return ioutil.WriteFile(path, bytes, 0644)
}
// Export exports selected *Instagram object options to an io.Writer
func Export(inst *Instagram, writer io.Writer) error {
url, err := neturl.Parse(goInstaAPIUrl)
if err != nil {
return err
}
config := ConfigFile{
ID: inst.Account.ID,
User: inst.user,
DeviceID: inst.dID,
UUID: inst.uuid,
RankToken: inst.rankToken,
Token: inst.token,
PhoneID: inst.pid,
Cookies: inst.c.Jar.Cookies(url),
}
bytes, err := json.Marshal(config)
if err != nil {
return err
}
_, err = writer.Write(bytes)
return err
}
// ImportReader imports instagram configuration from io.Reader
//
// This function does not set proxy automatically. Use SetProxy after this call.
func ImportReader(r io.Reader) (*Instagram, error) {
bytes, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
config := ConfigFile{}
err = json.Unmarshal(bytes, &config)
if err != nil {
return nil, err
}
return ImportConfig(config)
}
// ImportConfig imports instagram configuration from a configuration object.
//
// This function does not set proxy automatically. Use SetProxy after this call.
func ImportConfig(config ConfigFile) (*Instagram, error) {
url, err := neturl.Parse(goInstaAPIUrl)
if err != nil {
return nil, err
}
inst := &Instagram{
user: config.User,
dID: config.DeviceID,
uuid: config.UUID,
rankToken: config.RankToken,
token: config.Token,
pid: config.PhoneID,
c: &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
},
}
inst.c.Jar, err = cookiejar.New(nil)
if err != nil {
return inst, err
}
inst.c.Jar.SetCookies(url, config.Cookies)
inst.init()
inst.Account = &Account{inst: inst, ID: config.ID}
inst.Account.Sync()
return inst, nil
}
// Import imports instagram configuration
//
// This function does not set proxy automatically. Use SetProxy after this call.
func Import(path string) (*Instagram, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return ImportReader(f)
}
func (inst *Instagram) readMsisdnHeader() error {
data, err := json.Marshal(
map[string]string{
"device_id": inst.uuid,
},
)
if err != nil {
return err
}
_, err = inst.sendRequest(
&reqOptions{
Endpoint: urlMsisdnHeader,
IsPost: true,
Connection: "keep-alive",
Query: generateSignature(b2s(data)),
},
)
return err
}
func (inst *Instagram) contactPrefill() error {
data, err := json.Marshal(
map[string]string{
"phone_id": inst.pid,
"_csrftoken": inst.token,
"usage": "prefill",
},
)
if err != nil {
return err
}
_, err = inst.sendRequest(
&reqOptions{
Endpoint: urlContactPrefill,
IsPost: true,
Connection: "keep-alive",
Query: generateSignature(b2s(data)),
},
)
return err
}
func (inst *Instagram) zrToken() error {
_, err := inst.sendRequest(
&reqOptions{
Endpoint: urlZrToken,
IsPost: false,
Connection: "keep-alive",
Query: map[string]string{
"device_id": inst.dID,
"token_hash": "",
"custom_device_id": inst.uuid,
"fetch_reason": "token_expired",
},
},
)
return err
}
func (inst *Instagram) sendAdID() error {
data, err := inst.prepareData(
map[string]interface{}{
"adid": inst.adid,
},
)
if err != nil {
return err
}
_, err = inst.sendRequest(
&reqOptions{
Endpoint: urlLogAttribution,
IsPost: true,
Connection: "keep-alive",
Query: generateSignature(data),
},
)
return err
}
// Login performs instagram login.
//
// Password will be deleted after login
func (inst *Instagram) Login() error {
err := inst.readMsisdnHeader()
if err != nil {
return err
}
err = inst.syncFeatures()
if err != nil {
return err
}
err = inst.zrToken()
if err != nil {
return err
}
err = inst.sendAdID()
if err != nil {
return err
}
err = inst.contactPrefill()
if err != nil {
return err
}
result, err := json.Marshal(
map[string]interface{}{
"guid": inst.uuid,
"login_attempt_count": 0,
"_csrftoken": inst.token,
"device_id": inst.dID,
"adid": inst.adid,
"phone_id": inst.pid,
"username": inst.user,
"password": inst.pass,
"google_tokens": "[]",
},
)
if err != nil {
return err
}
body, err := inst.sendRequest(
&reqOptions{
Endpoint: urlLogin,
Query: generateSignature(b2s(result)),
IsPost: true,
Login: true,
},
)
if err != nil {
return err
}
inst.pass = ""
// getting account data
res := accountResp{}
err = json.Unmarshal(body, &res)
if err != nil {
return err
}
inst.Account = &res.Account
inst.Account.inst = inst
inst.rankToken = strconv.FormatInt(inst.Account.ID, 10) + "_" + inst.uuid
inst.zrToken()
return err
}
// Logout closes current session
func (inst *Instagram) Logout() error {
_, err := inst.sendSimpleRequest(urlLogout)
inst.c.Jar = nil
inst.c = nil
return err
}
func (inst *Instagram) syncFeatures() error {
data, err := inst.prepareData(
map[string]interface{}{
"id": inst.uuid,
"experiments": goInstaExperiments,
},
)
if err != nil {
return err
}
_, err = inst.sendRequest(
&reqOptions{
Endpoint: urlQeSync,
Query: generateSignature(data),
IsPost: true,
Login: true,
},
)
return err
}
func (inst *Instagram) megaphoneLog() error {
data, err := inst.prepareData(
map[string]interface{}{
"id": inst.Account.ID,
"type": "feed_aysf",
"action": "seen",
"reason": "",
"device_id": inst.dID,
"uuid": generateMD5Hash(string(time.Now().Unix())),
},
)
if err != nil {
return err
}
_, err = inst.sendRequest(
&reqOptions{
Endpoint: urlMegaphoneLog,
Query: generateSignature(data),
IsPost: true,
Login: true,
},
)
return err
}
func (inst *Instagram) expose() error {
data, err := inst.prepareData(
map[string]interface{}{
"id": inst.Account.ID,
"experiment": "ig_android_profile_contextual_feed",
},
)
if err != nil {
return err
}
_, err = inst.sendRequest(
&reqOptions{
Endpoint: urlExpose,
Query: generateSignature(data),
IsPost: true,
},
)
return err
}
// GetMedia returns media specified by id.
//
// The argument can be int64 or string
//
// See example: examples/media/like.go
func (inst *Instagram) GetMedia(o interface{}) (*FeedMedia, error) {
media := &FeedMedia{
inst: inst,
NextID: o,
}
return media, media.Sync()
}