/
twitterservice.go
152 lines (117 loc) · 3.42 KB
/
twitterservice.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
package booktweets
import (
"sort"
"strings"
"sync"
"net/url"
"github.com/nealedj/anaconda"
"appengine/urlfetch"
"encoding/json"
)
type searchResponse struct {
Statuses []anaconda.Tweet
}
type BooksByTweets []Book
func (b BooksByTweets) Len() int {
return len(b)
}
func (b BooksByTweets) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
func (b BooksByTweets) Less(i, j int) bool {
return len(b[i].Tweets) < len(b[j].Tweets)
}
func getTwitterKeyword(bookTitle string) string {
bookTitle = strings.ToLower(bookTitle)
// get everything before brackets
bookTitle = strings.SplitN(bookTitle, "(", 2)[0]
if theIdx := strings.Index(bookTitle, "the"); theIdx != -1 {
// if "the" is in title then read everything after it
bookTitle = bookTitle[theIdx+3:]
}
return strings.TrimSpace(bookTitle)
}
func getTweets(c RequestContext, bookList []Book) {
anaconda.SetConsumerKey(twitterConsumerKey)
anaconda.SetConsumerSecret(twitterConsumerSecret)
api := anaconda.NewTwitterApi(twitterAccessToken, twitterAccessTokenSecret)
api.HttpClient = urlfetch.Client(c)
defer api.Close()
var wg sync.WaitGroup
wg.Add(len(bookList))
for i := 0; i < len(bookList); i++ {
go func(book *Book) {
term := getTwitterKeyword(book.Title)
c.Infof("Getting tweets for %s", term)
v := url.Values{}
v.Add("lang", "en")
tweets, err := api.GetSearchNoQueue(term, v)
defer wg.Done()
if err != nil {
c.Errorf("Twitter call failed for %s: %s", term, err)
return
}
book.Tweets = tweets
c.Infof("Got %d tweets for %s", len(book.Tweets), term)
}(&bookList[i])
}
wg.Wait()
sort.Sort(sort.Reverse(BooksByTweets(bookList)))
PrintAsJson(c, bookList)
}
func getTweets__WaitWithChannel(c RequestContext, bookList []Book) {
anaconda.SetConsumerKey(twitterConsumerKey)
anaconda.SetConsumerSecret(twitterConsumerSecret)
api := anaconda.NewTwitterApi(twitterAccessToken, twitterAccessTokenSecret)
api.HttpClient = urlfetch.Client(c)
defer api.Close()
twitterc := make(chan error)
for i := 0; i < len(bookList); i++ {
go func(book *Book) {
term := getTwitterKeyword(book.Title)
c.Infof("Getting tweets for %s", term)
v := url.Values{}
v.Add("lang", "en")
tweets, err := api.GetSearchNoQueue(term, v)
if err != nil {
c.Errorf("Twitter call failed for %s: %s", term, err)
} else {
book.Tweets = tweets
c.Infof("Got %d tweets for %s", len(book.Tweets), term)
}
twitterc <- err
}(&bookList[i])
}
for i := 0; i < len(bookList); i++ {
<-twitterc
}
sort.Sort(sort.Reverse(BooksByTweets(bookList)))
PrintAsJson(c, bookList)
}
func getTweetsSync(c RequestContext, bookList []Book){
anaconda.SetConsumerKey(twitterConsumerKey)
anaconda.SetConsumerSecret(twitterConsumerSecret)
api := anaconda.NewTwitterApi(twitterAccessToken, twitterAccessTokenSecret)
api.HttpClient = urlfetch.Client(c)
defer api.Close()
for i := 0; i < len(bookList); i++ {
book := &bookList[i]
term := getTwitterKeyword(book.Title)
c.Infof("Getting tweets for %s", term)
v := url.Values{}
v.Add("lang", "en")
tweets, err := api.GetSearch(term, nil)
if err != nil {
c.Errorf("Twitter call failed for %s: %s", term, err)
return
}
book.Tweets = tweets
c.Infof("Got %d tweets for %s", len(book.Tweets), term)
}
sort.Sort(sort.Reverse(BooksByTweets(bookList)))
PrintAsJson(c, bookList)
}
func PrintAsJson(c RequestContext, data interface{}) {
raw, _ := json.Marshal(data)
c.Infof(string(raw[:]))
}