-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweets.go
114 lines (91 loc) · 2.35 KB
/
tweets.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
package main
import (
"fmt"
"net/http"
"net/url"
"github.com/jmcvetta/neoism"
"github.com/juju/errors"
"github.com/kurrik/twittergo"
)
func (p *Engine) tweetsGet(apiURL string, maxID uint64, params map[string]string) (twittergo.Timeline, error) {
query := url.Values{}
for k, v := range params {
query.Set(k, v)
}
var results = twittergo.Timeline{}
for {
if maxID != 0 {
query.Set("max_id", fmt.Sprintf("%v", maxID))
}
endpoint := fmt.Sprintf(apiURL, query.Encode())
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, errors.Annotate(err, "create request")
}
resp, err := p.client.SendRequest(req)
if err != nil {
return nil, errors.Annotate(err, "send request")
}
if err = resp.Parse(&results); err != nil {
if p.handleRateLimitError(err) {
continue
} else {
logger.Errorf("Problem parsing response: %v\n", err)
}
}
if resp.HasRateLimit() {
logger.Infof("ratelimit: %v calls available", resp.RateLimitRemaining())
}
break
}
return results, nil
}
func (p *Engine) tweetsImport(db *neoism.Database, tweets twittergo.Timeline, props neoism.Props) error {
logger.Infof("import %d new tweets", len(tweets))
props["tweets"] = tweets
cq := neoism.CypherQuery{
Statement: CYPHER_TWEETS_IMPORT,
Parameters: props,
}
if err := db.Cypher(&cq); err != nil {
return errors.Annotate(err, "db query")
}
return nil
}
func (p *Engine) AddTweets() error {
logger.Info("add tweets")
db, err := p.ensureClients()
if err != nil {
return errors.Annotate(err, "ensure clients")
}
screenName, err := p.cnf.ScreenName()
if err != nil {
return err
}
params := map[string]string{
"count": "200",
"screen_name": screenName,
}
props := neoism.Props{
"mention_type": "normal",
}
maxID, err := p.getMaxID(db, CYPHER_TWEETS_MAX_ID, screenName)
if err != nil {
return errors.Annotate(err, "tweets get max id")
}
tweets, err := p.tweetsGet(TW_PATH_USER_TIMELINE, maxID, params)
if err != nil {
return errors.Annotate(err, "get tweets")
}
for len(tweets) > 0 {
maxID = tweets[len(tweets)-1].Id() - 1
if err = p.tweetsImport(db, tweets, props); err != nil {
return errors.Annotate(err, "import tweets")
}
tweets, err = p.tweetsGet(TW_PATH_USER_TIMELINE, maxID, params)
if err != nil {
return errors.Annotate(err, "get tweets")
}
}
return nil
}