-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
213 lines (192 loc) · 5.42 KB
/
client.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
package gista
import (
"encoding/json"
"net/http"
"net/http/cookiejar"
"net/url"
"time"
"github.com/aliforever/gista/errs"
"github.com/aliforever/gista/middleware"
"github.com/aliforever/gista/constants"
)
const cookieAutoSaveInterval int64 = 45
type client struct {
userAgent string
client *http.Client
instagram *Instagram
cookieJar *cookiejar.Jar
cookieJarLastSaved int64
zeroRating *middleware.ZeroRating
}
func newClient(i *Instagram) (c *client) {
c = &client{instagram: i}
c.client = &http.Client{}
c.zeroRating = middleware.NewZeroRating()
middleWareContainer := middleware.NewContainer(&http.DefaultTransport)
middleWareContainer.Push(c.zeroRating)
c.client.Transport = middleWareContainer
return
}
func (c *client) Request(address string) (r *request) {
return newRequest(address, c)
}
/*func (c *client) getStreamForFile(fileMap map[string]*string) (result io.Reader, err error) {
//https://medium.com/@owlwalks/sending-big-file-with-minimal-memory-in-golang-8f3fc280d2c
if fileMap["contents"] != nil {
result = strings.NewReader(*fileMap["contents"])
} else if fileMap["filepath"] != nil {
file, fErr := os.Open(*fileMap["filepath"])
if fErr != nil {
err = errors.CannotOpenFile(*fileMap["filepath"], fErr.Error())
return
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(fileMap, filepath.Base(path))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
} else {
err = errors.NoDataForStreamCreation
}
return
}
*/
func (c *client) api(request *http.Request) (resp *http.Response, err error) {
request.Header.Set("User-Agent", c.userAgent)
request.Header.Set("Connection", "keep-alive")
request.Header.Set("X-FB-HTTP-Engine", constants.XFbHttpEngine)
request.Header.Set("Accept", "*/*")
request.Header.Set("Accept-Encoding", constants.AcceptEncoding)
request.Header.Set("Accept-Language", constants.AcceptLanguage)
if c.cookieJar != nil {
c.client.Jar = c.cookieJar
}
resp, err = c.client.Do(request)
if err != nil {
return
}
/*defer resp.Body.Close()*/
statusCode := resp.StatusCode
switch statusCode {
case 429:
err = errs.ThrottledResponse
return
case 431:
err = errs.RequestHeaderTooLargeResponse
return
}
if time.Now().Unix()-c.cookieJarLastSaved > cookieAutoSaveInterval {
err = c.SaveCookieJar()
}
return
}
func (c *client) UpdateFromCurrentSettings(resetCookieJar bool) {
c.userAgent = c.instagram.device.GetUserAgent()
c.LoadCookieJar(resetCookieJar)
if c.GetToken() == nil {
c.instagram.isMaybeLoggedIn = false
}
}
func (c *client) LoadCookieJar(resetCookieJar bool) {
c.cookieJar = nil
if resetCookieJar {
data := ""
c.instagram.settings.SetCookies(&data)
}
var cookies *string
cookies, _ = c.instagram.settings.GetCookies()
var restoredCookies []map[string]interface{}
if cookies != nil && len(*cookies) > 0 {
json.Unmarshal([]byte(*cookies), &restoredCookies)
}
c.cookieJar = c.mapSliceToCookieJar(restoredCookies)
c.cookieJarLastSaved = time.Now().Unix()
}
func (c *client) cookieJarToMapSlice(cj *cookiejar.Jar) (mapSlice []map[string]interface{}) {
address, _ := url.Parse(constants.CookieUrl)
cookies := cj.Cookies(address)
for _, c := range cookies {
mapSlice = append(mapSlice, map[string]interface{}{
"Name": c.Name,
"Value": c.Value,
"Domain": c.Domain,
"Path": c.Path,
"MaxAge": c.MaxAge,
"Expires": c.Expires,
"Secure": c.Secure,
"HttpOnly": c.HttpOnly,
})
}
return
}
func (c *client) mapSliceToCookieJar(mapSlice []map[string]interface{}) (cj *cookiejar.Jar) {
cj, _ = cookiejar.New(nil)
var cookies []*http.Cookie
for _, m := range mapSlice {
cookie := http.Cookie{}
cookie.Name = m["Name"].(string)
cookie.Value = m["Value"].(string)
cookie.Domain = m["Domain"].(string)
cookie.Path = m["Path"].(string)
cookie.MaxAge = int(m["MaxAge"].(float64))
layout := "2006-01-02T15:04:05Z"
t, _ := time.Parse(layout, m["Expires"].(string))
cookie.Expires = t
cookie.Secure = m["Secure"].(bool)
cookie.HttpOnly = m["HttpOnly"].(bool)
cookies = append(cookies, &cookie)
}
address, _ := url.Parse(constants.CookieUrl)
cj.SetCookies(address, cookies)
return
}
func (c *client) GetCookie(name string, domain *string, path *string) (cookie *http.Cookie) {
if c.cookieJar != nil {
address, _ := url.Parse(*domain)
for _, C := range (*c.cookieJar).Cookies(address) {
if C.Name == name && (C.Expires.IsZero() || !C.Expires.Before(time.Now())) && (path == nil || C.Path == *path) {
cookie = C
}
}
}
return
}
func (c *client) SaveCookieJar() (err error) {
newCookies, err := c.GetCookieJarAsJSON()
err = c.instagram.settings.SetCookies(newCookies)
if err != nil {
return
}
c.cookieJarLastSaved = time.Now().Unix()
return
}
func (c *client) GetCookieJarAsJSON() (jar *string, err error) {
if c.cookieJar == nil {
empty := ""
return &empty, nil
}
mapSlice := c.cookieJarToMapSlice(c.cookieJar)
var jsonByte []byte
jsonByte, err = json.Marshal(mapSlice)
if err != nil {
err = errs.CannotMarshalJSON(mapSlice, err.Error())
return
}
str := string(jsonByte)
jar = &str
return
}
func (c *client) GetToken() (t *string) {
cookie := c.GetCookie("csrftoken", &constants.CookieUrl, nil)
if cookie == nil || cookie.Value == "" {
return nil
}
t = &cookie.Value
return
}
func (c *client) ZeroRating() *middleware.ZeroRating {
return c.zeroRating
}