-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfbapp.go
134 lines (114 loc) · 3.3 KB
/
fbapp.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
package fbbotscan
import (
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
fb "github.com/huandu/facebook"
"github.com/moensch/fbbotscan/config"
)
type FBApp struct {
AppToken string
App *fb.App
Session *fb.Session
ConfigPath string
Config *config.Config
}
func New(configPath string) *FBApp {
fbapp := &FBApp{
ConfigPath: configPath,
}
if err := fbapp.Initialize(); err != nil {
log.Fatalf("Failed to initialize: %s", err)
}
return fbapp
}
func (a *FBApp) LoadConfig() error {
var err error
a.Config, err = config.LoadFile(a.ConfigPath)
return err
}
func (a *FBApp) Initialize() error {
var err error
if err := a.LoadConfig(); err != nil {
return err
}
log.Infof("Application ID: %s", a.Config.FB.AppID)
log.Infof("Application Secret: %s", a.Config.FB.AppSecret)
a.App = fb.New(a.Config.FB.AppID, a.Config.FB.AppSecret)
a.AppToken = a.App.AppAccessToken()
log.Infof("Application Token: %s", a.AppToken)
a.Session = a.App.Session(a.AppToken)
a.Session.SetDebug(fb.DEBUG_ALL)
return err
}
func (a *FBApp) LoadFeed(pageId string, maxEntries int, since int64) ([]FBPost, error) {
var err error
var posts = make([]FBPost, 0)
log.Infof("Loading feed for %s since %d", pageId, since)
res, err := a.Session.Get(fmt.Sprintf("/%s/feed", pageId), fb.Params{"limit": "4", "fields": "id,created_time,permalink_url,link,message,story", "since": since})
if err != nil {
return posts, err
}
p, err := res.Paging(a.Session)
if err != nil {
return posts, errors.New(fmt.Sprintf("Failed to call Paging: %s", err))
}
totalPosts := 0
for ok := true; ok; ok = p.HasNext() {
log.Debugf(" Loading next page for %s", pageId)
for _, res := range p.Data() {
var post FBPost
err = res.Decode(&post)
if err != nil {
return posts, errors.New(fmt.Sprintf("Failed to decode post: %s", err))
}
posts = append(posts, post)
totalPosts++
if totalPosts >= maxEntries {
return posts, err
}
}
noMore, err := p.Next()
if err != nil {
return posts, errors.New(fmt.Sprintf("Error whilst calling Next() on paginaation: %s", err))
}
if noMore == true {
return posts, err
}
}
return posts, err
}
func (a *FBApp) LoadComments(objectId string, since int64) ([]FBComment, error) {
var err error
var comments = make([]FBComment, 0)
log.Infof("Loading comments for %s since %d", objectId, since)
res, err := a.Session.Get(fmt.Sprintf("/%s/comments", objectId), fb.Params{"limit": "20", "order": "chronological", "fields": "id,created_time,from,message,parent,comment_count,like_count,permalink_url", "since": since})
if err != nil {
return comments, err
}
p, err := res.Paging(a.Session)
if err != nil {
return comments, errors.New(fmt.Sprintf("Failed to call Paging: %s", err))
}
totalPosts := 0
for ok := true; ok; ok = p.HasNext() {
log.Debugf(" Loading next page for %s", objectId)
for _, res := range p.Data() {
var comment FBComment
err = res.Decode(&comment)
if err != nil {
return comments, errors.New(fmt.Sprintf("Failed to decode comment: %s", err))
}
comments = append(comments, comment)
totalPosts++
}
noMore, err := p.Next()
if err != nil {
return comments, errors.New(fmt.Sprintf("Error whilst calling Next() on paginaation: %s", err))
}
if noMore == true {
return comments, err
}
}
return comments, err
}