forked from ahmdrz/goinsta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts.go
104 lines (90 loc) · 2.4 KB
/
contacts.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
package goinsta
import (
"encoding/json"
"strconv"
)
type Contacts struct {
inst *Instagram
}
type Contact struct {
Numbers []string `json:"phone_numbers"`
Emails []string `json:"email_addresses"`
Name string `json:"first_name"`
}
type SyncAnswer struct {
Users []struct {
Pk int64 `json:"pk"`
Username string `json:"username"`
FullName string `json:"full_name"`
IsPrivate bool `json:"is_private"`
ProfilePicURL string `json:"profile_pic_url"`
ProfilePicID string `json:"profile_pic_id"`
IsVerified bool `json:"is_verified"`
HasAnonymousProfilePicture bool `json:"has_anonymous_profile_picture"`
ReelAutoArchive string `json:"reel_auto_archive"`
AddressbookName string `json:"addressbook_name"`
} `json:"users"`
Warning string `json:"warning"`
Status string `json:"status"`
}
func newContacts(inst *Instagram) *Contacts {
return &Contacts{inst: inst}
}
func (c *Contacts) SyncContacts(contacts *[]Contact) (*SyncAnswer, error) {
acquireContacts := &reqOptions{
Endpoint: "address_book/acquire_owner_contacts/",
IsPost: true,
Login: true,
UseV2: false,
Query: map[string]string{
"phone_id": c.inst.pid,
"me": `{"phone_numbers":[],"email_addresses":[]}`,
},
}
body, err := c.inst.sendRequest(acquireContacts)
if err != nil {
return nil, err
}
byteContacts, err := json.Marshal(contacts)
if err != nil {
return nil, err
}
syncContacts := &reqOptions{
Endpoint: `address_book/link/`,
IsPost: true,
Login: true,
UseV2: false,
Query: map[string]string{
"_uuid": c.inst.uuid,
"_csrftoken": c.inst.token,
"contacts": string(byteContacts),
},
}
body, err = c.inst.sendRequest(syncContacts)
if err != nil {
return nil, err
}
answ := &SyncAnswer{}
json.Unmarshal(body, answ)
return answ, nil
}
func (c *Contacts) UnlinkContacts() error {
toSign := map[string]string{
"_csrftoken": c.inst.token,
"_uid": strconv.Itoa(int(c.inst.Account.ID)),
"_uuid": c.inst.uuid,
}
bytesS, _ := json.Marshal(toSign)
unlinkBody := &reqOptions{
Endpoint: "address_book/unlink/",
IsPost: true,
Login: true,
UseV2: false,
Query: generateSignature(string(bytesS)),
}
_, err := c.inst.sendRequest(unlinkBody)
if err != nil {
return err
}
return nil
}