-
Notifications
You must be signed in to change notification settings - Fork 1
/
instagram.go
158 lines (144 loc) · 4.27 KB
/
instagram.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
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/siongui/instago"
"net/http"
"runtime/debug"
"strconv"
"strings"
)
type InstaPost struct {
Description string `json:"description"`
PhotoURL string `json:"photo_url"`
Likes int64 `json:"likes"`
ID string `json:"id"`
PostUrl string `json:"post_url"`
}
type InstaUser struct {
UserName string `json:"user_name"`
Posts []InstaPost `json:"posts"`
Stories []InstaStory `json:"stories"`
}
type InstaStory struct {
StoryURL string `json:"story_url"`
OriginalID string `json:"original_id"`
ID string `json:"id"`
MediaURL string `json:"media_url"`
}
type InstagramHandler struct {
PrivateAPIManager instago.IGApiManager
PublicAPIManager instago.IGApiManager
}
func newInstagramHandler(dcUserId string, sessionID string, csrfToken string) InstagramHandler {
privateAPIManager := *instago.NewInstagramApiManager(dcUserId, sessionID, csrfToken)
publicAPIManager := *instago.NewInstagramApiManager("", "", "")
return InstagramHandler{
PrivateAPIManager: privateAPIManager,
PublicAPIManager: publicAPIManager,
}
}
func (handler InstagramHandler) handlePostsRequest() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("{\"Error\": \"%+v\"}", r)))
fmt.Println("recovered from ", r)
debug.PrintStack()
}
}()
vars := mux.Vars(r)
name := vars["username"]
id := getInt(vars["last"], 0)
resp := handler.getPosts(name, id)
res, _ := json.Marshal(resp)
w.WriteHeader(200)
w.Write(res)
}
}
func (handler InstagramHandler) getPosts(name string, id int64) *InstaUser {
userInfo, _ := handler.PublicAPIManager.GetUserInfo(name)
var medias []instago.IGMedia
if userInfo.IsPrivate {
medias, _ = handler.PrivateAPIManager.GetAllPostMedia(name)
} else {
medias, _ = handler.PublicAPIManager.GetAllPostMedia(name)
}
resp := &InstaUser{UserName: name, Posts: []InstaPost{}}
for _, media := range medias {
mediaId := getInt(media.Id, 0)
if mediaId <= id {
break
}
postInfo := InstaPost{
PhotoURL: media.DisplayUrl,
PostUrl: media.GetPostUrl(),
Likes: media.EdgeMediaPreviewLike.Count,
ID: media.Id,
}
if len(media.EdgeMediaToCaption.Edges) > 0 {
postInfo.Description = media.EdgeMediaToCaption.Edges[0].Node.Text
}
if media.IsVideo {
//all posts public response does not contain video url
info, _ := handler.PrivateAPIManager.GetPostInfo(media.GetPostCode())
postInfo.PhotoURL = info.GetMediaUrls()[0]
}
resp.Posts = append(resp.Posts, postInfo)
}
return resp
}
func (handler InstagramHandler) handleStoriesRequest() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if r := recover(); r != nil {
w.WriteHeader(500)
w.Write([]byte(fmt.Sprintf("{\"Error\": \"%+v\"}", r)))
fmt.Println("recovered from ", r)
debug.PrintStack()
}
}()
vars := mux.Vars(r)
name := vars["username"]
last := vars["last"]
lastId := getInt(last, 0)
resp := handler.getStories(name, lastId)
res, _ := json.Marshal(resp)
w.WriteHeader(200)
w.Write(res)
}
}
func (handler InstagramHandler) getStories(name string, lastId int64) *InstaUser {
userId, _ := instago.GetUserId(name)
stories, _ := handler.PrivateAPIManager.GetUserStory(userId)
resp := &InstaUser{UserName: name, Stories: []InstaStory{}}
for _, story := range stories.GetItems() {
storyId := getStoryIdWithoutUserId(story.Id)
if storyId <= lastId {
continue
}
media, _ := story.GetMediaUrls()
storyInfo := InstaStory{
StoryURL: story.GetPostUrl(),
ID: strconv.FormatInt(storyId, 10),
OriginalID: story.Id,
MediaURL: media[0],
}
resp.Stories = append(resp.Stories, storyInfo)
}
return resp
}
func getStoryIdWithoutUserId(storyId string) int64 {
storyIdString := storyId[:strings.IndexByte(storyId, '_')]
return getInt(storyIdString, 0)
}
func getInt(strValue string, defaultValue int64) int64 {
intValue, err := strconv.ParseInt(strValue, 10, 64)
if err != nil {
fmt.Printf("Incorrect int value, default value %+v will be used ", defaultValue)
return defaultValue
}
return intValue
}