-
Notifications
You must be signed in to change notification settings - Fork 58
/
activity.go
172 lines (159 loc) · 4.6 KB
/
activity.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
package goinsta
import (
"encoding/json"
"fmt"
)
// Activity is the recent activity menu.
//
// See example: examples/activity/recent.go
type Activity struct {
insta *Instagram
err error
// Ad is every column of Activity section
Ad struct {
Items []struct {
// User User `json:"user"`
Algorithm string `json:"algorithm"`
SocialContext string `json:"social_context"`
Icon string `json:"icon"`
Caption string `json:"caption"`
MediaIds []interface{} `json:"media_ids"`
ThumbnailUrls []interface{} `json:"thumbnail_urls"`
LargeUrls []interface{} `json:"large_urls"`
MediaInfos []interface{} `json:"media_infos"`
Value float64 `json:"value"`
IsNewSuggestion bool `json:"is_new_suggestion"`
} `json:"items"`
MoreAvailable bool `json:"more_available"`
} `json:"aymf"`
Counts struct {
Campaign int `json:"campaign_notification"`
CommentLikes int `json:"comment_likes"`
Comments int `json:"comments"`
Fundraiser int `json:"fundraiser"`
Likes int `json:"likes"`
NewPosts int `json:"new_posts"`
PhotosOfYou int `json:"photos_of_you"`
Relationships int `json:"relationships"`
Requests int `json:"requests"`
Shopping int `json:"shopping_notification"`
UserTags int `json:"usertags"`
} `json:"counts"`
FriendRequestStories []interface{} `json:"friend_request_stories"`
NewStories []RecentItems `json:"new_stories"`
OldStories []RecentItems `json:"old_stories"`
ContinuationToken int64 `json:"continuation_token"`
Subscription interface{} `json:"subscription"`
NextID string `json:"next_max_id"`
LastChecked float64 `json:"last_checked"`
FirstRecTs float64 `json:"pagination_first_record_timestamp"`
Status string `json:"status"`
}
type RecentItems struct {
Type int `json:"type"`
StoryType int `json:"story_type"`
Args struct {
Text string `json:"text"`
RichText string `json:"rich_text"`
IconUrl string `json:"icon_url"`
Links []struct {
Start int `json:"start"`
End int `json:"end"`
Type string `json:"type"`
ID interface{} `json:"id"`
} `json:"links"`
InlineFollow struct {
UserInfo User `json:"user_info"`
Following bool `json:"following"`
OutgoingRequest bool `json:"outgoing_request"`
} `json:"inline_follow"`
Actions []string `json:"actions"`
AfCandidateId int `json:"af_candidate_id"`
ProfileID int64 `json:"profile_id"`
ProfileImage string `json:"profile_image"`
Timestamp float64 `json:"timestamp"`
Tuuid string `json:"tuuid"`
Clicked bool `json:"clicked"`
ProfileName string `json:"profile_name"`
LatestReelMedia int64 `json:"latest_reel_media"`
Destination string `json:"destination"`
Extra interface{} `json:"extra"`
} `json:"args"`
Counts struct{} `json:"counts"`
Pk string `json:"pk"`
}
func (act *Activity) Error() error {
return act.err
}
// Next function allows pagination over notifications.
//
// See example: examples/activity/recent.go
func (act *Activity) Next() bool {
if act.err != nil {
return false
}
var first bool
if act.Status == "" {
first = true
}
query := map[string]string{
"mark_as_seen": "false",
"timezone_offset": timeOffset,
}
if act.NextID != "" {
query["max_id"] = act.NextID
query["last_checked"] = fmt.Sprintf("%f", act.LastChecked)
query["pagination_first_record_timestamp"] = fmt.Sprintf("%f", act.FirstRecTs)
}
insta := act.insta
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlActivityRecent,
Query: query,
},
)
if err != nil {
act.err = err
return false
}
act2 := Activity{}
err = json.Unmarshal(body, &act2)
if err == nil {
*act = act2
act.insta = insta
if first {
if err := act.MarkAsSeen(); err != nil {
act.err = err
return false
}
}
if act.NextID == "" {
act.err = ErrNoMore
return false
}
return true
}
act.err = err
return false
}
// MarkAsSeen will let instagram know you visited the activity page, and mark
// current items as seen.
func (act *Activity) MarkAsSeen() error {
insta := act.insta
_, _, err := insta.sendRequest(
&reqOptions{
Endpoint: urlActivitySeen,
IsPost: true,
Query: map[string]string{
"_uuid": insta.uuid,
},
},
)
return err
}
func newActivity(insta *Instagram) *Activity {
act := &Activity{
insta: insta,
}
return act
}