forked from justwatch/facebook-marketing-api-golang-sdk
/
image.go
113 lines (94 loc) · 3.08 KB
/
image.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
package v16
import (
"context"
"encoding/json"
"errors"
"io"
"net/url"
"path"
"regexp"
"github.com/stavgayer/facebook-marketing-api-golang-sdk/fb"
"golang.org/x/sync/errgroup"
)
var regexImageFilename = regexp.MustCompile(`^\d+_(\d+)_\d+_[a-z]\.([a-z]+)$`)
// ImageService works with ad images.
type ImageService struct {
c *fb.Client
}
// ReadList writes all ad images from an account to res.
func (is *ImageService) ReadList(ctx context.Context, act string, res chan<- Image) error {
jres := make(chan json.RawMessage)
wg := errgroup.Group{}
wg.Go(func() error {
defer close(jres)
return is.c.ReadList(ctx, fb.NewRoute(Version, "/act_%s/adimages", act).Fields("name", "hash", "url", "width", "height").Limit(1000).String(), jres)
})
wg.Go(func() error {
for e := range jres {
v := Image{}
err := json.Unmarshal(e, &v)
if err != nil {
return err
}
v.ID, err = is.getImageID(v.URL)
if err != nil {
return err
}
res <- v
}
return nil
})
return wg.Wait()
}
// Upload uploads an image to Facebook.
func (is *ImageService) Upload(ctx context.Context, act, name string, r io.Reader) (*Image, error) {
fur := &fileUploadResponse{}
err := is.c.UploadFile(ctx, fb.NewRoute(Version, "/act_%s/adimages", act).String(), name, r, nil, fur)
if err != nil {
return nil, err
}
im := fur.Images[name]
if im == nil {
return nil, errors.New("did not get image metadata response")
}
id, err := is.getImageID(im.URL)
if err != nil {
return nil, err
}
im.ID = id
return im, nil
}
func (is *ImageService) getImageID(s string) (string, error) {
parsed, err := url.Parse(s)
if err != nil {
return "", err
}
_, file := path.Split(parsed.Path)
parts := regexImageFilename.FindStringSubmatch(file)
if len(parts) < 3 {
return "", nil
}
return parts[1], nil
}
// Image represents an image being uploaded to the creative library.
type Image struct {
ID string `json:"id,omitempty"`
Height int `json:"height,omitempty"`
Hash string `json:"hash,omitempty"`
IsAssociatedCreativesInAdgroups bool `json:"is_associated_creatives_in_adgroups,omitempty"`
Name string `json:"name,omitempty"`
OriginalHeight int `json:"original_height,omitempty"`
OriginalWidth int `json:"original_width,omitempty"`
PermalinkURL string `json:"permalink_url,omitempty"`
Status string `json:"status,omitempty"`
UpdatedTime string `json:"updated_time,omitempty"`
CreatedTime string `json:"created_time,omitempty"`
URL string `json:"url,omitempty"`
AccountID string `json:"account_id,omitempty"`
URL128 string `json:"url_128,omitempty"`
Width int `json:"width,omitempty"`
Creatives []string `json:"creatives,omitempty"`
}
type fileUploadResponse struct {
Images map[string]*Image `json:"images"`
}