-
Notifications
You must be signed in to change notification settings - Fork 25
/
post.go
388 lines (354 loc) · 12.8 KB
/
post.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package v16
import (
"context"
"encoding/json"
"fmt"
"strings"
"golang.org/x/sync/errgroup"
"github.com/justwatch/facebook-marketing-api-golang-sdk/fb"
)
// PostService works on posts.
type PostService struct {
c *fb.Client
*fb.StatsContainer
}
// Get returns a single post.
func (ps *PostService) Get(ctx context.Context, id string) (*Post, error) {
res := &Post{}
err := ps.c.GetJSON(ctx, fb.NewRoute(Version, "/%s", id).Fields(postFields...).String(), res)
if err != nil {
if fb.IsNotFound(err) {
return nil, nil
}
return nil, err
}
err = ps.getPostAttachments(ctx, res)
if err != nil {
return nil, err
}
if res.Type != "video" && res.Type != "photo" && res.Type != "link" {
res.Type = "unknown"
}
return res, nil
}
func (ps *PostService) getPostAttachments(ctx context.Context, post *Post) error {
if post == nil {
return nil
}
pAs := &StoryAttachments{}
err := ps.c.GetJSON(ctx, fb.NewRoute(Version, "/%s/attachments", post.ID).Fields(postAttachmentsFields...).String(), pAs)
if err != nil {
if fb.IsNotFound(err) {
return nil
}
return err
}
pA := StoryAttachment{}
if len(pAs.Data) > 0 {
pA = pAs.Data[0]
} else {
return nil
}
sat := strings.TrimSpace(pA.StoryAttachmentType)
mt := strings.TrimSpace(pA.MediaType)
if mt != "" {
if mt == "link" {
post.Type = "status"
} else {
post.Type = mt
}
} else if sat != "" {
post.Type = sat
}
if strings.HasPrefix(post.Type, "video") {
post.Type = "video"
}
if post.Type == "album" {
post.Type = "photo"
}
if pA.Title != "" {
post.Name = pA.Title
} else if pA.Name != "" {
post.Name = pA.Name
}
if pA.URL != "" {
post.Link = pA.URL
} else if pA.UnshimmedURL != "" {
post.Link = pA.UnshimmedURL
}
if pA.Description != "" {
post.Description = pA.Description
}
if pA.Target != nil {
if pA.Target.ID != "" {
post.ObjectID = pA.Target.ID
}
}
return nil
}
// GetReactions returns the amount of reactions for a post.
func (ps *PostService) GetReactions(ctx context.Context, postID string) (Reactions, error) {
m := Reactions{}
for _, r := range reactions {
rc := fb.SummaryContainer{}
err := ps.c.GetJSON(ctx, fb.NewRoute(Version, "/%s/reactions", postID).Summary("total_count").Limit(0).Type(r).String(), &rc)
if err != nil {
return nil, err
} else if rc.Summary.TotalCount == 0 {
continue
}
m[r] = rc.Summary.TotalCount
}
return m, nil
}
// CountComments returns the total amount of parent comments.
func (ps *PostService) CountComments(ctx context.Context, postID string) (uint64, error) {
sc := &fb.SummaryContainer{}
err := ps.c.GetJSON(ctx, fb.NewRoute(Version, "/%s/comments", postID).Limit(0).Summary("1").String(), sc)
return sc.Summary.TotalCount, err
}
// ListComments creates a new CommentListCall
// Filters may be "stream" or "toplevel".
func (ps *PostService) ListComments(postID, filter string) *CommentListCall {
return &CommentListCall{
RouteBuilder: fb.NewRoute(Version, "/%s/comments", postID).Fields("message", "message_tags", "parent", "from", "created_time").Limit(100).Order("chronological").Filter(filter),
c: ps.c,
id: postID,
StatsContainer: ps.StatsContainer,
}
}
// CommentListCall is used for listing comments of a post.
type CommentListCall struct {
*fb.RouteBuilder
c *fb.Client
id string
*fb.StatsContainer
}
// List performs the CommentListCall and returns all comments as slice.
func (clc *CommentListCall) List(ctx context.Context) ([]Comment, error) {
stats := clc.StatsContainer.AddStats(clc.id)
if stats == nil {
return nil, fmt.Errorf("post %s comments already being downloaded", clc.id)
}
defer clc.StatsContainer.RemoveStats(clc.id)
ctx = stats.AddToContext(ctx)
res := []Comment{}
err := clc.c.GetList(ctx, clc.RouteBuilder.String(), &res)
if err != nil {
return nil, err
}
return res, nil
}
func (clc *CommentListCall) Read(ctx context.Context, c chan<- Comment) error {
stats := clc.StatsContainer.AddStats(clc.id)
if stats == nil {
return fmt.Errorf("post %s comments already being downloaded", clc.id)
}
jres := make(chan json.RawMessage)
wg := errgroup.Group{}
wg.Go(func() error {
defer close(jres)
return clc.c.ReadList(ctx, clc.RouteBuilder.String(), jres)
})
wg.Go(func() error {
for e := range jres {
v := Comment{}
err := json.Unmarshal(e, &v)
if err != nil {
return err
}
stats.Add(1)
c <- v
}
clc.StatsContainer.RemoveStats(clc.id)
return nil
})
return wg.Wait()
}
// Other fields that can be used:
// "actions",
// "admin_creator",
// "allowed_advertising_objectives",
// "application",
// "backdated_time",
// "caption",
// "child_attachments",
// "comments_mirroring_domain",
// "coordinates",
// "created_time",
// "event",
// "expanded_height",
// "expanded_width",
// "feed_targeting",
// "full_picture",
// "height",
// "icon",
// "instagram_eligibility",
// "is_app_share",
// "is_expired",
// "is_hidden",
// "is_instagram_eligible",
// "is_popular",
// "is_published",
// "is_spherical",
// "message_tags",
// "multi_share_end_card",
// "multi_share_optimized",
// "parent_id",
// "place",
// "privacy",
// "promotion_status",
// "properties",
// "scheduled_publish_time",
// "shares",
// "status_type",
// "story",
// "story_tags",
// "subscribed",
// "target",
// "targeting",
// "timeline_visibility",
// "updated_time",
// "via",
// "video_buying_eligibility",
// "width",.
var (
postFields = []string{"call_to_action", "from", "id", "message", "picture", "promotable_id"}
reactions = []string{"LIKE", "LOVE", "WOW", "HAHA", "SAD", "ANGRY", "THANKFUL"}
postAttachmentsFields = []string{"description", "name", "type", "url", "target", "media_type"}
)
// Post represents the fb graph api response for a fb video post https://developers.facebook.com/docs/graph-api/reference/v5.0/page-post
type Post struct {
CreatedTime string `json:"created_time"`
ContentCategory string `json:"content_category"`
Description string `json:"description"`
EmbedHTML string `json:"embed_html"`
Embeddable bool `json:"embeddable"`
ID string `json:"id"`
Icon string `json:"icon"`
IsInstagramEligible bool `json:"is_instagram_eligible"`
Picture string `json:"picture"`
PermalinkURL string `json:"permalink_url"`
MonetizationStatus string `json:"monetization_status"`
Length float64 `json:"length"`
Link string `json:"link"`
Name string `json:"name"`
Type string `json:"type"`
Published bool `json:"published"`
UpdatedTime string `json:"updated_time"`
Message string `json:"message"`
InstagramEligibility string `json:"instagram_eligibility"`
FullPicture string `json:"full_picture"`
MultiShareEndCard bool `json:"multi_share_end_card"`
MultiShareOptimized bool `json:"multi_share_optimized"`
ObjectID string `json:"object_id"`
PromotableID string `json:"promotable_id"`
PromotionStatus string `json:"promotion_status"`
StatusType string `json:"status_type"`
Subscribed bool `json:"subscribed"`
TimelineVisibility string `json:"timeline_visibility"`
VideoBuyingEligibility []string `json:"video_buying_eligibility"`
IsHidden bool `json:"is_hidden"`
IsAppShare bool `json:"is_app_share"`
IsExpired bool `json:"is_expired"`
IsPopular bool `json:"is_popular"`
IsPublished bool `json:"is_published"`
IsSpherical bool `json:"is_spherical"`
CallToAction *AdCreativeLinkDataCallToAction `json:"call_to_action"`
Format []struct {
EmbedHTML string `json:"embed_html"`
Filter string `json:"filter"`
Height int `json:"height"`
Picture string `json:"picture"`
Width int `json:"width"`
} `json:"format"`
From IDContainer `json:"from"`
Privacy struct {
Allow string `json:"allow"`
Deny string `json:"deny"`
Description string `json:"description"`
Friends string `json:"friends"`
Networks string `json:"networks"`
Value string `json:"value"`
} `json:"privacy"`
Status struct {
VideoStatus string `json:"video_status"`
} `json:"status"`
Application struct {
Category string `json:"category"`
Link string `json:"link"`
Name string `json:"name"`
ID string `json:"id"`
} `json:"application"`
Coordinates struct{} `json:"coordinates"`
Actions []struct {
Name string `json:"name"`
Link string `json:"link"`
} `json:"actions"`
Properties []struct {
Name string `json:"name"`
Text string `json:"text"`
} `json:"properties"`
}
// StoryAttachment holds information about a post, used since v3.3 https://developers.facebook.com/docs/graph-api/reference/story-attachment/
type StoryAttachment struct {
Description string `json:"description,omitempty"`
Media *StoryAttachmentMedia `json:"media,omitempty"`
MediaType string `json:"media_type,omitempty"`
Title string `json:"title,omitempty"`
StoryAttachmentType string `json:"type,omitempty"`
UnshimmedURL string `json:"unshimmed_url,omitempty"`
URL string `json:"url,omitempty"`
Name string `json:"name,omitempty"`
Target *StoryAttachmentTarget `json:"target,omitempty"`
}
// StoryAttachments wraps the data slice around the StoryAttachment(s).
type StoryAttachments struct {
Data []StoryAttachment `json:"data,omitempty"`
}
// StoryAttachmentTarget https://developers.facebook.com/docs/graph-api/reference/story-attachment-target/
type StoryAttachmentTarget struct {
ID string `json:"id,omitempty"`
UnshimmedURL string `json:"unshimmed_url,omitempty"`
URL string `json:"url,omitempty"`
}
// StoryAttachmentMedia https://developers.facebook.com/docs/graph-api/reference/v5.0/story-attachment-media
type StoryAttachmentMedia struct {
Image interface{} `json:"image,omitempty"`
Source string `json:"source,omitempty"`
}
// Comment represents a comment on a facebook post.
type Comment struct {
ID string `json:"id,omitempty"`
Attachment json.RawMessage `json:"attachment,omitempty"`
CanComment bool `json:"can_comment,omitempty"`
CanRemove bool `json:"can_remove,omitempty"`
CanHide bool `json:"can_hide,omitempty"`
CanLike bool `json:"can_like,omitempty"`
CanReplyPrivately bool `json:"can_reply_privately,omitempty"`
CommentCount int32 `json:"comment_count,omitempty"`
CreatedTime fb.Time `json:"created_time,omitempty"`
From *User `json:"from,omitempty"`
LikeCount int32 `json:"like_count,omitempty"`
Message string `json:"message,omitempty"`
MessageTags []MessageTag `json:"message_tags,omitempty"`
Object json.RawMessage `json:"object,omitempty"`
Parent *Comment `json:"parent,omitempty"`
PrivateReplyConversation json.RawMessage `json:"private_reply_conversation,omitempty"`
UserLikes bool `json:"user_likes,omitempty"`
}
// MessageTag represents a tagged user or site in a comment.
type MessageTag struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Offset int `json:"offset,omitempty"`
Length int `json:"length,omitempty"`
}
// User represents a facebook user.
type User struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
// Reactions contains a reation and how often it was performed on an object.
type Reactions map[string]uint64