-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
291 lines (237 loc) · 6.29 KB
/
controller.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
package main
import (
"bufio"
"errors"
"fmt"
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/Davincible/goinsta"
"github.com/spf13/viper"
)
var checkedUser = make(map[string]bool)
var insta *goinsta.Instagram
func login() {
err := reloadSession()
if err != nil {
createAndSaveSession()
}
}
func reloadSession() error {
insta, err := goinsta.Import("./insta-session")
if err != nil {
return errors.New("Couldn't recover the session")
}
if insta != nil {
instabot.Insta = insta
}
log.Println("Successfully logged in")
return nil
}
func createAndSaveSession() {
insta := goinsta.New(
viper.GetString("user.instagram.username"),
viper.GetString("user.instagram.password"))
instabot.Insta = insta
err := instabot.Insta.Login()
check(err)
err = insta.Export("./insta-session")
check(err)
log.Println("Created and saved the session")
}
func (myInstabot Instabot) syncFollowers() {
following := myInstabot.Insta.Account.Following()
followers := myInstabot.Insta.Account.Followers()
var followerUsers []goinsta.User
var followingUsers []goinsta.User
for following.Next() {
for _, user := range following.Users {
followingUsers = append(followingUsers, *user)
}
}
for followers.Next() {
for _, user := range followers.Users {
followerUsers = append(followerUsers, *user)
}
}
var users []goinsta.User
for _, user := range followingUsers {
if containsString(userWhitelist, user.Username) {
continue
}
if !containsUser(followerUsers, user) {
users = append(users, user)
}
}
if len(users) == 0 {
return
}
for _, user := range users {
userBlacklist = append(userBlacklist, user.Username)
user.Unfollow()
log.Println(user.Username + " has been unfollowed")
time.Sleep(6 * time.Second)
}
}
func (myInstabot Instabot) loopTags() {
for tag = range tagsList {
limitsConf := viper.GetStringMap("tags." + tag)
limits = map[string]int{
"follow": int(limitsConf["follow"].(float64)),
"like": int(limitsConf["like"].(float64)),
"comment": int(limitsConf["comment"].(float64)),
}
numFollowed = 0
numLiked = 0
numCommented = 0
myInstabot.browse()
}
buildReport()
}
func (myInstabot Instabot) goThrough(images *goinsta.FeedTag) {
var i = 0
for _, image := range images.Items {
if numFollowed >= limits["follow"] && numLiked >= limits["like"] && numCommented >= limits["comment"] {
break
}
if image.User.Username == viper.GetString("user.instagram.username") {
continue
}
if i >= limits["follow"] && i >= limits["like"] && i >= limits["comment"] {
break
}
if checkedUser[image.User.Username] {
continue
}
var userInfo *goinsta.User
err := retry(10, 20*time.Second, func() (err error) {
userInfo, err = myInstabot.Insta.Profiles.ByName(image.User.Username)
return
})
check(err)
followerCount := userInfo.FollowerCount
buildLine()
checkedUser[userInfo.Username] = true
log.Println("Checking followers for " + userInfo.Username + " - for #" + tag)
log.Printf("%s has %d followers\n", userInfo.Username, followerCount)
i++
like := followerCount > likeLowerLimit && followerCount < likeUpperLimit && numLiked < limits["like"]
follow := followerCount > followLowerLimit && followerCount < followUpperLimit && numFollowed < limits["follow"] && like
comment := followerCount > commentLowerLimit && followerCount < commentUpperLimit && numCommented < limits["comment"] && like
skip := false
following := myInstabot.Insta.Account.Following()
var followingUsers []goinsta.User
for following.Next() {
for _, user := range following.Users {
followingUsers = append(followingUsers, *user)
}
}
for _, user := range followingUsers {
if user.Username == userInfo.Username {
skip = true
break
}
}
if !skip {
if like {
myInstabot.likeImage(*image)
if follow && !containsString(userBlacklist, userInfo.Username) {
myInstabot.followUser(userInfo)
}
if comment {
myInstabot.commentImage(*image)
}
}
}
log.Printf("%s done\n\n", userInfo.Username)
time.Sleep(20 * time.Second)
}
}
func (myInstabot Instabot) followUser(user *goinsta.User) {
log.Printf("Following %s\n", user.Username)
if !user.Friendship.Following {
user.Follow()
log.Println("Followed")
numFollowed++
report[line{tag, "follow"}]++
} else {
log.Println("Already following " + user.Username)
}
}
func (myInstabot Instabot) likeImage(image goinsta.Item) {
log.Println("Liking the picture")
if !image.HasLiked {
image.Like()
log.Println("Liked")
numLiked++
report[line{tag, "like"}]++
} else {
log.Println("Image already liked")
}
}
func (myInstabot Instabot) commentImage(image goinsta.Item) {
rand.Seed(time.Now().Unix())
text := commentsList[rand.Intn(len(commentsList))]
comments := image.Comments
if comments == nil {
image.Comment(text)
} else {
comments.Add(text)
}
log.Println("Commented " + text)
numCommented++
report[line{tag, "comment"}]++
}
func (myInstabot Instabot) browse() {
var i = 0
for numFollowed < limits["follow"] || numLiked < limits["like"] || numCommented < limits["comment"] {
log.Println("Fetching the list of images for #" + tag + "\n")
i++
var images *goinsta.FeedTag
err := retry(10, 20*time.Second, func() (err error) {
images, err = myInstabot.Insta.Feed.Tags(tag)
return
})
check(err)
myInstabot.goThrough(images)
if viper.IsSet("limits.maxRetry") && i > viper.GetInt("limits.maxRetry") {
log.Println("Currentrly not enough images for this tag to achieve goals")
break
}
}
}
func containsString(slice []string, user string) bool {
for _, currentUser := range slice {
if currentUser == user {
return true
}
}
return false
}
func containsUser(slice []goinsta.User, user goinsta.User) bool {
for _, currentUser := range slice {
if currentUser.Username == user.Username {
return true
}
}
return false
}
func getInput(format string, args ...interface{}) string {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(format, args...)
input, err := reader.ReadString('\n')
check(err)
return strings.TrimSpace(input)
}
func (myInstabot Instabot) updateConfig() {
viper.Set("whitelist", userWhitelist)
viper.Set("blacklist", userBlacklist)
err := viper.WriteConfig()
if err != nil {
log.Println("Update config file error", err)
return
}
log.Println("Config file updated")
}