-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfacebook.go
215 lines (176 loc) · 4.79 KB
/
facebook.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"encoding/json"
"errors"
//"fmt"
"net/http"
"net/url"
//"github.com/kr/pretty"
)
type FacebookPost struct {
ID string // id
PageName string
PageID string
Type string // type
Caption string // message / description
TotalLikes uint64 // likes.summary.total_count
TotalComments uint64 // comments.summary.total_count
Thumbnail string // picture
PublishedAt string // created_time
}
func (fp *FacebookPost) GetMetadata() bool {
fp.GetPageID()
var respTyped postMetaResp
resp, _ := fbRequest("/" + fp.PageID + "_" + fp.ID + "?fields=id,name,caption,description,picture,created_time,type,message,properties,insights,likes.limit(1).summary(true),comments.limit(1).summary(true)")
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&respTyped)
if err != nil {
return false
}
fp.Type = respTyped.Type
fp.Caption = respTyped.Message
fp.TotalLikes = respTyped.Likes.Summery.TotalCount
fp.TotalComments = respTyped.Comments.Summary.TotalCount
fp.Thumbnail = respTyped.Picture
fp.PublishedAt = respTyped.CreatedTime
return true
}
func (fp FacebookPost) GetComments() CommentList {
fp.GetPageID()
var comments = []*Comment{}
after := ""
max := 10000
for {
if len(comments) >= max {
break
}
var respTyped postCommentListResp
resp, _ := fbRequest("/" + fp.PageID + "_" + fp.ID + "/comments?limit=100&order=reverse_chronological&after=" + after)
//fmt.Println("/" + this.PageID + "_" + this.ID + "/comments?limit=100&order=reverse_chronological&after=" + after)
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&respTyped)
//LogMsg(fmt.Sprintf("Total comments: %d", len(comments)))
if err == nil {
for _, entry := range respTyped.Data {
thisComment := &Comment{
ID: entry.ID,
Published: entry.CreatedOn,
Content: entry.Message,
AuthorName: entry.From.Name,
}
comments = append(comments, thisComment)
}
if respTyped.Pagination.Cursors.After != "" {
after = respTyped.Pagination.Cursors.After
} else {
break
}
}
}
return CommentList{Comments: comments}
}
func (fp *FacebookPost) GetPageID() *FacebookPost {
if fp.PageID != "" {
return fp
}
var respTyped pageNameResp
resp, _ := fbRequest("/" + fp.PageName)
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
err := decoder.Decode(&respTyped)
if err != nil {
return fp
}
fp.PageID = respTyped.ID
return fp
}
func fbRequest(path string) (*http.Response, error) {
u, err := url.Parse(path)
if err != nil {
return nil, errors.New("FB request path invalid")
}
u.Scheme = "https"
u.Host = "graph.facebook.com"
query := u.Query()
query.Add("access_token", GetConfigString("fbkey")+"|"+GetConfigString("fbsecret"))
u.RawQuery = query.Encode()
//LogMsg(u.String())
response, err := http.Get(u.String())
if err != nil {
return nil, err
} else {
return response, nil
}
}
type pageNameResp struct {
Name string `json:"name"`
ID string `json:"id"`
}
type postCommentResp struct {
From struct {
Name string `json:"name"`
ID string `json:"id"`
} `json:"from"`
Message string `json:"message"`
CreatedOn string `json:"created_time"`
ID string `json:"id"`
}
type postCommentListResp struct {
Data []postCommentResp `json:"data"`
Pagination struct {
Cursors struct {
After string `json:"after,omitempty"`
Before string `json:"before,omitempty"`
} `json:"cursors"`
Next string `json:"next,omitempty"`
} `json:"paging"`
}
type postMetaProps struct {
Name string `json:"name"`
Text string `json:"text"`
}
type likesSummary struct {
TotalCount uint64 `json:"total_count"`
CanLike bool `json:"can_like"`
HasLiked bool `json:"has_liked"`
}
type commentSummary struct {
Order string `json:"order"`
TotalCount uint64 `json:"total_count"`
CanComment bool `json:"can_comment"`
}
type postMetaResp struct {
ID string `json:"id"`
Name string `json:"name"`
Picture string `json:"picture"`
CreatedTime string `json:"created_time"`
Type string `json:"type"`
Message string `json:"message"`
Properties []postMetaProps `json:"properties"`
Likes struct {
Data []struct {
ID string `json:"id"`
} `json:"data"`
Paging struct {
Cursors struct {
After string `json:"after"`
Before string `json:"before"`
}
Next string `json:"next"`
} `json:"paging"`
Summery likesSummary `json:"summary"`
} `json:"likes"`
Comments struct {
Data []postCommentResp
Paging struct {
Cursors struct {
After string `json:"after"`
Before string `json:"before"`
}
Next string `json:"next"`
} `json:"paging"`
Summary commentSummary `json:"summary"`
}
}