-
Notifications
You must be signed in to change notification settings - Fork 58
/
hashtags.go
277 lines (242 loc) · 6.44 KB
/
hashtags.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package goinsta
import (
"encoding/json"
"fmt"
)
// Hashtag is used for getting the media that matches a hashtag on instagram.
type Hashtag struct {
insta *Instagram
err error
Name string `json:"name"`
ID int64 `json:"id"`
MediaCount int `json:"media_count"`
FormattedMediaCount string `json:"formatted_media_count"`
FollowStatus interface{} `json:"follow_status"`
Subtitle string `json:"subtitle"`
Description string `json:"description"`
Following interface{} `json:"following"`
AllowFollowing interface{} `json:"allow_following"`
AllowMutingStory interface{} `json:"allow_muting_story"`
ProfilePicURL interface{} `json:"profile_pic_url"`
NonViolating interface{} `json:"non_violating"`
RelatedTags interface{} `json:"related_tags"`
DebugInfo interface{} `json:"debug_info"`
// All Top Items
Items []*Item
// All ItemsRecent Items
ItemsRecent []*Item
Story *StoryMedia
NumResults int
// Sections will always contain the last fetched sections, regardless of tab
Sections []hashtagSection `json:"sections"`
PageInfo map[string]hashtagPageInfo
AutoLoadMoreEnabled bool `json:"auto_load_more_enabled"`
MoreAvailable bool `json:"more_available"`
NextID string `json:"next_max_id"`
NextPage int `json:"next_page"`
NextMediaIds []int64 `json:"next_media_ids"`
Status string `json:"status"`
}
type hashtagSection struct {
LayoutType string `json:"layout_type"`
LayoutContent struct {
// TODO: misses onebyoneitem etc., like discover page
FillItems []mediaItem `json:"fill_items"`
Medias []mediaItem `json:"medias"`
} `json:"layout_content"`
FeedType string `json:"feed_type"`
ExploreItemInfo struct {
NumColumns int `json:"num_columns"`
TotalNumColumns int `json:"total_num_columns"`
AspectRatio float32 `json:"aspect_ratio"`
Autoplay bool `json:"autoplay"`
} `json:"explore_item_info"`
}
type mediaItem struct {
Item *Item `json:"media"`
}
type hashtagPageInfo struct {
MoreAvailable bool `json:"more_available"`
NextID string `json:"next_max_id"`
NextPage int `json:"next_page"`
NextMediaIds []int64 `json:"next_media_ids"`
Status string `json:"status"`
}
func (h *Hashtag) setValues() {
if h.PageInfo == nil {
h.PageInfo = make(map[string]hashtagPageInfo)
}
for _, s := range h.Sections {
for _, m := range s.LayoutContent.Medias {
setToItem(m.Item, h)
}
for _, m := range s.LayoutContent.FillItems {
setToItem(m.Item, h)
}
}
}
// Delete only a place holder, does nothing
func (h *Hashtag) Delete() error {
return nil
}
func (h *Hashtag) GetNextID() string {
return ""
}
// NewHashtag returns initialised hashtag structure
// Name parameter is hashtag name
func (insta *Instagram) NewHashtag(name string) *Hashtag {
return &Hashtag{
insta: insta,
Name: name,
PageInfo: make(map[string]hashtagPageInfo),
}
}
// Sync wraps Hashtag.Info()
func (h *Hashtag) Sync() error {
return h.Info()
}
// Info updates Hashtag information
func (h *Hashtag) Info() error {
insta := h.insta
body, err := insta.sendSimpleRequest(urlTagInfo, h.Name)
if err != nil {
return err
}
err = json.Unmarshal(body, h)
return err
}
// Next paginates over hashtag top pages.
func (h *Hashtag) Next(p ...interface{}) bool {
return h.next("top")
}
// NextRecent paginates over hashtag top recent pages.
func (h *Hashtag) NextRecent() bool {
return h.next("recent")
}
func (h *Hashtag) next(tab string) bool {
pageInfo, ok := h.PageInfo[tab]
if h.err != nil && h.err != ErrNoMore {
return false
} else if h.err == ErrNoMore && ok && !pageInfo.MoreAvailable {
return false
}
if tab != "top" && tab != "recent" && tab != "clips" {
h.err = ErrInvalidTab
return false
}
insta := h.insta
name := h.Name
query := map[string]string{
"tab": tab,
"_uuid": insta.uuid,
"include_persistent": "false",
"rank_token": insta.rankToken,
}
if ok {
nextMediaIds, err := json.Marshal(pageInfo.NextMediaIds)
if err != nil {
h.err = err
return false
}
query["max_id"] = pageInfo.NextID
query["page"] = toString(pageInfo.NextPage)
query["next_media_ids"] = string(nextMediaIds)
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: fmt.Sprintf(urlTagContent, name),
IsPost: true,
Query: query,
},
)
if err != nil {
h.err = err
return false
}
res := &Hashtag{}
if err := json.Unmarshal(body, res); err != nil {
h.err = err
return false
}
h.fillItems(res, tab)
if !h.MoreAvailable {
h.err = ErrNoMore
return false
}
return true
}
func (h *Hashtag) fillItems(res *Hashtag, tab string) {
h.AutoLoadMoreEnabled = res.AutoLoadMoreEnabled
h.NextID = res.NextID
h.MoreAvailable = res.MoreAvailable
h.NextPage = res.NextPage
h.Status = res.Status
h.Sections = res.Sections
h.PageInfo[tab] = hashtagPageInfo{
MoreAvailable: res.MoreAvailable,
NextID: res.NextID,
NextPage: res.NextPage,
NextMediaIds: res.NextMediaIds,
}
h.setValues()
count := 0
for _, s := range res.Sections {
for _, m := range s.LayoutContent.Medias {
count += 1
if tab == "top" {
h.Items = append(h.Items, m.Item)
} else if tab == "recent" {
h.ItemsRecent = append(h.ItemsRecent, m.Item)
}
}
for _, m := range s.LayoutContent.FillItems {
count += 1
if tab == "top" {
h.Items = append(h.Items, m.Item)
} else if tab == "recent" {
h.ItemsRecent = append(h.ItemsRecent, m.Item)
}
}
}
h.NumResults = count
}
// Latest will return the last fetched items.
func (h *Hashtag) Latest() []*Item {
var res []*Item
for _, s := range h.Sections {
for _, m := range s.LayoutContent.Medias {
res = append(res, m.Item)
}
}
return res
}
// Error returns hashtag error
func (h *Hashtag) Error() error {
return h.err
}
// Clears the Hashtag.err error
func (h *Hashtag) ClearError() {
h.err = nil
}
func (media *Hashtag) getInsta() *Instagram {
return media.insta
}
// Stories returns hashtag stories.
func (h *Hashtag) Stories() error {
body, err := h.insta.sendSimpleRequest(
urlTagStories, h.Name,
)
if err != nil {
return err
}
var resp struct {
Story *StoryMedia `json:"story"`
Status string `json:"status"`
}
err = json.Unmarshal(body, &resp)
if err != nil {
return err
}
h.Story = resp.Story
return err
}