forked from ahmdrz/goinsta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
location.go
109 lines (92 loc) · 2.48 KB
/
location.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
package goinsta
import (
"encoding/json"
"fmt"
)
type LocationInstance struct {
inst *Instagram
}
func newLocation(inst *Instagram) *LocationInstance {
return &LocationInstance{inst: inst}
}
type LayoutSection struct {
LayoutType string `json:"layout_type"`
LayoutContent struct {
Medias []struct {
Media 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 int `json:"aspect_ratio"`
Autoplay bool `json:"autoplay"`
} `json:"explore_item_info"`
}
type Section struct {
inst *Instagram
err error
endpoint string
tab string
Sections []LayoutSection `json:"sections"`
MoreAvailable bool `json:"more_available"`
NextPage int `json:"next_page"`
NextMediaIds []int64 `json:"next_media_ids"`
NextID string `json:"next_max_id"`
Status string `json:"status"`
}
// Next allows to paginate after calling:
// Locations.Feeds()
// returns false when list reach the end.
func (section *Section) Next() bool {
if section.err != nil {
return false
}
insta := section.inst
endpoint := section.endpoint
body, err := insta.sendRequest(
&reqOptions{
Endpoint: endpoint,
Query: map[string]string{
"max_id": section.NextID,
"rank_token": insta.rankToken,
"tab": section.tab,
"_csrftoken": insta.token,
"_uuid": insta.uuid,
},
IsPost: true,
},
)
if err == nil {
newSection := Section{}
err = json.Unmarshal(body, &newSection)
if err == nil {
newSection.tab = section.tab
newSection.inst = section.inst
newSection.endpoint = section.endpoint
*section = newSection
if section.NextID == "" {
section.err = ErrNoMore
return false
}
return true
}
}
section.err = err
return false
}
func (section *Section) Error() error {
return section.err
}
// SetNextID sets specific NextID to section structure, to continue pagination
func (s *Section) SetNextID(NextID string) {
s.NextID = NextID
}
//Feeds creates a starting point for fetching location feed.
//Use .Next() for pagination.
//Tab can be "recent" or "top" according to instagram sections
func (l *LocationInstance) Feeds(locationID int64, tab string) *Section {
endpoint := fmt.Sprintf(urlFeedLocations, locationID)
return &Section{inst: l.inst, endpoint: endpoint, tab: tab}
}