-
Notifications
You must be signed in to change notification settings - Fork 56
/
account.go
589 lines (531 loc) · 15 KB
/
account.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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
package goinsta
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/Davincible/goinsta/v3/utilities"
)
// Account is personal account object
//
// See examples: examples/account/*
type Account struct {
insta *Instagram
ID int64 `json:"pk"`
Username string `json:"username"`
FullName string `json:"full_name"`
Biography string `json:"biography"`
ProfilePicURL string `json:"profile_pic_url"`
Email string `json:"email"`
PhoneNumber string `json:"phone_number"`
IsBusiness bool `json:"is_business"`
Gender int `json:"gender"`
ProfilePicID string `json:"profile_pic_id"`
CanSeeOrganicInsights bool `json:"can_see_organic_insights"`
ShowInsightsTerms bool `json:"show_insights_terms"`
Nametag Nametag `json:"nametag"`
HasAnonymousProfilePicture bool `json:"has_anonymous_profile_picture"`
IsPrivate bool `json:"is_private"`
IsUnpublished bool `json:"is_unpublished"`
AllowedCommenterType string `json:"allowed_commenter_type"`
IsVerified bool `json:"is_verified"`
MediaCount int `json:"media_count"`
FollowerCount int `json:"follower_count"`
FollowingCount int `json:"following_count"`
GeoMediaCount int `json:"geo_media_count"`
ExternalURL string `json:"external_url"`
HasBiographyTranslation bool `json:"has_biography_translation"`
ExternalLynxURL string `json:"external_lynx_url"`
HdProfilePicURLInfo PicURLInfo `json:"hd_profile_pic_url_info"`
HdProfilePicVersions []PicURLInfo `json:"hd_profile_pic_versions"`
UsertagsCount int `json:"usertags_count"`
HasChaining bool `json:"has_chaining"`
ReelAutoArchive string `json:"reel_auto_archive"`
PublicEmail string `json:"public_email"`
PublicPhoneNumber string `json:"public_phone_number"`
PublicPhoneCountryCode string `json:"public_phone_country_code"`
ContactPhoneNumber string `json:"contact_phone_number"`
Byline string `json:"byline"`
SocialContext string `json:"social_context,omitempty"`
SearchSocialContext string `json:"search_social_context,omitempty"`
MutualFollowersCount float64 `json:"mutual_followers_count"`
LatestReelMedia int64 `json:"latest_reel_media,omitempty"`
CityID int64 `json:"city_id"`
CityName string `json:"city_name"`
AddressStreet string `json:"address_street"`
DirectMessaging string `json:"direct_messaging"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Category string `json:"category"`
BusinessContactMethod string `json:"business_contact_method"`
IsCallToActionEnabled bool `json:"is_call_to_action_enabled"`
FbPageCallToActionID string `json:"fb_page_call_to_action_id"`
Zip string `json:"zip"`
AllowContactsSync bool `json:"allow_contacts_sync"`
CanBoostPost bool `json:"can_boost_post"`
}
// Sync updates account information
func (account *Account) Sync() error {
insta := account.insta
body, _, err := insta.sendRequest(&reqOptions{
Endpoint: urlCurrentUser,
Query: map[string]string{
"edit": "true",
},
})
if err != nil {
return err
}
resp := profResp{}
err = json.Unmarshal(body, &resp)
if err != nil {
return err
}
*account = resp.Account
account.insta = insta
return nil
}
// ChangePassword changes current password.
//
// GoInsta does not store current instagram password (for security reasons)
// If you want to change your password you must parse old and new password.
//
// See example: examples/account/changePass.go
func (account *Account) ChangePassword(old, new_ string) error {
insta := account.insta
timestamp := strconv.FormatInt(time.Now().Unix(), 10)
old, err := utilities.EncryptPassword(old, insta.pubKey, insta.pubKeyID, timestamp)
if err != nil {
return err
}
new_, err = utilities.EncryptPassword(new_, insta.pubKey, insta.pubKeyID, timestamp)
if err != nil {
return err
}
data, err := json.Marshal(
map[string]string{
"_uid": toString(insta.Account.ID),
"_uuid": insta.uuid,
"enc_old_password": old,
"enc_new_password1": new_,
"enc_new_password2": new_,
},
)
if err == nil {
_, _, err = insta.sendRequest(
&reqOptions{
Endpoint: urlChangePass,
Query: generateSignature(data),
IsPost: true,
},
)
}
return err
}
type profResp struct {
Status string `json:"status"`
Account Account `json:"user"`
}
// RemoveProfilePic removes current profile picture
//
// This function updates current Account information.
//
// See example: examples/account/removeProfilePic.go
func (account *Account) RemoveProfilePic() error {
insta := account.insta
data, err := json.Marshal(
map[string]string{
"_uid": toString(insta.Account.ID),
"_uuid": insta.uuid,
},
)
if err != nil {
return err
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlRemoveProfPic,
IsPost: true,
Query: generateSignature(data),
},
)
if err == nil {
resp := profResp{}
err = json.Unmarshal(body, &resp)
if err == nil {
*account = resp.Account
account.insta = insta
}
}
return err
}
// ChangeProfilePic Update profile picture
//
// See example: examples/account/change-profile-pic/main.go
func (account *Account) ChangeProfilePic(photo io.Reader) error {
insta := account.insta
buf, err := readFile(photo)
if err != nil {
return fmt.Errorf("ChangeProfilePic readFile: %w", err)
}
o := UploadOptions{
insta: insta,
File: photo,
buf: buf,
}
t := http.DetectContentType(buf.Bytes())
if t != "image/jpeg" {
return ErrInvalidImage
}
if err = o.uploadPhoto(); err != nil {
return fmt.Errorf("ChangeProfilePic uploadPhoto: %w", err)
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlChangeProfPic,
Query: map[string]string{
"_uuid": insta.uuid,
"use_fbuploader": "true",
"upload_id": o.uploadID,
},
IsPost: true,
},
)
if err != nil {
return fmt.Errorf("ChangeProfilePic unmarshal response json: %w", err)
}
resp := profResp{}
if err = json.Unmarshal(body, &resp); err != nil {
return fmt.Errorf("Failed to unmarshal account from json resposne: %w", err)
}
*account = resp.Account
account.insta = insta
return nil
}
// SetPrivate sets account to private mode.
//
// This function updates current Account information.
//
// See example: examples/account/setPrivate.go
func (account *Account) SetPrivate() error {
return account.changePublic(urlSetPrivate)
}
// SetPublic sets account to public mode.
//
// This function updates current Account information.
//
// See example: examples/account/setPublic.go
func (account *Account) SetPublic() error {
return account.changePublic(urlSetPublic)
}
func (account *Account) changePublic(endpoint string) error {
insta := account.insta
data, err := json.Marshal(
map[string]string{
"_uid": toString(insta.Account.ID),
"_uuid": insta.uuid,
})
if err != nil {
return err
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: endpoint,
IsPost: true,
Query: generateSignature(data),
},
)
if err == nil {
resp := profResp{}
err = json.Unmarshal(body, &resp)
if err == nil {
*account = resp.Account
account.insta = insta
}
}
return err
}
// Followers returns a list of user followers.
//
// Query can be used to search for a specific user.
// Be aware that it only matches from the start, e.g.
// "theprimeagen" will only match "theprime" not "prime".
// To fetch all user an empty string "".
//
// Users.Next can be used to paginate
func (account *Account) Followers(query string) *Users {
user := &User{
insta: account.insta,
ID: account.ID,
}
return user.Followers(query)
}
// Following returns a list of user following.
//
// Query can be used to search for a specific user.
// Be aware that it only matches from the start, e.g.
// "theprimeagen" will only match "theprime" not "prime".
// To fetch all user an empty string "".
//
// Users.Next can be used to paginate
func (account *Account) Following(query string, order FollowOrder) *Users {
user := &User{
insta: account.insta,
ID: account.ID,
}
return user.Following(query, order)
}
// Feed returns current account feed
//
// params can be:
// string: timestamp of the minimum media timestamp.
//
// minTime is the minimum timestamp of media.
//
// For pagination use FeedMedia.Next()
func (account *Account) Feed(params ...interface{}) *FeedMedia {
insta := account.insta
media := &FeedMedia{
insta: insta,
endpoint: urlUserFeed,
uid: account.ID,
}
for _, param := range params {
switch s := param.(type) {
case string:
media.timestamp = s
}
}
return media
}
// Stories returns account stories.
func (account *Account) Stories() (*StoryMedia, error) {
return account.insta.fetchStories(account.ID)
}
// Tags returns media where account is tagged in
//
// For pagination use FeedMedia.Next()
func (account *Account) Tags(minTimestamp []byte) (*FeedMedia, error) {
insta := account.insta
timestamp := string(minTimestamp)
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: fmt.Sprintf(urlUserTags, account.ID),
Query: map[string]string{
"max_id": "",
"rank_token": account.insta.rankToken,
"min_timestamp": timestamp,
"ranked_content": "true",
},
},
)
if err != nil {
return nil, err
}
media := &FeedMedia{
insta: insta,
endpoint: urlUserTags,
uid: account.ID,
}
err = json.Unmarshal(body, media)
return media, err
}
// Saved returns saved media.
// To get all the media you have to
// use the Next() method.
func (account *Account) Saved() *SavedMedia {
return &SavedMedia{
insta: account.insta,
endpoint: urlFeedSavedPosts,
}
}
// UpdateProfile method allows you to update your current account information.
// :param: form takes a map[string]string, the common values are:
//
// external_url
// phone_number
// username
// first_name -- is actually your full name
// biography
// email
//
func (account *Account) UpdateProfile(form map[string]string) error {
insta := account.insta
query := map[string]string{
"external_url": "",
"phone_number": "",
"username": insta.Account.Username,
"first_name": insta.Account.FullName,
"_uid": toString(insta.Account.ID),
"device_id": insta.dID,
"biography": insta.Account.Biography,
"_uuid": insta.uuid,
"email": insta.Account.Email,
}
for k, v := range form {
query[k] = v
}
data, err := json.Marshal(query)
if err != nil {
return err
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlEditProfile,
IsPost: true,
Query: generateSignature(data),
},
)
if err != nil {
return err
}
resp := struct {
Status string `json:"status"`
User *Account `json:"user"`
}{
User: insta.Account,
}
err = json.Unmarshal(body, &resp)
if err != nil {
return err
}
if resp.Status != "ok" {
return fmt.Errorf("Can't update profile")
}
insta.Account = resp.User
return nil
}
// EditBiography changes your Instagram's biography.
//
// This function updates current Account information.
func (account *Account) EditBiography(bio string) error {
return account.UpdateProfile(map[string]string{"biography": bio})
}
// EditName changes your Instagram account name.
//
// This function updates current Account information.
func (account *Account) EditName(name string) error {
return account.UpdateProfile(map[string]string{"first_name": name})
}
// EditUrl changes your Instagram account url.
//
// This function updates current Account information.
func (account *Account) EditUrl(url string) error {
return account.UpdateProfile(map[string]string{"external_url": url})
}
// Liked are liked publications
func (account *Account) Liked() *FeedMedia {
insta := account.insta
media := &FeedMedia{
insta: insta,
endpoint: urlFeedLiked,
}
return media
}
// PendingFollowRequests returns pending follow requests.
func (account *Account) PendingFollowRequests() (*PendingRequests, error) {
insta := account.insta
resp, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlFriendshipPending,
},
)
if err != nil {
return nil, err
}
var result PendingRequests
err = json.Unmarshal(resp, &result)
if err != nil {
return nil, err
}
if result.Status != "ok" {
return nil, fmt.Errorf("bad status: %s", result.Status)
}
var users []string
for _, u := range result.Users {
u.insta = insta
users = append(users, toString(u.ID))
}
friendships, err := account.FriendhipsShowMany(users)
if err != nil {
return nil, err
}
for _, u := range result.Users {
if val, ok := friendships[toString(u.ID)]; ok {
u.Friendship = val
}
}
return &result, nil
}
// PendingRequestCount returns the number of open pending friendships as int
func (account *Account) PendingRequestCount() (int, error) {
insta := account.insta
resp, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlFriendshipPendingCount,
},
)
if err != nil {
return 0, err
}
var result struct {
Count int `json:"count"`
Status string `json:"status"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return 0, err
}
if result.Status != "ok" {
return 0, fmt.Errorf("bad status: %s", result.Status)
}
return result.Count, nil
}
func (account *Account) FriendhipsShowMany(userIds []string) (map[string]Friendship, error) {
insta := account.insta
resp, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlFriendshipShowMany,
IsPost: true,
Query: map[string]string{
"user_ids": strings.Join(userIds, ","),
"_uuid": insta.uuid,
},
},
)
if err != nil {
return nil, err
}
var result struct {
Friendships map[string]Friendship `json:"friendship_statuses"`
Status string `json:"status"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return nil, err
}
if result.Status != "ok" {
return nil, fmt.Errorf("bad status: %s", result.Status)
}
return result.Friendships, nil
}
// Archived returns current account archive feed
//
// For pagination use FeedMedia.Next()
func (account *Account) Archived(params ...interface{}) *FeedMedia {
insta := account.insta
media := &FeedMedia{
insta: insta,
endpoint: urlUserArchived,
}
for _, param := range params {
switch s := param.(type) {
case string:
media.timestamp = s
}
}
return media
}