-
Notifications
You must be signed in to change notification settings - Fork 14
/
decision.go
194 lines (165 loc) · 5.71 KB
/
decision.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
package bot
import(
"bot/session"
"bot/utils"
"bot/http"
"strings"
"math"
)
func BasicDecision(s *session.Session, follows int, likes int, intervals int, done chan bool){
// Round robin the hashtags. Allows for manual weighting eg: [#dog,#dog,#cute]
posts := http.GetPosts(s,s.GetHashtag(intervals))
// Go from end to reduce collision
// Doesn't bother checking
i := 19
for (likes > 0 || follows > 0) && i >= 0 {
// Process likes
if likes > 0 {
go http.LikePosts(s, posts.Data[i].Id)
likes--
// Doing this seperately reaches larger audience
// Never exceeds 12/11 at a given time
}else if follows > 0 {
go http.FollowUser(s, posts.Data[i].Id)
follows--
}
// Decrement
i--
}
// Indicate doneness
done <- true
}
func IntelligentDecision(s *session.Session, follows int, likes int, intervals int, done chan bool) {
// Still do round robin, but this time the hashtags are smart
// and our choice is educated
posts := http.GetPosts(s,s.GetHashtag(intervals))
// Set up channels for async download/ processing from instagram
next := make(chan *http.Posts)
grp := make(chan *group)
count := 0
calls := 0
go sort(s, grp, follows, likes, &calls, &count, done)
go listen(s, grp, next, &calls, &count)
next <- &posts
}
// Async heapsort, hope it works
func sort(s *session.Session, next chan *group, follows, likes int, calls, total *int, done chan bool) {
var instances []group
count := 0
x := 0
min := math.Inf(1)
for {
select {
case instance := <-next:
x++
// Catches up and thus done
if x == *total && *calls == utils.MAXPOSTGRAB {
i := 0
for (likes > 0 || follows > 0){
// Highest value for follows then do likes
if follows > 0 {
go http.FollowUser(s, instances[i].id)
follows--
}else if likes > 0 {
go http.LikePosts(s, instances[i].id)
likes--
}
i++
}
s.FlushCache()
done <- true
close(next)
return
}
// We already have our fill and this value won't contribute
if instance.id == "continue" || (instance.value <= min && count == follows + likes) {
continue
}
if min < instance.value {
if count == follows + likes {
min = instance.value
}
} else {
if count < follows + likes {
min = instance.value
}
}
if count < follows + likes {
instances = append(instances, *instance)
count += 1
} else {
// Replace end
instances[count - 1] = *instance
}
// Bubble sort
for i := count - 2; i >= 0; i-- {
if instance.value > instances[i].value {
holder := instances[i]
instances[i] = *instance
instances[i + 1] = holder
} else {
break
}
}
}
}
}
type group struct {
value float64
id string
user string
}
// Async set up multi calls
func listen(s *session.Session, grp chan *group, next chan *http.Posts, calls, count *int) {
for {
select {
case posts := <-next:
i := len(posts.Data) - 1
*count += len(posts.Data)
go process(s, posts, i, grp)
close(next)
if *calls == utils.MAXPOSTGRAB || posts.Pagination.Next_url == "" {
return
}
var batch http.Posts
nxt := make(chan *http.Posts)
batch = http.GetNextPost(s, posts.Pagination.Next_url)
// Recursion with channels is weird,
// best to just kill this and start another
*calls += 1
go listen(s, grp, nxt, calls, count)
nxt <- &batch
return
}
}
}
func process(s *session.Session, posts *http.Posts, i int, grp chan *group){
for i >= 0 {
// I don't really have to parse here, forgot kosher implementation.
// See http/json
id := strings.Split(posts.Data[i].Id,"_")[1]
if posts.Data[i].User_has_liked || s.CheckCache(id) || http.IsFollowing(s, id){
// Try to add to channel and stop if done
grp <- &group{
id:"continue",
}
i--
continue
}
//
user := http.GetUser(s, id)
// Create perosn to get value
person := session.Person{
Followers: float64(user.Data.Counts.Follows),
Following: float64(user.Data.Counts.Followed_by),
Posts: float64(user.Data.Counts.Media),
}
// Forget sigmoid for now
grp <- &group{
id:posts.Data[i].Id,
value: person.Followers/person.Following, // person.Sigmoid(session.GetTheta()) would be the ideal way
user: posts.Data[i].User.Username,
}
i--
}
}