-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmedia.go
More file actions
195 lines (184 loc) · 4.97 KB
/
media.go
File metadata and controls
195 lines (184 loc) · 4.97 KB
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
package ig
import (
"errors"
"fmt"
"net/url"
"strconv"
"time"
"github.com/felipeinf/instago/igerrors"
)
const userTimelineGQLHash = "e7e2f4da4b02303f74f0841279e52d76"
// RankToken returns a token used when paginating user feeds (user id and client UUID).
func (c *Client) RankToken() string {
return fmt.Sprintf("%d_%s", c.userID(), c.uuids.UUID)
}
// UserMediasPaginatedV1 fetches one page from feed/user/{id}/; maxID is the next cursor from the previous call.
func (c *Client) UserMediasPaginatedV1(userID int64, amount int, maxID string) ([]Media, string, error) {
params := url.Values{}
if maxID != "" {
params.Set("max_id", maxID)
}
params.Set("count", strconv.Itoa(amount))
params.Set("rank_token", c.RankToken())
params.Set("ranked_content", "true")
res, err := c.PrivateRequest(PrivateRequestOpts{
Endpoint: fmt.Sprintf("feed/user/%d/", userID),
Params: params,
WithSignature: false,
})
if err != nil {
return nil, "", err
}
items, _ := res["items"].([]any)
out := make([]Media, 0, len(items))
for _, x := range items {
m, ok := x.(map[string]any)
if !ok {
continue
}
out = append(out, extractMediaV1(m))
}
next, _ := res["next_max_id"].(string)
if amount > 0 && len(out) > amount {
out = out[:amount]
}
return out, next, nil
}
// UserMediasV1 collects up to amount posts using the private REST feed, following next_max_id until done.
func (c *Client) UserMediasV1(userID int64, amount int) ([]Media, error) {
var all []Media
next := ""
page := 33
if amount > 0 && amount < page {
page = amount
}
for {
chunk, nxt, err := c.UserMediasPaginatedV1(userID, page, next)
if err != nil {
return nil, err
}
all = append(all, chunk...)
if nxt == "" {
break
}
if amount > 0 && len(all) >= amount {
break
}
next = nxt
time.Sleep(c.requestTimeout)
}
if amount > 0 && len(all) > amount {
all = all[:amount]
}
return all, nil
}
// UserMediasPaginatedGQL fetches one GraphQL timeline page; endCursor comes from the previous page's page_info.
func (c *Client) UserMediasPaginatedGQL(userID int64, pageAmount int, endCursor string) ([]Media, string, error) {
first := 50
if pageAmount > 0 && pageAmount < 50 {
first = pageAmount
}
vars := map[string]any{
"id": userID,
"first": first,
}
if endCursor != "" {
vars["after"] = endCursor
}
data, err := c.PublicGraphqlRequest(vars, userTimelineGQLHash)
if err != nil {
return nil, "", err
}
userNode, _ := data["user"].(map[string]any)
if userNode == nil {
return nil, "", fmt.Errorf("ig: gql user unavailable")
}
edge, _ := userNode["edge_owner_to_timeline_media"].(map[string]any)
if edge == nil {
return nil, "", nil
}
pageInfo, _ := edge["page_info"].(map[string]any)
edges, _ := edge["edges"].([]any)
next := ""
if pageInfo != nil {
next, _ = pageInfo["end_cursor"].(string)
}
out := make([]Media, 0, len(edges))
for _, x := range edges {
e, ok := x.(map[string]any)
if !ok {
continue
}
node, _ := e["node"].(map[string]any)
if node == nil {
continue
}
out = append(out, extractMediaGql(node))
}
if pageAmount > 0 && len(out) > pageAmount {
out = out[:pageAmount]
}
return out, next, nil
}
// UserMediasGQL paginates the public GraphQL user timeline until amount items or no next cursor. sleepSec is the delay between pages; 0 picks a short random sleep.
func (c *Client) UserMediasGQL(userID int64, amount int, sleepSec int) ([]Media, error) {
var all []Media
cursor := ""
for {
remain := 0
if amount > 0 {
remain = amount - len(all)
if remain <= 0 {
break
}
}
page, next, err := c.UserMediasPaginatedGQL(userID, remain, cursor)
if err != nil {
return nil, err
}
all = append(all, page...)
if next == "" || len(page) == 0 {
break
}
if amount > 0 && len(all) >= amount {
break
}
cursor = next
sleep := sleepSec
if sleep <= 0 {
sleep = 1 + c.rng.Intn(3)
}
time.Sleep(time.Duration(sleep) * time.Second)
}
if amount > 0 && len(all) > amount {
all = all[:amount]
}
return all, nil
}
func (c *Client) userMediasTryGQL(userID int64, amount int, sleepSec int) ([]Media, error) {
out, err := c.UserMediasGQL(userID, amount, sleepSec)
if err != nil {
var lr *igerrors.LoginRequired
if errors.As(err, &lr) && c.injectSessionIDToPublic() {
return c.UserMediasGQL(userID, amount, sleepSec)
}
return nil, err
}
return out, nil
}
// UserMediasWithSleep tries GraphQL pagination first (with sleepSec between pages), then UserMediasV1 if GraphQL fails for non-login reasons.
func (c *Client) UserMediasWithSleep(userID int64, amount int, sleepSec int) ([]Media, error) {
gql, err := c.userMediasTryGQL(userID, amount, sleepSec)
if err == nil {
return gql, nil
}
var lr *igerrors.LoginRequired
if errors.As(err, &lr) {
return nil, err
}
return c.UserMediasV1(userID, amount)
}
// UserMedias is the high-level helper: GraphQL with automatic backoff, then REST fallback.
func (c *Client) UserMedias(userID int64, amount int) ([]Media, error) {
return c.UserMediasWithSleep(userID, amount, 0)
}