-
Notifications
You must be signed in to change notification settings - Fork 14
/
rapidAPIInstagram85.go
232 lines (194 loc) · 5.26 KB
/
rapidAPIInstagram85.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
package instagram
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/coretrix/hitrix/service/component/config"
)
// RapidAPIInstagram85 https://rapidapi.com/premium-apis-premium-apis-default/api/instagram85/
type RapidAPIInstagram85 struct {
client *http.Client
apiKey string
apiHost string
apiBaseURL string
providerName string
}
func NewRapidAPIInstagram85(configService config.IConfig) (IProvider, error) {
return &RapidAPIInstagram85{
client: &http.Client{},
apiKey: configService.MustString("instagram.api.rapid_api_token"),
apiHost: "instagram85.p.rapidapi.com",
apiBaseURL: "https://instagram85.p.rapidapi.com",
providerName: "RapidAPIInstagram85",
}, nil
}
func (i *RapidAPIInstagram85) GetName() string {
return i.providerName
}
func (i *RapidAPIInstagram85) APIKey() string {
return i.apiKey
}
func (i *RapidAPIInstagram85) GetAccount(account string) (*Account, error) {
response := struct {
Data struct {
ID int64 `json:"id"`
Fullname string `json:"full_name"`
Bio string `json:"biography"`
Website string `json:"website"`
Picture struct {
HD string `json:"hd"`
} `json:"profile_picture"`
Figures struct {
Posts int64 `json:"posts"`
Followers int64 `json:"followers"`
Followings int64 `json:"followings"`
} `json:"figures"`
IsPrivate bool `json:"is_private"`
} `json:"data"`
Code int64 `json:"code"`
}{}
res, err := sendRapidRequest(i, fmt.Sprintf("%v/account/%v/info", i.apiBaseURL, account))
if err != nil {
return nil, err
}
if err = json.Unmarshal(res, &response); err != nil {
errResponse := struct {
Code int64 `json:"code"`
}{}
if err2 := json.Unmarshal(res, &errResponse); err2 != nil {
return nil, err2
}
if errResponse.Code == 302 {
return nil, errors.New("internal_server_error status code 302")
}
return nil, err
}
if response.Code == 404 {
return nil, errors.New("internal_server_error status code 404")
} else if response.Code != 200 {
return nil, errors.New("internal_server_error status code not 200")
}
return &Account{
AccountID: response.Data.ID,
FullName: response.Data.Fullname,
Bio: response.Data.Bio,
Posts: response.Data.Figures.Posts,
Followers: response.Data.Figures.Followers,
Following: response.Data.Figures.Followings,
Picture: response.Data.Picture.HD,
IsPrivate: response.Data.IsPrivate,
Website: response.Data.Website,
}, nil
}
func (i *RapidAPIInstagram85) GetFeed(accountID int64, nextPageToken string) ([]*Post, string, error) {
response := struct {
Data []RapidAPIInstagram85Post `json:"data"`
Meta struct {
HasNext bool `json:"has_next"`
NextPage string `json:"next_page"`
} `json:"meta"`
Code int64 `json:"code"`
}{}
instagramURL := fmt.Sprintf("%s/account/%d/feed", i.apiBaseURL, accountID)
if nextPageToken != "" {
instagramURL += fmt.Sprintf("?pageId=%s", nextPageToken)
}
log.Println(instagramURL)
res, err := sendRapidRequest(i, instagramURL)
if err != nil {
return nil, "", err
}
if err = json.Unmarshal(res, &response); err != nil {
return nil, "", err
}
if response.Code == 404 {
return nil, "", errors.New("internal_server_error status code 404")
} else if response.Code != 200 {
return nil, "", errors.New("internal_server_error status code not 200")
}
var posts []*Post
for i := range response.Data {
postData := response.Data[i]
if postData.Type != "image" && postData.Type != "sidecar" {
continue
}
if post := postData.ToPost(); post != nil {
posts = append(posts, post)
}
}
nextPageToken = ""
if response.Meta.HasNext {
nextPageToken = response.Meta.NextPage
}
return posts, nextPageToken, nil
}
func (i *RapidAPIInstagram85) APIHost() string {
return i.apiHost
}
func (i *RapidAPIInstagram85) HTTPClient() *http.Client {
return i.client
}
func (p RapidAPIInstagram85Post) ToPost() *Post {
post := &Post{
ID: p.ID,
Title: p.Caption,
CreatedAt: p.CreatedTime.Unix,
}
var images []string
if p.Type == "image" {
images = []string{
p.Images.Original.High,
}
} else {
for j := range p.Sidecar {
if p.Sidecar[j].Type == "image" {
images = append(images, p.Sidecar[j].Images.Original.High)
}
}
}
post.Images = images
return post
}
type RapidAPIInstagram85Post struct {
ID string `json:"id"`
CreatedTime struct {
Unix int64 `json:"unix"`
} `json:"created_time"`
Caption string `json:"caption"`
Type string `json:"type"`
Images struct {
Original struct {
High string `json:"high"`
} `json:"original"`
} `json:"images"`
Videos struct {
Standard string `json:"standard"`
} `json:"videos"`
Sidecar []RapidAPIInstagram85Post `json:"sidecar"`
}
type rapidAPIProvider interface {
HTTPClient() *http.Client
APIKey() string
APIHost() string
}
func sendRapidRequest(provider rapidAPIProvider, url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("x-rapidapi-key", provider.APIKey())
req.Header.Add("x-rapidapi-host", provider.APIHost())
res, err := provider.HTTPClient().Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, nil
}