/
tweet-retreiver.go
69 lines (56 loc) · 1.49 KB
/
tweet-retreiver.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
package tweetharvest
import (
"sync"
"time"
"github.com/ChimeraCoder/anaconda"
"golang.org/x/net/context"
"google.golang.org/appengine/log"
"google.golang.org/appengine/urlfetch"
)
//TweetRetriever is responsible for getting a list of Tweets from the Twitter API
type TweetRetriever struct {
context context.Context
out chan<- anaconda.Tweet
}
//getTweets gets all tweets from twitter with the speified keyword
func (tr TweetRetriever) getTweets(query string,
cutoff time.Time,
wg *sync.WaitGroup) {
defer wg.Done()
log.Infof(tr.context, "Downloading Tweets.")
anaconda.SetConsumerKey(consumerKey)
anaconda.SetConsumerSecret(consumerSecretKey)
api := anaconda.NewTwitterApi(accessToken, accessTokenSecret)
api.HttpClient.Transport = &urlfetch.Transport{Context: tr.context}
result, err := api.GetSearch(query, nil)
if err != nil {
log.Errorf(tr.context, "Harvester- getTweets: %v", err.Error())
return
}
cont := true
for cont {
cont = tr.addIfNewerThan(cutoff, result)
cont = false
if cont {
result, err = result.GetNext(api)
//log.Infof(c, "Getting more tweets!")
if err != nil {
log.Errorf(tr.context, "Harvester- getTweets: %v", err.Error())
}
}
}
close(tr.out)
}
func (tr TweetRetriever) addIfNewerThan(cutoff time.Time,
result anaconda.SearchResponse) bool {
cont := true
for _, tweet := range result.Statuses {
if time, _ := tweet.CreatedAtTime(); time.After(cutoff) {
tr.out <- tweet
} else {
cont = false
break
}
}
return cont
}