/
timeline.go
248 lines (207 loc) · 5.52 KB
/
timeline.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
package main
import (
"log"
"strconv"
"time"
"strings"
"github.com/acgshare/acgsh/db"
"github.com/acgshare/acgsh/rpc"
"github.com/acgshare/acgsh/search"
)
const sync_posts_number = 2000
type updatePostsInfo struct {
maxK int64
minK int64
lastK int64
}
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}
func syncTimeLine() {
// Update publishers
twisterUsers, err := rpc.GetFollowing(adminTwisterUsername)
if err != nil {
log.Println("Error: can not fetch following users for", adminTwisterUsername, "from Twister RPC server.", err)
}
log.Println(twisterUsers)
log.Println("Get", len(*twisterUsers), "following users for", adminTwisterUsername)
db.AddPublishersIfNotExist(twisterUsers)
// Get all publishers' username, max id, since id and latest id (k), from DB
publishers, err := db.GetPublishers()
if err != nil {
log.Println(err)
log.Printf("Error: syncTimeLine GetPublishers: %+v\n", publishers)
}
//log.Printf("%+v\n", publishers)
// Get posts from Twister for all publishers
var gup getUserPosts
for name, data := range publishers {
gup.addUser(name, data.Max, data.Since)
}
newPosts, err := rpc.GetPosts(sync_posts_number, gup.data)
//log.Printf("%+v\n", presult)
if err != nil {
log.Println(err)
log.Printf("Error: syncTimeLine GetPosts: %+v\n", gup.data)
return
}
log.Println(len(*newPosts), "new posts")
// Update publishers sync data with info from retrieved posts.
var newShPosts db.ShPosts
var newShPubReplyPosts db.ShPubReplyPosts
hasNewPostsPublishers := make(map[string]updatePostsInfo)
for _, tp := range *newPosts {
// Make sure current post's username is in the publishers list.(May contain promoted post)
_, ok := publishers[tp.Userpost.N]
if !ok {
continue
}
// Get lastK, may be nil pointer.
var lastK int64
if tp.Userpost.Lastk == nil {
lastK = -1
} else {
lastK = *(tp.Userpost.Lastk)
}
upi, ok := hasNewPostsPublishers[tp.Userpost.N]
if ok {
upi.lastK = lastK
if upi.maxK < tp.Userpost.K {
upi.maxK = tp.Userpost.K
}
if upi.minK > tp.Userpost.K {
upi.minK = tp.Userpost.K
}
hasNewPostsPublishers[tp.Userpost.N] = upi
} else {
hasNewPostsPublishers[tp.Userpost.N] = updatePostsInfo{
lastK: lastK,
maxK: tp.Userpost.K,
minK: tp.Userpost.K,
}
}
// Add New ShPost
if tp.Userpost.Reply == nil && tp.Userpost.Rt == nil {
btih, category, fileSize, title, ok := retrieveMagnetInfo(tp.Userpost.Msg)
if ok {
shPost := db.ShPost{
Msg: tp.Userpost.Msg,
N: tp.Userpost.N,
K: tp.Userpost.K,
Lastk: lastK,
Time: tp.Userpost.Time,
Category: category,
Title: title,
Magnet: btih,
Size: fileSize,
}
newShPosts = append(newShPosts, shPost)
//fmt.Println(index, tp.Userpost.N, tp.Userpost.K, lastK)
}
}
// Add new ShPubReplyPost
if tp.Userpost.Reply != nil {
if tp.Userpost.Reply.N == tp.Userpost.N {
shPubReplyPosts := db.ShPubReplyPost{
Msg: tp.Userpost.Msg,
Time: tp.Userpost.Time,
N: tp.Userpost.N,
K: tp.Userpost.Reply.K,
Lastk: lastK,
}
newShPubReplyPosts = append(newShPubReplyPosts, shPubReplyPosts)
//fmt.Println(index, tp.Userpost.N, tp.Userpost.K, lastK)
}
}
}
// Save posts to DB
db.AddPosts(&newShPosts)
err = db.AddPublishersReplyPosts(&newShPubReplyPosts)
if err != nil {
log.Println(err)
log.Printf("Error: syncTimeLine db.AddPublishersReplyPosts: %+v\n", len(newShPubReplyPosts))
}
//log.Printf("%+v\n", hasNewPostsPublishers)
// Update publishers in DB
updatedPublishers := make(map[string]db.SyncData)
for name, postsInfo := range hasNewPostsPublishers {
var newSd db.SyncData
oldSd := publishers[name]
newSd.Latest = oldSd.Latest
if postsInfo.maxK > oldSd.Latest {
newSd.Latest = postsInfo.maxK
}
newSd.Since = oldSd.Since
lastK := min(postsInfo.lastK, postsInfo.minK-1)
if lastK <= oldSd.Since {
newSd.Since = newSd.Latest
newSd.Max = max_post_id
} else {
newSd.Max = lastK
}
updatedPublishers[name] = newSd
}
err = db.UpdatePublishers(&updatedPublishers)
if err != nil {
log.Println(err)
log.Printf("Error: syncTimeLine: %+v\n", updatedPublishers)
}
// Indexing posts
search.Index(&newShPosts)
}
func retrieveMagnetInfo(ss string) (string, string, uint64, string, bool) {
lowerS := strings.ToLower(ss)
// If not magnet return
if !(strings.Contains(lowerS, "magnet")) {
return "", "", 0, "", false
}
idx := strings.Index(lowerS, "magnet")
magnetString := ss[idx:]
idx = strings.Index(magnetString, "?")
if idx == -1 {
return "", "", 0, "", false
}
if len(magnetString) <= idx+1 {
return "", "", 0, "", false
}
paramString := magnetString[idx+1:]
//log.Println(paramString)
params := strings.Split(paramString, "&")
var category, title string
var fileSize uint64
for _, param := range params {
//log.Println(param)
k := strings.Split(param, "=")
//log.Println(k, len(k))
if len(k) >= 2 {
n := strings.ToLower(k[0])
if strings.Contains(n, "xl") {
var err error
fileSize, err = strconv.ParseUint(k[1], 10, 64)
if err != nil {
fileSize = 0
}
}
if strings.Contains(n, "x.c") {
category = k[1]
}
if strings.Contains(n, "dn") {
title = k[1]
}
}
}
magnetString = strings.TrimSpace(magnetString)
category = strings.TrimSpace(category)
title = strings.TrimSpace(title)
return magnetString, category, fileSize, title, true
}
func runSyncTimeLine() {
for {
time.Sleep(60 * time.Second)
syncTimeLine()
}
}
// todo: add x.l