-
Notifications
You must be signed in to change notification settings - Fork 8
/
media.go
138 lines (123 loc) · 3.79 KB
/
media.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
package gista
import (
"fmt"
"strconv"
"github.com/aliforever/gista/errs"
"github.com/aliforever/gista/signatures"
"github.com/aliforever/gista/utils"
"strings"
"errors"
"github.com/aliforever/gista/constants"
"github.com/aliforever/gista/responses"
)
type media struct {
ig *Instagram
}
func newMedia(i *Instagram) *media {
return &media{ig: i}
}
func (m *media) GetInfo(mediaId interface{}) (res *responses.MediaInfo, err error) {
mediaIdStr := ""
switch mediaId.(type) {
case int64:
mediaIdStr = fmt.Sprintf("%d", mediaId.(int64))
case int:
mediaIdStr = fmt.Sprintf("%d", mediaId.(int))
case string:
mediaIdStr = mediaId.(string)
}
res = &responses.MediaInfo{}
err = m.ig.client.Request(fmt.Sprintf(constants.GetMediaInfo, mediaIdStr)).GetResponse(res)
return
}
func (m *media) GetBlockedMedia() (res *responses.BlockedMedia, err error) {
res = &responses.BlockedMedia{}
err = m.ig.client.Request(constants.BlockedMedia).GetResponse(res)
return
}
func (m *media) LikeComment(commentId int64) (res *responses.CommentLike, err error) {
res = &responses.CommentLike{}
err = m.ig.client.Request(fmt.Sprintf(constants.CommentLike, commentId)).
AddUuIdPost().
AddUIdPost().
AddCSRFPost().
AddDeviceIdPost().GetResponse(res)
return
}
func (m *media) Comment(mediaId interface{}, commentText string, replyCommentId *int, module *string) (res *responses.Comment, err error) {
res = &responses.Comment{}
mediaIdStr := ""
switch mediaId.(type) {
case int64:
mediaIdStr = fmt.Sprintf("%d", mediaId.(int64))
case int:
mediaIdStr = fmt.Sprintf("%d", mediaId.(int))
case string:
mediaIdStr = mediaId.(string)
}
moduleText := ""
if module == nil {
moduleText = "comments_feed_timeline"
}
request := m.ig.client.Request(fmt.Sprintf(constants.CommentMedia, mediaIdStr)).
AddPost("user_breadcrumb", utils.GenerateUserBreadCrumb(len([]rune(commentText)))).
AddPost("idempotence_token", signatures.GenerateUUID(true)).
AddUuIdPost().
AddUIdPost().
AddCSRFPost().
AddPost("comment_text", commentText).
AddPost("containermodule", moduleText).
AddPost("radio_type", "wifi-none").
AddDeviceIdPost()
/* if ($replyCommentId !== null) {
if ($commentText[0] !== '@') {
throw new \InvalidArgumentException('When replying to a comment, your text must begin with an @-mention to their username.');
}
$request->addPost('replied_to_comment_id', $replyCommentId);
}*/
if replyCommentId != nil {
if commentText[0] != '@' {
err = errs.MissingMentionInReply(commentText)
return
}
request.AddPost("replied_to_comment_id", fmt.Sprintf("%d", replyCommentId))
}
err = request.GetResponse(res)
return
}
func (m *media) GetComments(mediaId interface{}, options map[string]string) (res *responses.MediaComment, err error) {
res = &responses.MediaComment{}
mediaIdInt := int64(0)
switch mediaId.(type) {
case int64:
mediaIdInt = mediaId.(int64)
case string:
idTemp, _ := strconv.Atoi(mediaId.(string)[:strings.Index(mediaId.(string), "_")])
mediaIdInt = int64(idTemp)
}
req := m.ig.client.Request(fmt.Sprintf(constants.GetComments, mediaIdInt)).
AddParam("can_support_threading", "true")
if options != nil {
minId, minIdOk := options["min_id"]
maxId, maxIdOk := options["max_id"]
if minIdOk && maxIdOk {
err = errors.New("you can use either 'min_id' or 'max_id', but not both at the same time")
return
}
if minIdOk {
req.AddParam("min_id", minId)
}
if maxIdOk {
req.AddParam("max_id", maxId)
}
if val, ok := options["target_comment_id"]; ok {
if minIdOk || maxIdOk {
err = errors.New("you cannot use the 'target_comment_id' parameter together with the 'min_id' or 'max_id' parameters")
return
}
req.AddParam("target_comment_id", val)
}
}
err = req.GetResponse(res)
return
}