-
Notifications
You must be signed in to change notification settings - Fork 10
/
saved.go
134 lines (115 loc) · 3.21 KB
/
saved.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
package instago
// This file decode JSON data returned by Instagram saved posts API endpoint
import (
"encoding/json"
"log"
"strings"
"time"
)
const urlSaved = `https://i.instagram.com/api/v1/feed/saved/`
const urlSavedCollection = `https://i.instagram.com/api/v1/feed/collection/{{COLLECTIONID}}/`
const urlSavedCollectionList = `https://i.instagram.com/api/v1/collections/list/`
type savedPostsResp struct {
Items []struct {
Item IGItem `json:"media"`
} `json:"items"`
NumResults int64 `json:"num_results"`
MoreAvailable bool `json:"more_available"`
AutoLoadMoreEnabled bool `json:"auto_load_more_enabled"`
NextMaxId string `json:"next_max_id"`
Status string `json:"status"`
}
type Collection struct {
CollectionId string `json:"collection_id"`
CollectionName string `json:"collection_name"`
CollectionType string `json:"collection_type"`
CollectionMediaCount int64 `json:"collection_media_count"`
//TODO: cover_media
}
type collectionsList struct {
Collections []Collection `json:"items"`
MoreAvailable bool `json:"more_available"`
AutoLoadMoreEnabled bool `json:"auto_load_more_enabled"`
NextMaxId string `json:"next_max_id"`
Status string `json:"status"`
}
// GetSavedPosts returns your saved posts. Pass -1 will get all saved posts.
func (m *IGApiManager) GetSavedPosts(numOfItem int) (items []IGItem, err error) {
b, err := m.getHTTPResponse(urlSaved, "GET")
if err != nil {
return
}
// for development purpose
if saveRawJsonByte {
SaveRawJsonByte("saved-", b)
}
spp := savedPostsResp{}
err = json.Unmarshal(b, &spp)
if err != nil {
return
}
for _, item := range spp.Items {
items = append(items, item.Item)
}
for spp.MoreAvailable {
if numOfItem > 0 && len(items) > numOfItem {
break
}
url := urlSaved + "?max_id=" + spp.NextMaxId
b, err = m.getHTTPResponse(url, "GET")
if err != nil {
return
}
spp = savedPostsResp{}
err = json.Unmarshal(b, &spp)
if err != nil {
return
}
for _, item := range spp.Items {
items = append(items, item.Item)
}
log.Println("fetched", len(items), "items")
// sleep 500ms to prevent http 429
time.Sleep(500 * time.Millisecond)
}
return
}
// GetSavedCollection returns your saved collections.
func (m *IGApiManager) GetSavedCollection(id string) (items []IGItem, err error) {
url := strings.Replace(urlSavedCollection, "{{COLLECTIONID}}", id, 1)
b, err := m.getHTTPResponse(url, "GET")
if err != nil {
return
}
// for development purpose
if saveRawJsonByte {
SaveRawJsonByte(id+"-collection-", b)
}
spp := savedPostsResp{}
err = json.Unmarshal(b, &spp)
if err != nil {
return
}
for _, item := range spp.Items {
items = append(items, item.Item)
}
return
}
// GetSavedCollectionList returns your list of saved collections.
func (m *IGApiManager) GetSavedCollectionList() (c []Collection, err error) {
b, err := m.getHTTPResponse(urlSavedCollectionList, "GET")
if err != nil {
return
}
// for development purpose
if saveRawJsonByte {
SaveRawJsonByte("collections-list-", b)
}
cl := collectionsList{}
err = json.Unmarshal(b, &cl)
if err != nil {
return
}
c = cl.Collections
return
}