This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.go
127 lines (105 loc) · 3.5 KB
/
users.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
package gostagram
import (
"fmt"
"github.com/mitchellh/mapstructure"
)
// UserDetailed struct represents user profile information.
type UserDetailed struct {
Id string
Bio string
Website string
Username string
FullName string `mapstructure:"full_name"`
ProfilePicture string `mapstructure:"profile_picture"`
Counts struct {
Media int
Follows int
FollowedBy int `mapstructure:"followed_by"`
}
}
// User struct represents main user information,
// most of the time, returned by comments or media
// metadata.
type User struct {
Id string
Type string
Username string
LastName string `mapstructure:"last_name"`
FirstName string `mapstructure:"first_name"`
ProfilePicture string `mapstructure:"profile_picture"`
}
func (c Client) getUser(uri string) (*UserDetailed, error) {
tmp, _, err := c.get(uri)
if err != nil {
return nil, err
}
userDetailedMap := (*tmp).(map[string]interface{})
var userDetailed UserDetailed
if err = mapstructure.Decode(userDetailedMap, &userDetailed); err != nil {
return nil, err
}
return &userDetailed, nil
}
func (c Client) getUsers(uri string) ([]*User, error) {
tmp, _, err := c.get(uri)
if err != nil {
return nil, err
}
tmpUsers := (*tmp).([]interface{})
var users []*User
for _, tmpUser := range tmpUsers {
var user User
if err := mapstructure.Decode(tmpUser, &user); err != nil {
return nil, err
}
users = append(users, &user)
}
return users, nil
}
// Get current logged user data,
// for more information go to
// https://www.instagram.com/developer/endpoints/users/#get_users_self
func (c Client) GetCurrentUser() (*UserDetailed, error) {
return c.getUser(fmt.Sprintf("%susers/self/?access_token=%s", apiUrl, c.access_token))
}
// Get other user data,
// got more information go to
// https://www.instagram.com/developer/endpoints/users/#get_users
func (c Client) GetUser(id string) (*UserDetailed, error) {
return c.getUser(fmt.Sprintf("%susers/%s/?access_token=%s", apiUrl, id, c.access_token))
}
// get a list of users from the query,
// for more information go to
// https://www.instagram.com/developer/endpoints/users/#get_users_search
func (c Client) SearchUsers(query string, parameters Parameters) ([]*User, error) {
tmp := "%susers/search?q=%s&access_token=%s"
if parameters != nil {
if parameters["count"] != "" {
tmp += fmt.Sprintf("&count=%s", parameters["count"])
}
}
return c.getUsers(fmt.Sprintf(tmp,
apiUrl, query, c.access_token))
}
// Get a list of users that current
// user follows, for more information go to
// https://www.instagram.com/developer/endpoints/relationships/#get_users_follows
func (c Client) GetCurrentUserFollows() ([]*User, error) {
return c.getUsers(fmt.Sprintf("%susers/self/follows?access_token=%s",
apiUrl, c.access_token))
}
// Get a list of users, that current user
// is followed, for more information go to
// https://www.instagram.com/developer/endpoints/relationships/#get_users_followed_by
func (c Client) GetCurrentUserFollowedBy() ([]*User, error) {
return c.getUsers(fmt.Sprintf("%susers/self/followed-by?access_token=%s",
apiUrl, c.access_token))
}
// Get a list of users who have requested
// this user's permission to follow,
// for more information go to
// https://www.instagram.com/developer/endpoints/relationships/#get_incoming_requests
func (c Client) GetCurrentUserRequestedBy() ([]*User, error) {
return c.getUsers(fmt.Sprintf("%susers/self/requested-by?access_token=%s",
apiUrl, c.access_token))
}