-
Notifications
You must be signed in to change notification settings - Fork 58
/
feeds.go
176 lines (159 loc) · 4 KB
/
feeds.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
package goinsta
import (
"encoding/json"
"fmt"
)
// Feed is the object for all feed endpoints.
type Feed struct {
insta *Instagram
}
// newFeed creates new Feed structure
func newFeed(insta *Instagram) *Feed {
return &Feed{
insta: insta,
}
}
// Feed search by locationID
func (feed *Feed) LocationID(locationID int64) (*FeedLocation, error) {
insta := feed.insta
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: fmt.Sprintf(urlFeedLocationID, locationID),
Query: map[string]string{
"rank_token": insta.rankToken,
"ranked_content": "true",
},
},
)
if err != nil {
return nil, err
}
res := &FeedLocation{}
err = json.Unmarshal(body, res)
if err != nil {
return nil, err
}
for _, i := range res.RankedItems {
i.insta = insta
}
for _, i := range res.Items {
i.insta = insta
}
return res, nil
}
// FeedLocation is the struct that fits the structure returned by instagram on LocationID search.
type FeedLocation struct {
RankedItems []*Item `json:"ranked_items"`
Items []*Item `json:"items"`
NumResults int `json:"num_results"`
NextID string `json:"next_max_id"`
MoreAvailable bool `json:"more_available"`
AutoLoadMoreEnabled bool `json:"auto_load_more_enabled"`
MediaCount int `json:"media_count"`
Location Location `json:"location"`
Status string `json:"status"`
}
// Tags search by Tag in user Feed
//
// This method does not perform a search for a tag, but directly queries the
// feed items for the specified Tag. The preffered way would be to search
// for the tag, call TopSearchItem.RegisterClick(), and then fetch the feed.
//
// This method uses an older endpoint, although it still seems to work.
// The preffered way to fetch Hashtag feeds is by using the Hashtag struct.
// This can be obtained from insta.NewHashtag(tag), or insta.Searchbar.SearchHashtag(tag)
//
func (feed *Feed) Tags(tag string) (*FeedTag, error) {
insta := feed.insta
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: fmt.Sprintf(urlFeedTag, tag),
Query: map[string]string{
"rank_token": insta.rankToken,
"ranked_content": "true",
},
},
)
if err != nil {
return nil, err
}
res := &FeedTag{
insta: insta,
}
err = json.Unmarshal(body, res)
if err != nil {
return nil, err
}
res.name = tag
res.setValues()
return res, nil
}
// FeedTag is the struct that fits the structure returned by instagram on TagSearch.
type FeedTag struct {
insta *Instagram
err error
name string
RankedItems []*Item `json:"ranked_items"`
Items []*Item `json:"items"`
NumResults int `json:"num_results"`
NextID string `json:"next_max_id"`
MoreAvailable bool `json:"more_available"`
AutoLoadMoreEnabled bool `json:"auto_load_more_enabled"`
Story StoryMedia `json:"story"`
Status string `json:"status"`
}
func (ft *FeedTag) setValues() {
for i := range ft.RankedItems {
ft.RankedItems[i].insta = ft.insta
ft.RankedItems[i].media = &FeedMedia{
insta: ft.insta,
NextID: ft.RankedItems[i].ID,
}
}
for i := range ft.Items {
ft.Items[i].insta = ft.insta
ft.Items[i].media = &FeedMedia{
insta: ft.insta,
NextID: ft.Items[i].ID,
}
}
}
// Next paginates over hashtag feed.
func (ft *FeedTag) Next() bool {
if ft.err != nil {
return false
}
insta := ft.insta
name := ft.name
body, _, err := insta.sendRequest(
&reqOptions{
Query: map[string]string{
"max_id": ft.NextID,
"rank_token": insta.rankToken,
},
Endpoint: fmt.Sprintf(urlFeedTag, name),
},
)
if err == nil {
newFT := &FeedTag{
insta: insta,
}
err = json.Unmarshal(body, newFT)
if err == nil {
*ft = *newFT
ft.insta = insta
ft.name = name
if !ft.MoreAvailable {
ft.err = ErrNoMore
}
ft.setValues()
return true
}
}
ft.err = err
return false
}
// Error returns hashtag error
func (ft *FeedTag) Error() error {
return ft.err
}