/
aliker.go
203 lines (177 loc) · 4.94 KB
/
aliker.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
package main
import (
"fmt"
// "github.com/bradfitz/iter"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/hut8/tumblr-go"
//"github.com/kr/pretty"
"html/template"
"net/http"
"os"
)
var upgrader websocket.Upgrader
func HomeHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/index.go.html")
ensureNil(err)
err = t.Execute(w, nil)
ensureNil(err)
}
type SimilarPostRequest struct {
PostUri string
}
func sendProcessNotification(c *websocket.Conn, bh string, pid int64) error {
return c.WriteJSON(&struct {
MsgType string `json:"msg-type"`
BaseHostname string `json:"base-hostname"`
PostID int64 `json:"pid"`
}{
"process-post",
bh,
pid,
})
}
func sendBlogsLikingPostData(c *websocket.Conn, blogs []string) error {
return c.WriteJSON(&struct {
MsgType string `json:"msg-type"`
Blogs []string `json:"blogs"`
}{
"blogs-liking-post",
blogs,
})
}
func sendBlogLikesData(c *websocket.Conn, bhn string, likes []int64,
page int, totalPages int) error {
return c.WriteJSON(&struct {
MsgType string `json:"msg-type"`
Blog string `json:"blog"`
Likes []int64 `json:"likes"`
}{
"blog-likes",
bhn,
likes,
})
}
func sendPostSimilarities(c *websocket.Conn, s map[int64]int64) error {
return c.WriteJSON(&struct {
MsgType string `json:"msg-type"`
SimilarityMap map[int64]int64 `json:"similarities"`
}{
"post-similarities",
s,
})
}
func sendPostsData(c *websocket.Conn, pd map[int64]tumblr.Post) error {
return c.WriteJSON(&struct {
MsgType string `json:"msg-type"`
PostData map[int64]tumblr.Post `json:"posts-data"`
}{
"posts-data",
pd,
})
}
func sendErrorNotification(c *websocket.Conn, err error) error {
return c.WriteJSON(&struct {
MsgType string `json:"msg-type"`
Message string `json:"message"`
}{
"error",
err.Error(),
})
}
func SimilarHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
ensureNil(err)
defer conn.Close()
// Figure out what post they want and extract the details we need
spr := &SimilarPostRequest{}
err = conn.ReadJSON(spr)
ensureNil(err)
bh, pid, err := extractPostId(spr.PostUri)
// bh, pid will be zero values if invalid. the JS knows this.
mustSend(sendProcessNotification(conn, bh, pid))
if err != nil {
return
}
// Find every blog that likes the input post
likingBlogs, err := blogsLikingPost(bh, pid)
ensureNil(err)
mustSend(sendBlogsLikingPostData(conn, likingBlogs))
// Find every liked post from every blog that likes the input post
// postId -> []blogUrl
blogLikeMap := make(map[int64][]string)
// postId -> Post
postMap := make(map[int64]tumblr.Post)
// TODO Break out each blog into a separate goroutine!
for _, blogName := range likingBlogs {
b := tumblrClient.NewBlog(blogName)
fmt.Printf("Requesting likes for: %s\n", b.BaseHostname)
likeCollection, err := b.Likes(tumblr.LimitOffset{})
if err != nil {
fmt.Println(err.Error())
continue
}
// How many pages are we going to loop through
totalPages := int(likeCollection.TotalCount) / 20
if (likeCollection.TotalCount % 20) != 0 {
totalPages += 1
}
if totalPages > 20 {
totalPages = 20
}
// Loop over pages.
// Note that we already retrieved the first page, so fetch at end of loop
for currentPage := 1; currentPage < totalPages; currentPage++ {
pagePostIDs := []int64{}
for _, likedPost := range likeCollection.Likes.Posts {
pagePostIDs = append(pagePostIDs, likedPost.PostId())
postMap[likedPost.PostId()] = likedPost
// Initialize if key if needbe. No autovivification here!
_, ok := blogLikeMap[likedPost.PostId()]
if !ok {
blogLikeMap[likedPost.PostId()] = []string{}
}
blogLikeMap[likedPost.PostId()] = append(
blogLikeMap[likedPost.PostId()], b.BaseHostname)
}
mustSend(sendBlogLikesData(conn, b.BaseHostname, pagePostIDs,
currentPage, totalPages))
// Fetch next page
likeCollection, err = b.Likes(tumblr.LimitOffset{
Offset: currentPage * 20,
})
if err != nil {
sendErrorNotification(conn, err)
fmt.Println(err.Error())
break
}
}
}
// Send a map of postId -> tumblr.Post
mustSend(sendPostsData(conn, postMap))
// Finally, calculate overall similarities now that we have all the data
// postId -> how many users like it
postPopularityMap := make(map[int64]int64)
for postId, blogLikes := range blogLikeMap {
postPopularityMap[postId] = int64(len(blogLikes))
}
mustSend(sendPostSimilarities(conn, postPopularityMap))
//fmt.Printf("PostID->Blogs who like it%# v\n", pretty.Formatter(blogLikeMap))
}
func mustSend(x interface{}) {
ensureNil(x)
}
func ensureNil(x interface{}) {
if x != nil {
panic(x)
}
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/", HomeHandler)
router.HandleFunc("/post", SimilarHandler)
n := negroni.New()
n.UseHandler(router)
n.Run(fmt.Sprintf(":%s", os.Getenv("PORT")))
}