-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathstories.go
275 lines (248 loc) · 7.44 KB
/
stories.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
package goinsta
import (
"encoding/json"
"fmt"
"time"
)
// StoryReelMention represent story reel mention
type StoryReelMention struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Z int `json:"z"`
Width float64 `json:"width"`
Height float64 `json:"height"`
Rotation float64 `json:"rotation"`
IsPinned int `json:"is_pinned"`
IsHidden int `json:"is_hidden"`
IsSticker int `json:"is_sticker"`
IsFBSticker int `json:"is_fb_sticker"`
User User
DisplayType string `json:"display_type"`
}
// StoryCTA represent story cta
type StoryCTA struct {
Links []struct {
LinkType int `json:"linkType"`
WebURI string `json:"webUri"`
AndroidClass string `json:"androidClass"`
Package string `json:"package"`
DeeplinkURI string `json:"deeplinkUri"`
CallToActionTitle string `json:"callToActionTitle"`
RedirectURI interface{} `json:"redirectUri"`
LeadGenFormID string `json:"leadGenFormId"`
IgUserID string `json:"igUserId"`
AppInstallObjectiveInvalidationBehavior interface{} `json:"appInstallObjectiveInvalidationBehavior"`
} `json:"links"`
}
// StoryMedia is the struct that handles the information from the methods to get info about Stories.
type StoryMedia struct {
Reel Reel `json:"reel"`
Broadcast *Broadcast `json:"broadcast"`
Broadcasts []*Broadcast `json:"broadcasts"`
Status string `json:"status"`
}
// Reel represents a single user's story collection.
// Every user has one reel, and one reel can contain many story items
type Reel struct {
insta *Instagram
ID interface{} `json:"id"`
Items []*Item `json:"items"`
MediaCount int `json:"media_count"`
MediaIDs []int64 `json:"media_ids"`
Muted bool `json:"muted"`
LatestReelMedia int64 `json:"latest_reel_media"`
LatestBestiesReelMedia float64 `json:"latest_besties_reel_media"`
ExpiringAt float64 `json:"expiring_at"`
Seen float64 `json:"seen"`
SeenRankedPosition int `json:"seen_ranked_position"`
CanReply bool `json:"can_reply"`
CanGifQuickReply bool `json:"can_gif_quick_reply"`
ClientPrefetchScore float64 `json:"client_prefetch_score"`
Title string `json:"title"`
CanReshare bool `json:"can_reshare"`
ReelType string `json:"reel_type"`
ReelMentions []string `json:"reel_mentions"`
PrefetchCount int `json:"prefetch_count"`
// this field can be int or bool
HasBestiesMedia interface{} `json:"has_besties_media"`
HasPrideMedia bool `json:"has_pride_media"`
HasVideo bool `json:"has_video"`
IsCacheable bool `json:"is_cacheable"`
IsSensitiveVerticalAd bool `json:"is_sensitive_vertical_ad"`
RankedPosition int `json:"ranked_position"`
RankerScores struct {
Fp float64 `json:"fp"`
Ptap float64 `json:"ptap"`
Vm float64 `json:"vm"`
} `json:"ranker_scores"`
StoryRankingToken string `json:"story_ranking_token"`
FaceFilterNuxVersion int `json:"face_filter_nux_version"`
HasNewNuxStory bool `json:"has_new_nux_story"`
User User `json:"user"`
}
// Stories will fetch a user's stories.
func (user *User) Stories() (*StoryMedia, error) {
return user.insta.fetchStories(user.ID)
}
// Highlights will fetch a user's highlights.
func (user *User) Highlights() ([]*Reel, error) {
data, err := getSupCap()
if err != nil {
return nil, err
}
body, _, err := user.insta.sendRequest(
&reqOptions{
Endpoint: fmt.Sprintf(urlUserHighlights, user.ID),
Query: map[string]string{"supported_capabilities_new": data},
},
)
if err != nil {
return nil, err
}
tray := &Tray{}
err = json.Unmarshal(body, &tray)
if err != nil {
return nil, err
}
tray.set(user.insta)
return tray.Stories, nil
}
// Deletes ALL user's instagram stories.
// If you want to remove a single story, pick one from StoryMedia.Items, and
// call Item.Delete()
//
// See example: examples/media/deleteStories.go
func (media *Reel) Delete() error {
// TODO: update to reel
for _, item := range media.Items {
err := item.Delete()
if err != nil {
return err
}
time.Sleep(200 * time.Millisecond)
}
return nil
}
func (media *StoryMedia) setValues(insta *Instagram) {
media.Reel.setValues(insta)
if media.Broadcast != nil {
media.Broadcast.setValues(insta)
}
for _, br := range media.Broadcasts {
br.setValues(insta)
}
}
func (media *Reel) setValues(insta *Instagram) {
media.insta = insta
media.User.insta = insta
for _, i := range media.Items {
i.insta = insta
i.User.insta = insta
}
}
// Seen marks story as seen.
/*
func (media *StoryMedia) Seen() error {
insta := media.inst
data, err := insta.prepareData(
map[string]interface{}{
"container_module": "feed_timeline",
"live_vods_skipped": "",
"nuxes_skipped": "",
"nuxes": "",
"reels": "", // TODO xd
"live_vods": "",
"reel_media_skipped": "",
},
)
if err == nil {
_, _, err = insta.sendRequest(
&reqOptions{
Endpoint: urlMediaSeen, // reel=1&live_vod=0
Query: generateSignature(data),
IsPost: true,
UseV2: true,
},
)
}
return err
}
*/
type trayRequest struct {
Name string `json:"name"`
Value string `json:"value"`
}
func (insta *Instagram) fetchStories(id int64) (*StoryMedia, error) {
supCap, err := getSupCap()
if err != nil {
return nil, err
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: fmt.Sprintf(urlUserStories, id),
Query: map[string]string{"supported_capabilities_new": supCap},
},
)
if err != nil {
return nil, err
}
m := &StoryMedia{}
err = json.Unmarshal(body, m)
if err != nil {
return nil, err
}
m.setValues(insta)
return m, nil
}
// Sync function is used when Highlights must be sync.
// Highlight must be sync when User.Highlights does not return any object inside StoryMedia slice.
//
// This function does NOT update Stories items.
//
// This function updates (fetches) StoryMedia.Items
func (media *Reel) Sync() error {
if media.ReelType != "highlight_reel" {
return ErrNotHighlight
}
insta := media.insta
supCap, err := getSupCap()
if err != nil {
return err
}
id := media.ID.(string)
data, err := json.Marshal(
map[string]interface{}{
"exclude_media_ids": "[]",
"supported_capabilities_new": supCap,
"source": "reel_feed_timeline",
"_uid": toString(insta.Account.ID),
"_uuid": insta.uuid,
"user_ids": []string{id},
},
)
if err != nil {
return err
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlReelMedia,
IsPost: true,
Query: generateSignature(data),
},
)
if err != nil {
return err
}
resp := trayResp{}
err = json.Unmarshal(body, &resp)
if err != nil {
return err
}
m, ok := resp.Reels[id]
if ok {
*media = m
media.setValues(insta)
return nil
}
return fmt.Errorf("cannot find %s structure in response", id)
}