This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 161
/
insta.go
357 lines (305 loc) · 8.69 KB
/
insta.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package main
import (
"bufio"
"errors"
"fmt"
"log"
"math/rand"
"os"
"reflect"
"strings"
"time"
"unsafe"
"github.com/Davincible/goinsta/v3"
"github.com/spf13/viper"
)
// Storing user in session
var checkedUser = make(map[string]bool)
var insta *goinsta.Instagram
// login will try to reload a previous session, and will create a new one if it can't
func login() {
err := reloadSession()
if err != nil {
createAndSaveSession()
}
}
// reloadSession will attempt to recover a previous session
func reloadSession() error {
insta, err := goinsta.Import("./goinsta-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
}
// Logins and saves the session
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 = instabot.Insta.Export("./goinsta-session")
check(err)
log.Println("Created and saved the session")
}
func getInput(text string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Printf(text)
input, err := reader.ReadString('\n')
check(err)
return strings.TrimSpace(input)
}
// Checks if the user is in the slice
func containsUser(slice []goinsta.User, user goinsta.User) bool {
for _, currentUser := range slice {
if currentUser.Username == user.Username {
return true
}
}
return false
}
func getInputf(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)
}
// Same, with strings
func containsString(slice []string, user string) bool {
for _, currentUser := range slice {
if currentUser == user {
return true
}
}
return false
}
func (myInstabot MyInstabot) syncFollowers() {
following := myInstabot.Insta.Account.Following("", goinsta.DefaultOrder)
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 {
// Skip whitelisted users.
if containsString(userWhitelist, user.Username) {
continue
}
if !containsUser(followerUsers, user) {
users = append(users, user)
}
}
if len(users) == 0 {
return
}
fmt.Printf("\n%d users are not following you back!\n", len(users))
answer := getInput("Do you want to review these users ? [yN]")
if answer != "y" {
fmt.Println("Not unfollowing.")
os.Exit(0)
}
answerUnfollowAll := getInput("Unfollow everyone ? [yN]")
for _, user := range users {
if answerUnfollowAll != "y" {
answerUserUnfollow := getInputf("Unfollow %s ? [yN]", user.Username)
if answerUserUnfollow != "y" {
userWhitelist = append(userWhitelist, user.Username)
continue
}
}
userBlacklist = append(userBlacklist, user.Username)
if !dev {
user.Unfollow()
}
time.Sleep(6 * time.Second)
}
}
// Follows a user, if not following already
func (myInstabot MyInstabot) followUser(user *goinsta.User) {
log.Printf("Following %s\n", user.Username)
// If not following already
if !user.Friendship.Following {
if !dev {
user.Follow()
}
log.Println("Followed")
numFollowed++
report[line{tag, "follow"}]++
} else {
log.Println("Already following " + user.Username)
}
}
func (myInstabot MyInstabot) loopTags() {
for tag = range tagsList {
limitsConf := viper.GetStringMap("tags." + tag)
// Some converting
limits = map[string]int{
"follow": int(limitsConf["follow"].(float64)),
"like": int(limitsConf["like"].(float64)),
"comment": int(limitsConf["comment"].(float64)),
}
// What we did so far
numFollowed = 0
numLiked = 0
numCommented = 0
myInstabot.browse()
}
buildReport()
}
// Likes an image, if not liked already
func (myInstabot MyInstabot) likeImage(image goinsta.Item) {
log.Println("Liking the picture")
if !image.HasLiked {
if !dev {
image.Like()
}
log.Println("Liked")
numLiked++
report[line{tag, "like"}]++
} else {
log.Println("Image already liked")
}
}
func (myInstabot MyInstabot) 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++
// Getting all the pictures we can on the first page
// Instagram will return a 500 sometimes, so we will retry 10 times.
// Check retry() for more info.
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("Currently not enough images for this tag to achieve goals")
break
}
}
}
// Goes through all the images for a certain tag
func (myInstabot MyInstabot) goThrough(images *goinsta.FeedTag) {
var i = 0
// do for other too
for _, image := range images.Items {
// Exiting the loop if there is nothing left to do
if numFollowed >= limits["follow"] && numLiked >= limits["like"] && numCommented >= limits["comment"] {
break
}
// Skip our own images
if image.User.Username == viper.GetString("user.instagram.username") {
continue
}
// Check if we should fetch new images for tag
if i >= limits["follow"] && i >= limits["like"] && i >= limits["comment"] {
break
}
// Skip checked user if the flag is turned on
if checkedUser[image.User.Username] && noduplicate {
continue
}
// Getting the user info
// Instagram will return a 500 sometimes, so we will retry 10 times.
// Check retry() for more info.
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++
// Will only follow and comment if we like the picture
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
// Checking if we are already following current user and skipping if we do
skip := false
following := myInstabot.Insta.Account.Following("", goinsta.DefaultOrder)
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
}
}
// Like, then comment/follow
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)
// This is to avoid the temporary ban by Instagram
time.Sleep(20 * time.Second)
}
}
// Comments an image
func (myInstabot MyInstabot) commentImage(image goinsta.Item) {
rand.Seed(time.Now().Unix())
text := commentsList[rand.Intn(len(commentsList))]
if !dev {
comments := image.Comments
if comments == nil {
// monkey patching
// we need to do that because https://github.com/ahmdrz/goinsta/pull/299 is not in goinsta/v2
// I know, it's ugly
newComments := goinsta.Comments{}
rs := reflect.ValueOf(&newComments).Elem()
rf := rs.FieldByName("item")
rf = reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem()
item := reflect.New(reflect.TypeOf(image))
item.Elem().Set(reflect.ValueOf(image))
rf.Set(item)
newComments.Add(text)
// end hack!
} else {
comments.Add(text)
}
}
log.Println("Commented " + text)
numCommented++
report[line{tag, "comment"}]++
}
func (myInstabot MyInstabot) 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")
}