-
Notifications
You must be signed in to change notification settings - Fork 0
/
followers.go
106 lines (85 loc) · 2.25 KB
/
followers.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
package main
import (
"fmt"
"net/http"
"net/url"
"github.com/jmcvetta/neoism"
"github.com/juju/errors"
)
func (p *Engine) usersGet(apiURL string, cursor int64, params map[string]string) ([]interface{}, int64, error) {
query := url.Values{}
for k, v := range params {
query.Set(k, v)
}
query.Set("cursor", fmt.Sprintf("%v", cursor))
results := make(map[string]interface{})
for {
endpoint := fmt.Sprintf(apiURL, query.Encode())
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, 0, errors.Annotate(err, "create request")
}
resp, err := p.client.SendRequest(req)
if err != nil {
return nil, 0, 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
}
users := results["users"].([]interface{})
cursor = int64(results["next_cursor"].(float64))
return users, cursor, nil
}
func (p *Engine) usersImport(db *neoism.Database, users []interface{}, props neoism.Props) error {
logger.Infof("import %d users", len(users))
props["users"] = users
cq := neoism.CypherQuery{
Statement: CYPHER_FOLLOWERS_IMPORT,
Parameters: props,
}
if err := db.Cypher(&cq); err != nil {
return errors.Annotate(err, "db query")
}
return nil
}
func (p *Engine) AddFollowers() error {
logger.Info("add followers")
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{
"screen_name": screenName,
}
users, cursor, err := p.usersGet(TW_PATH_FOLLOWERS_TIMELINE, -1, params)
if err != nil {
return errors.Annotate(err, "get followers")
}
for len(users) > 0 {
if err = p.usersImport(db, users, props); err != nil {
return errors.Annotate(err, "import followers")
}
users, cursor, err = p.usersGet(TW_PATH_FOLLOWERS_TIMELINE, cursor, params)
if err != nil {
return errors.Annotate(err, "get followers")
}
}
return nil
}