-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.go
More file actions
112 lines (107 loc) · 2.64 KB
/
methods.go
File metadata and controls
112 lines (107 loc) · 2.64 KB
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
package main
import (
"os"
"io"
//"log"
"errors"
"strings"
"net/http"
"encoding/json"
)
// Get user profile picture
func getProfilePicture(client *http.Client) (string, error) {
resp, err := client.Get(getAPIUrl("/me/picture?type=large"))
defer resp.Body.Close()
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", errors.New("Unathorized. Check token.")
}
pictureFileExt := "jpg"
if strings.ToLower(resp.Header.Get("Content-Type")) == "image/png" {
pictureFileExt = "png"
}
pictureFilename := "picture." + pictureFileExt
picture, err := os.Create(pictureFilename)
if err != nil {
return "", err
}
defer picture.Close()
_, err = io.Copy(picture, resp.Body)
if err != nil {
return "", err
}
return pictureFilename, nil
}
// Get user profile name
func getProfileName(client *http.Client, optional ...string) (string, error) {
url := getAPIUrl("/me?fields=name")
if len(optional) == 1 {
url = optional[0]
}
resp, err := client.Get(url)
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var profile FacebookProfile
err = decoder.Decode(&profile)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", errors.New("Unathorized. Check token.")
}
return profile.Name, nil
}
// Get full user profile data
func getFullProfile(client *http.Client, optional ...string) (FacebookPublicProfile, error) {
url := getAPIUrl("/me?fields=name,locale,age_range,gender")
if len(optional) == 1 {
url = optional[0]
}
resp, err := client.Get(url)
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var profile FacebookPublicProfile
err = decoder.Decode(&profile)
if err != nil {
return FacebookPublicProfile{}, err
}
if resp.StatusCode != 200 {
return FacebookPublicProfile{}, errors.New("Unathorized. Check token.")
}
return profile, nil
}
// Get all friends
func getProfileFriendlist(client *http.Client, optional ...string) func() ([]FacebookProfile, error) {
next := getAPIUrl("/me/friends")
if len(optional) == 1 {
next = optional[0]
}
return func() ([]FacebookProfile, error) {
if next == "" {
return []FacebookProfile{}, errors.New("No data")
}
resp, err := client.Get(next)
defer resp.Body.Close()
if err != nil {
return []FacebookProfile{}, err
}
if resp.StatusCode != 200 {
return []FacebookProfile{}, errors.New("Unathorized. Check token.")
}
decoder := json.NewDecoder(resp.Body)
var data FacebookFriends
err = decoder.Decode(&data)
if err != nil {
return []FacebookProfile{}, err
}
emptyPaging := FacebookPaging{}
if data.Paging != emptyPaging {
next = data.Paging.Next
} else {
next = ""
}
return data.Data, nil
}
}