-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
151 lines (116 loc) · 3.09 KB
/
utils.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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/spf13/viper"
)
var (
nomail bool
run bool
unfollow bool
)
var likeLowerLimit int
var likeUpperLimit int
var followLowerLimit int
var followUpperLimit int
var commentLowerLimit int
var commentUpperLimit int
var tagsList map[string]interface{}
var limits map[string]int
var commentsList []string
type line struct {
Tag, Action string
}
var report map[line]int
var userBlacklist []string
var userWhitelist []string
var numFollowed int
var numCommented int
var numLiked int
var tag string
func check(err error) {
if err != nil {
log.Fatal("ERROR:", err)
}
}
func parseOptions() {
flag.BoolVar(&run, "run", false, "Use this option to follow, like and comment")
flag.BoolVar(&unfollow, "sync", false, "Use this option to unfollow those who are not following back")
flag.Parse()
}
func getConfig() {
viper.SetConfigFile("./config.json")
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
}
viper.SetEnvPrefix("instabot")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()
log.Printf("Using config: %s\n\n", viper.ConfigFileUsed())
likeLowerLimit = viper.GetInt("limits.like.min")
likeUpperLimit = viper.GetInt("limits.like.max")
followLowerLimit = viper.GetInt("limits.comment.min")
followUpperLimit = viper.GetInt("limits.comment.max")
commentLowerLimit = viper.GetInt("limits.comment.min")
commentUpperLimit = viper.GetInt("limits.comment.max")
tagsList = viper.GetStringMap("tags")
commentsList = viper.GetStringSlice("comments")
userBlacklist = viper.GetStringSlice("blacklist")
userWhitelist = viper.GetStringSlice("whitelist")
report = make(map[line]int)
}
func retry(maxAttempts int, sleep time.Duration, function func() error) (err error) {
for currentAttempt := 0; currentAttempt < maxAttempts; currentAttempt++ {
err = function()
if err == nil {
return
}
for i := 0; i <= currentAttempt; i++ {
time.Sleep(sleep)
}
log.Println("Retrying after error:", err)
}
return fmt.Errorf("After %d attempts, last error: %s", maxAttempts, err)
}
func buildLine() {
reportTag := ""
for index, element := range report {
if index.Tag == tag {
reportTag += fmt.Sprintf("%s %d/%d - ", index.Action, element, limits[index.Action])
}
}
if reportTag != "" {
log.Println(strings.TrimSuffix(reportTag, " - "))
}
}
func buildReport() {
reportAsString := ""
for index, element := range report {
var times string
if element == 1 {
times = "time"
} else {
times = "times"
}
if index.Action == "like" {
reportAsString += fmt.Sprintf("#%s has been liked %d %s \n", index.Tag, element, times)
} else {
reportAsString += fmt.Sprintf("#%s has been %sed %d %s\n", index.Tag, index.Action, element, times)
}
}
filename := "report.txt"
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(reportAsString); err != nil {
panic(err)
}
fmt.Println("Report has been added")
}