This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
/
feeds.go
154 lines (138 loc) · 3.34 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
package goinsta
import (
"encoding/json"
"fmt"
)
// Feed is the object for all feed endpoints.
type Feed struct {
inst *Instagram
}
// newFeed creates new Feed structure
func newFeed(inst *Instagram) *Feed {
return &Feed{
inst: inst,
}
}
// Feed search by locationID
func (feed *Feed) LocationID(locationID int64) (*FeedLocation, error) {
insta := feed.inst
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)
return res, err
}
// 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
//
// (sorry for returning FeedTag. See #FeedTag)
func (feed *Feed) Tags(tag string) (*FeedTag, error) {
insta := feed.inst
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{}
err = json.Unmarshal(body, res)
if err != nil {
return nil, err
}
res.name = tag
res.inst = feed.inst
res.setValues()
return res, nil
}
// FeedTag is the struct that fits the structure returned by instagram on TagSearch.
type FeedTag struct {
inst *Instagram
err error
name string
RankedItems []Item `json:"ranked_items"`
Images []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].media = &FeedMedia{
inst: ft.inst,
NextID: ft.RankedItems[i].ID,
}
}
for i := range ft.Images {
ft.Images[i].media = &FeedMedia{
inst: ft.inst,
NextID: ft.Images[i].ID,
}
}
}
// Next paginates over hashtag feed.
func (ft *FeedTag) Next() bool {
if ft.err != nil {
return false
}
insta := ft.inst
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{}
err = json.Unmarshal(body, newFT)
if err == nil {
*ft = *newFT
ft.inst = 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
}