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
/
account.go
442 lines (406 loc) · 11.3 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
package goinsta
import (
"encoding/json"
"fmt"
)
type accountResp struct {
Status string `json:"status"`
Account Account `json:"logged_in_user"`
}
// Account is personal account object
//
// See examples: examples/account/*
type Account struct {
inst *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.inst
data, err := insta.prepareData()
if err != nil {
return err
}
body, err := insta.sendRequest(&reqOptions{
Endpoint: urlCurrentUser,
Query: generateSignature(data),
IsPost: true,
})
if err == nil {
resp := profResp{}
err = json.Unmarshal(body, &resp)
if err == nil {
*account = resp.Account
account.inst = insta
}
}
return err
}
// 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.inst
data, err := insta.prepareData(
map[string]interface{}{
"old_password": old,
"new_password1": new,
"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.inst
data, err := insta.prepareData()
if err != nil {
return err
}
body, err := insta.sendRequest(
&reqOptions{
Endpoint: urlRemoveProfPic,
Query: generateSignature(data),
IsPost: true,
},
)
if err == nil {
resp := profResp{}
err = json.Unmarshal(body, &resp)
if err == nil {
*account = resp.Account
account.inst = insta
}
}
return err
}
// SetPrivate sets account to private mode.
//
// This function updates current Account information.
//
// See example: examples/account/setPrivate.go
func (account *Account) SetPrivate() error {
insta := account.inst
data, err := insta.prepareData()
if err != nil {
return err
}
body, err := insta.sendRequest(
&reqOptions{
Endpoint: urlSetPrivate,
Query: generateSignature(data),
IsPost: true,
},
)
if err == nil {
resp := profResp{}
err = json.Unmarshal(body, &resp)
if err == nil {
*account = resp.Account
account.inst = insta
}
}
return err
}
// SetPublic sets account to public mode.
//
// This function updates current Account information.
//
// See example: examples/account/setPublic.go
func (account *Account) SetPublic() error {
insta := account.inst
data, err := insta.prepareData()
if err != nil {
return err
}
body, err := insta.sendRequest(
&reqOptions{
Endpoint: urlSetPublic,
Query: generateSignature(data),
IsPost: true,
},
)
if err == nil {
resp := profResp{}
err = json.Unmarshal(body, &resp)
if err == nil {
*account = resp.Account
account.inst = insta
}
}
return err
}
// Followers returns a list of user followers.
//
// Users.Next can be used to paginate
//
// See example: examples/account/followers.go
func (account *Account) Followers() *Users {
endpoint := fmt.Sprintf(urlFollowers, account.ID)
users := &Users{}
users.inst = account.inst
users.endpoint = endpoint
return users
}
// Following returns a list of user following.
//
// Users.Next can be used to paginate
//
// See example: examples/account/following.go
func (account *Account) Following() *Users {
endpoint := fmt.Sprintf(urlFollowing, account.ID)
users := &Users{}
users.inst = account.inst
users.endpoint = endpoint
return users
}
// 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.inst
media := &FeedMedia{}
media.inst = insta
media.endpoint = urlUserFeed
media.uid = account.ID
for _, param := range params {
switch s := param.(type) {
case string:
media.timestamp = s
}
}
return media
}
// Stories returns account stories.
//
// Use StoryMedia.Next for pagination.
//
// See example: examples/account/stories.go
func (account *Account) Stories() *StoryMedia {
media := &StoryMedia{}
media.uid = account.ID
media.inst = account.inst
media.endpoint = urlUserStories
return media
}
// Tags returns media where account is tagged in
//
// For pagination use FeedMedia.Next()
func (account *Account) Tags(minTimestamp []byte) (*FeedMedia, error) {
timestamp := b2s(minTimestamp)
body, err := account.inst.sendRequest(
&reqOptions{
Endpoint: fmt.Sprintf(urlUserTags, account.ID),
Query: map[string]string{
"max_id": "",
"rank_token": account.inst.rankToken,
"min_timestamp": timestamp,
"ranked_content": "true",
},
},
)
if err != nil {
return nil, err
}
media := &FeedMedia{}
err = json.Unmarshal(body, media)
media.inst = account.inst
media.endpoint = urlUserTags
media.uid = account.ID
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{
inst: account.inst,
endpoint: urlFeedSaved,
err: nil,
}
}
type editResp struct {
Status string `json:"status"`
Account Account `json:"user"`
}
func (account *Account) edit() {
insta := account.inst
acResp := editResp{}
body, err := insta.sendRequest(
&reqOptions{
Endpoint: urlCurrentUser,
Query: map[string]string{
"edit": "true",
},
},
)
if err == nil {
err = json.Unmarshal(body, &acResp)
if err == nil {
acResp.Account.inst = insta
*account = acResp.Account
}
}
}
// SetBiography changes your Instagram's biography.
//
// This function updates current Account information.
func (account *Account) SetBiography(bio string) error {
account.edit() // preparing to edit
insta := account.inst
data, err := insta.prepareData(
map[string]interface{}{
"raw_text": bio,
},
)
if err != nil {
return err
}
body, err := insta.sendRequest(
&reqOptions{
Endpoint: urlSetBiography,
Query: generateSignature(data),
IsPost: true,
},
)
if err == nil {
var resp struct {
User struct {
Pk int64 `json:"pk"`
Biography string `json:"biography"`
} `json:"user"`
Status string `json:"status"`
}
err = json.Unmarshal(body, &resp)
if err == nil {
account.Biography = resp.User.Biography
}
}
return err
}
// Liked are liked publications
func (account *Account) Liked() *FeedMedia {
insta := account.inst
media := &FeedMedia{}
media.inst = insta
media.endpoint = urlFeedLiked
return media
}
// PendingFollowRequests returns pending follow requests.
func (account *Account) PendingFollowRequests() ([]User, error) {
insta := account.inst
resp, err := insta.sendRequest(
&reqOptions{
Endpoint: urlFriendshipPending,
},
)
if err != nil {
return nil, err
}
var result struct {
Users []User `json:"users"`
// TODO: pagination
// TODO: SuggestedUsers
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.Users, nil
}
// Archived returns current account archive feed
//
// For pagination use FeedMedia.Next()
func (account *Account) Archived(params ...interface{}) *FeedMedia {
insta := account.inst
media := &FeedMedia{}
media.inst = insta
media.endpoint = urlUserArchived
for _, param := range params {
switch s := param.(type) {
case string:
media.timestamp = s
}
}
return media
}