forked from ahmdrz/goinsta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtags.go
140 lines (129 loc) · 3.18 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
package goinsta
import (
"encoding/json"
"fmt"
)
// Hashtag is used for getting the media that matches a hashtag on instagram.
type Hashtag struct {
inst *Instagram
err error
Name string `json:"name"`
Sections []struct {
LayoutType string `json:"layout_type"`
LayoutContent struct {
// F*ck you instagram.
// Why you do this f*cking horribly structure?!?
// Media []Media IS EASY. CHECK IT!
Medias []struct {
Item Item `json:"media"`
} `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"`
} `json:"sections"`
MediaCount int `json:"media_count"`
ID int64 `json:"id"`
MoreAvailable bool `json:"more_available"`
NextID string `json:"next_max_id"`
NextPage int `json:"next_page"`
NextMediaIds []int64 `json:"next_media_ids"`
AutoLoadMoreEnabled bool `json:"auto_load_more_enabled"`
Status string `json:"status"`
}
func (h *Hashtag) setValues() {
for i := range h.Sections {
for j := range h.Sections[i].LayoutContent.Medias {
m := &FeedMedia{
inst: h.inst,
}
setToItem(&h.Sections[i].LayoutContent.Medias[j].Item, m)
}
}
}
// NewHashtag returns initialised hashtag structure
// Name parameter is hashtag name
func (inst *Instagram) NewHashtag(name string) *Hashtag {
return &Hashtag{
inst: inst,
Name: name,
}
}
// Sync updates Hashtag information preparing it to Next call.
func (h *Hashtag) Sync() error {
insta := h.inst
body, err := insta.sendSimpleRequest(urlTagSync, h.Name)
if err == nil {
var resp struct {
Name string `json:"name"`
ID int64 `json:"id"`
MediaCount int `json:"media_count"`
}
err = json.Unmarshal(body, &resp)
if err == nil {
h.Name = resp.Name
h.ID = resp.ID
h.MediaCount = resp.MediaCount
h.setValues()
}
}
return err
}
// Next paginates over hashtag pages (xd).
func (h *Hashtag) Next() bool {
if h.err != nil {
return false
}
insta := h.inst
name := h.Name
body, err := insta.sendRequest(
&reqOptions{
Query: map[string]string{
"max_id": h.NextID,
"rank_token": insta.rankToken,
"page": fmt.Sprintf("%d", h.NextPage),
},
Endpoint: fmt.Sprintf(urlTagContent, name),
IsPost: false,
},
)
if err == nil {
ht := &Hashtag{}
err = json.Unmarshal(body, ht)
if err == nil {
*h = *ht
h.inst = insta
h.Name = name
if !h.MoreAvailable {
h.err = ErrNoMore
}
h.setValues()
return true
}
}
h.err = err
return false
}
// Error returns hashtag error
func (h *Hashtag) Error() error {
return h.err
}
// Stories returns hashtag stories.
func (h *Hashtag) Stories() (*StoryMedia, error) {
body, err := h.inst.sendSimpleRequest(
urlTagStories, h.Name,
)
if err == nil {
var resp struct {
Story StoryMedia `json:"story"`
Status string `json:"status"`
}
err = json.Unmarshal(body, &resp)
return &resp.Story, err
}
return nil, err
}