-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccounts.go
137 lines (123 loc) · 3.7 KB
/
accounts.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
package instabot
import (
"context"
"github.com/winterssy/sreq"
)
const (
apiLogin = apiV1 + "accounts/login/"
apiTwoFactorLogin = apiV1 + "accounts/two_factor_login/"
apiLogout = apiV1 + "accounts/logout/"
apiSetAccountPrivate = apiV1 + "accounts/set_private/"
apiSetAccountPublic = apiV1 + "accounts/set_public/"
apiSetAccountBiography = apiV1 + "accounts/set_biography/"
apiSetAccountGender = apiV1 + "accounts/set_gender/"
)
func (bot *Bot) Login(ctx context.Context, interactive bool) (sreq.H, error) {
form := sreq.Form{
"username": bot.Username,
"password": bot.Password,
"_csrftoken": bot.GetCSRFToken(),
"guid": bot.uuid,
"phone_id": bot.phoneId,
"device_id": bot.deviceId,
"login_attempt_count": "0",
}
req, _ := sreq.NewRequest(sreq.MethodPost, apiLogin,
sreq.WithForm(GenerateSignedForm(form)),
sreq.WithContext(ctx),
)
data, err := bot.sendRequest(req)
if err != nil && data != nil {
if data.GetBool("two_factor_required") {
if interactive {
twoFactorId := data.GetH("two_factor_info").GetString("two_factor_identifier")
twoFactorCode := readUserInput("Enter 2FA verification code: ")
return bot.TwoFactorLogin(ctx, twoFactorId, twoFactorCode)
}
return data, ErrTwoFactorRequired
}
if data.GetString("error_type") == "checkpoint_challenge_required" {
return data, ErrChallengeRequired
}
}
return data, err
}
func (bot *Bot) TwoFactorLogin(ctx context.Context, twoFactorId string, twoFactorCode string) (sreq.H, error) {
form := sreq.Form{
"two_factor_identifier": twoFactorId,
"verification_code": twoFactorCode,
"username": bot.Username,
"password": bot.Password,
"device_id": bot.deviceId,
"ig_sig_key_version": igSigKeyVersion,
}
req, _ := sreq.NewRequest(sreq.MethodPost, apiTwoFactorLogin,
sreq.WithForm(GenerateSignedForm(form)),
sreq.WithContext(ctx),
)
return bot.sendRequest(req)
}
func (bot *Bot) Logout(ctx context.Context) (sreq.H, error) {
form := sreq.Form{
"_csrftoken": bot.GetCSRFToken(),
"guid": bot.uuid,
"_uuid": bot.uuid,
"phone_id": bot.phoneId,
"device_id": bot.deviceId,
}
req, _ := sreq.NewRequest(sreq.MethodPost, apiLogout,
sreq.WithForm(form),
sreq.WithContext(ctx),
)
return bot.sendRequest(req)
}
func (bot *Bot) SetAccountPrivate(ctx context.Context, undo bool) (sreq.H, error) {
form := sreq.Form{
"_csrftoken": bot.GetCSRFToken(),
"_uid": bot.uuid,
"_uuid": bot.uuid,
}
url := apiSetAccountPrivate
if undo {
url = apiSetAccountPublic
}
req, _ := sreq.NewRequest(sreq.MethodPost, url,
sreq.WithForm(GenerateSignedForm(form)),
sreq.WithContext(ctx),
)
return bot.sendRequest(req)
}
func (bot *Bot) SetAccountBiography(ctx context.Context, biography string) (sreq.H, error) {
form := sreq.Form{
"raw_text": biography,
"_csrftoken": bot.GetCSRFToken(),
"_uid": bot.uuid,
"_uuid": bot.uuid,
"device_id": bot.deviceId,
}
req, _ := sreq.NewRequest(sreq.MethodPost, apiSetAccountBiography,
sreq.WithForm(GenerateSignedForm(form)),
sreq.WithContext(ctx),
)
return bot.sendRequest(req)
}
func (bot *Bot) SetAccountGender(ctx context.Context, gender string) (sreq.H, error) {
form := sreq.Form{
"_csrftoken": bot.GetCSRFToken(),
"_uid": bot.uuid,
"_uuid": bot.uuid,
"device_id": bot.deviceId,
}
switch gender {
case "1", "2", "3":
form.Set("gender", gender)
default:
form.Set("gender", "4")
form.Set("custom_gender", gender)
}
req, _ := sreq.NewRequest(sreq.MethodPost, apiSetAccountGender,
sreq.WithForm(form),
sreq.WithContext(ctx),
)
return bot.sendRequest(req)
}