-
Notifications
You must be signed in to change notification settings - Fork 0
/
instafollow.go
65 lines (49 loc) · 1.29 KB
/
instafollow.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
package instafollow
import (
"encoding/json"
"errors"
"strings"
"github.com/dekoch/gouniversal/shared/api/instaclient"
"github.com/dekoch/gouniversal/shared/api/instafollow/query"
)
type response struct {
Result string `json:"result"`
Message string `json:"message"`
Status string `json:"status"`
}
func Follow(id, username, xinstagramajax string, ic *instaclient.InstaClient) error {
return send(id, username, xinstagramajax, true, ic)
}
func Unfollow(id, username, xinstagramajax string, ic *instaclient.InstaClient) error {
return send(id, username, xinstagramajax, false, ic)
}
func send(id, username, xinstagramajax string, follow bool, ic *instaclient.InstaClient) error {
var (
err error
b []byte
resp response
)
for i := 0; i <= 2; i++ {
switch i {
case 0:
b, err = query.Send(id, username, xinstagramajax, follow, ic)
case 1:
err = json.Unmarshal(b, &resp)
case 2:
if resp.Status != "ok" {
// if response json contains empty message tag, user is already unfollowed
if strings.Contains(string(b), "\"message\":") {
if resp.Message != "" {
err = errors.New(resp.Status)
}
} else {
err = errors.New(resp.Status)
}
}
}
if err != nil {
return errors.New(err.Error() + " \"" + string(b) + "\"")
}
}
return nil
}