-
Notifications
You must be signed in to change notification settings - Fork 2
/
relationships.go
275 lines (235 loc) Β· 6.97 KB
/
relationships.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
package main
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"time"
"github.com/ahmdrz/goinsta/response"
"github.com/borteo/ermes/config"
humanize "github.com/dustin/go-humanize"
)
// getFollowing gets users that we follow.
func (a *App) getFollowings() error {
fmt.Println("Collecting your 'Followings' list...")
resp, err := a.api.SelfTotalUserFollowing()
if err != nil {
return err
}
for _, user := range resp.Users {
username := strings.ToLower(user.Username)
a.followings[username] = true
a.db2.Write("followings", username, InstagramUser{
ID: user.ID,
Username: username,
IsPrivate: user.IsPrivate,
IsLiked: false,
IsChecked: true,
IsGood: !user.IsPrivate,
})
}
return nil
}
// getFollowers gets users that follow us.
func (a *App) getFollowers() error {
log.Println("Collecting your 'Followers' list...")
// Resp data
// Username: string
// HasAnonymousProfilePicture: bool
// ProfilePictureID: int
// ProfilePictureURL: URL
// FullName: string
// ID: int
// IsVerified: bool
// IsPrivate: bool
// IsFavorite: bool
// IsUnpublished: bool
resp, err := a.api.SelfTotalUserFollowers()
if err != nil {
return err
}
for _, user := range resp.Users {
username := strings.ToLower(user.Username)
uptUser := InstagramUser{
ID: user.ID,
Username: username,
IsPrivate: user.IsPrivate,
IsLiked: false,
IsGood: true,
IsChecked: true,
}
a.followers[username] = true
a.db2.Write("followers", username, uptUser)
}
return nil
}
// Get passed user.ID followers.
// @param isLimited: if true fetch only the amount of pages defined
func (a *App) fetchUserFollowers(vipID int64, isLimited bool) (response.UsersResponse, error) {
usersLimit := 1500
resp := response.UsersResponse{}
for {
tempResp, err := a.api.UserFollowing(vipID, resp.NextMaxID)
if err != nil {
return response.UsersResponse{}, err
}
resp.Users = append(resp.Users, tempResp.Users...)
resp.PageSize += tempResp.PageSize
if !tempResp.BigList || (isLimited && len(resp.Users) >= usersLimit) {
return resp, nil
}
resp.NextMaxID = tempResp.NextMaxID
resp.Status = tempResp.Status
}
}
func (a *App) getUserFollowers(vip *InstagramUser, isLimited bool) error {
fmt.Printf("Collecting %s's followers πΆ \n\n", vip.Username)
collection := "user_followers_" + vip.Username
resp, err := a.api.TotalUserFollowing(vip.ID)
// Use it to limit the number of pages requested:
// resp, err := a.fetchUserFollowers(vip.ID, isLimited)
if err != nil {
return err
}
// --- resp structure ---
// BigList: false
// PageSize: 200
// Users: []
// log.Printf("There are %v \n", len(resp.Users))
// log.Printf("There are %+v \n", resp)
for _, user := range resp.Users {
// --- user structure ---
// Username
// HasAnonymousProfilePicture: false
// ProfilePictureID: 1500417914300789823_408446133
// ProfilePictureURL
// FullName:
// ID: number
// IsVerified: false
// IsPrivate: false
// IsFavorite: false
// IsUnpublished: false
// log.Printf("USER data %+v \n", user)
username := strings.ToLower(user.Username)
currentUser := &InstagramUser{
ID: user.ID,
Username: username,
IsPrivate: user.IsPrivate,
IsLiked: false,
IsChecked: false,
IsGood: !user.IsPrivate,
}
a.db2.Write(collection, username, currentUser)
}
return nil
}
// check if user's followers are good to follow
func (a *App) checkUserFollowers(username string) {
limit := 2000
collection := "user_followers_" + username
results, _ := a.db2.ReadAll(collection)
data := []InstagramUser{}
for _, user := range results {
iu := InstagramUser{}
json.Unmarshal([]byte(user), &iu)
if iu.IsPrivate == false && iu.IsChecked == false {
data = append(data, iu)
}
}
totalUsersLen := len(data)
// Don't analyze more than 'limit' amount of users
// Why? it requires 3 hours to analyze 1k users.
// Ideally we could analyze 1k, then process the outcome and restart the loop
if len(data) > limit {
data = data[0:limit]
}
fmt.Printf("π There are %d followers to check; %d more available \n", len(data), totalUsersLen-len(data))
var delaySecs time.Duration = time.Duration(config.WAITING_TIME*len(data)) * time.Second
fmt.Printf("β± %s \n\n", humanize.Time(time.Now().Add(delaySecs)))
counter := 0
for _, follower := range data {
counter++
fID := follower.ID
fUsername := follower.Username
resp, err := a.api.GetUserByID(fID)
if err != nil {
log.Printf("Got error checking %s. Moving on...", fUsername)
continue
}
// A very simple way to narrow down people that won't follow you back.
// People w/ tons of followers and a few followings don't give a shit about you.
isGood := resp.User.FollowingCount > resp.User.FollowerCount
log.Printf("[%v/%v][%s] %d followings, %d followers, is good? %t", counter, len(data), fUsername, resp.User.FollowingCount, resp.User.FollowerCount, isGood)
uptUser := InstagramUser{
ID: follower.ID,
Username: follower.Username,
IsPrivate: follower.IsPrivate,
IsLiked: false,
IsGood: isGood,
IsChecked: true,
}
if err := a.db2.Write(collection, follower.Username, uptUser); err != nil {
fmt.Printf("Error while setting isLiked at true, %s", err)
}
time.Sleep(time.Duration(config.WAITING_TIME) * time.Second)
}
}
// unfollow leeches -- TODO pass array to unfollow
func (a *App) unfollowLeeches() {
var (
counter = 1
remaining = len(a.leeches)
)
if remaining == 0 {
fmt.Printf("Congrats! You have no ingrates\n")
return
}
fmt.Printf("\n π π π unfollow π π π \n\n")
for _, username := range a.leeches {
if _, ok := a.followings[username]; !ok {
fmt.Printf("[ERROR] Username %s not found in following map\n", username)
continue
}
// Unfollow.
userIDStr := a.getUserId(username)
randomInt := random(config.WAITING_TIME)
log.Printf("- [%d of %d]: %s (UID %s) β° %ds\n", counter, remaining, username, userIDStr, randomInt)
// Convert the user ID from a string to an int.
userID, err := strconv.Atoi(userIDStr)
if err != nil {
panic(err)
}
_, err = a.api.UnFollow(int64(userID))
if err != nil {
log.Panicf("Got error when unfollowing %s: %s", username, err)
}
counter++
time.Sleep(time.Duration(randomInt) * time.Second)
}
}
func (a *App) showList() {
// Sort the lists
followings := a.sortKeys(a.followings)
followers := a.sortKeys(a.followers)
leeches := a.leeches
// Sum up the numbers.
var (
numFollowings = len(followings)
numFollowers = len(followers)
numLeeches = len(leeches)
)
fmt.Printf("You've got %d followings, %d followers and leeches %d\n", numFollowings, numFollowers, numLeeches)
for i := 0; i < len(leeches); i++ {
fmt.Printf("- %s \n", leeches[i])
}
}
func (a *App) follow() {
// ID := ???
// // TODO add a comment?
// respFollow, errFollow := a.api.Follow(ID)
// if errFollow != nil {
// log.Panicf("Got error when Following : %s", errFollow)
// }
// log.Printf("Started to follow %s - response: %v ", f.Username, respFollow)
}