-
Notifications
You must be signed in to change notification settings - Fork 4
/
goFbAlbum.go
78 lines (66 loc) · 1.75 KB
/
goFbAlbum.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
package goFbAlbum
import (
"encoding/json"
"errors"
"fmt"
"log"
fb "github.com/huandu/facebook"
)
func init() {
}
type FbAlbum struct {
Token string
}
// Constructor
func NewFbAlbum(token string) *FbAlbum {
if token == "" {
return nil
}
f := new(FbAlbum)
f.Token = token
return f
}
// Get my all albums
func (self *FbAlbum) GetMyAlbums() (*FBAlbums, error) {
return self.GetAlbumsByUserId("me")
}
// Get all album using user id.
// note: this function only work if you provide a page id or page name. such as scottiepippen or 112743018776863.
func (self *FbAlbum) GetAlbumsByUserId(uid string) (*FBAlbums, error) {
if uid == "" {
return nil, errors.New("uid is empty")
}
resAlbum := self.RunFBGraphAPI("/" + uid + "/albums")
retAlbum := FBAlbums{}
ParseMapToStruct(resAlbum, &retAlbum)
return &retAlbum, nil
}
// Get all photo from a album id, you can get album id from FBAlbums{} struct.
func (self *FbAlbum) GetPhotoByAlbum(albumId string, count int) (*FBPhotos, error) {
if albumId == "" {
return nil, errors.New("albumId is empty")
}
photoRet := FBPhotos{}
queryString := fmt.Sprintf("/%s/photos?limit=%d", albumId, count)
resPhoto := self.RunFBGraphAPI(queryString)
ParseMapToStruct(resPhoto, &photoRet)
return &photoRet, nil
}
// FaceBook Graph Query API.
func (self *FbAlbum) RunFBGraphAPI(query string) (queryResult interface{}) {
res, err := fb.Get(query, fb.Params{
"access_token": self.Token,
})
if err != nil {
log.Fatalln("FB connect error, err=", err.Error())
}
return res
}
// Parse Graph API result and convert to specific interface.
func ParseMapToStruct(inData interface{}, decodeStruct interface{}) {
jret, _ := json.Marshal(inData)
err := json.Unmarshal(jret, &decodeStruct)
if err != nil {
log.Fatalln(err)
}
}