-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodec.go
149 lines (139 loc) · 3.82 KB
/
codec.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
package bili
import (
"errors"
"fmt"
cookiejar "github.com/juju/persistent-cookiejar"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"github.com/cheggaaa/pb/v3"
"github.com/google/go-querystring/query"
)
type progress struct {
enabled bool
bar *pb.ProgressBar
}
func (p *progress) Write(ch []byte) (n int, err error) {
n = len(ch)
p.bar.Add(n)
return n, nil
}
func HttpGet(client *http.Client, endpoint string) ([]byte, error) {
response, err := client.Get(endpoint)
if err != nil {
return nil, err
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return responseBody, err
}
func HttpGetAsFile(client *http.Client, endpoint string, path string, showProgress bool, progressWriter ProgressWriter) error {
tmpPath := fmt.Sprintf("%s.tmp", path)
out, err := os.Create(tmpPath)
if err != nil {
return err
}
response, err := client.Get(endpoint)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return errors.New("get response does not return 200")
}
progressBar := progress{showProgress, pb.StartNew(int(response.ContentLength))}
teeReader := io.TeeReader(response.Body, &progressBar)
if progressWriter != nil {
if progressWriter.SetContentLength(int(response.ContentLength)) != nil {
return errors.New("HttpGetAsFile: cannot set content length")
}
teeReader = io.TeeReader(teeReader, progressWriter)
}
_, err = io.Copy(out, teeReader)
progressBar.bar.Finish()
if err != nil {
return err
}
if err = os.Rename(tmpPath, path); err != nil {
return err
}
return nil
}
func HttpGetWithParams(client *http.Client, endpoint string, params interface{}) ([]byte, error) {
v, err := query.Values(params)
if err != nil {
return nil, err
}
fullEndpoint := endpoint + "?" + v.Encode()
return HttpGet(client, fullEndpoint)
}
// https://gist.github.com/tonyhb/5819315
func structToMap(i interface{}) (values url.Values) {
values = url.Values{}
iVal := reflect.ValueOf(i).Elem()
typ := iVal.Type()
for i := 0; i < iVal.NumField(); i++ {
f := iVal.Field(i)
// You ca use tags here...
// tag := typ.Field(i).Tag.Get("tagname")
// Convert each type into a string for the url.Values string map
var v string
switch f.Interface().(type) {
case int, int8, int16, int32, int64:
v = strconv.FormatInt(f.Int(), 10)
case uint, uint8, uint16, uint32, uint64:
v = strconv.FormatUint(f.Uint(), 10)
case float32:
v = strconv.FormatFloat(f.Float(), 'f', 4, 32)
case float64:
v = strconv.FormatFloat(f.Float(), 'f', 4, 64)
case []byte:
v = string(f.Bytes())
case string:
v = f.String()
}
values.Set(typ.Field(i).Name, v)
}
return
}
func HttpPostWithParams(client *http.Client, endpoint string, form interface{}) ([]byte, []*http.Cookie, error) {
return HttpPostWithParamsReferer(client, endpoint, form, "")
}
func HttpPostWithParamsReferer(client *http.Client, endpoint string, form interface{}, referer string) ([]byte, []*http.Cookie, error) {
v, err := query.Values(form)
if err != nil {
return nil, nil, err
}
request, err := http.NewRequest("POST", endpoint, strings.NewReader(v.Encode()))
if err != nil {
return nil, nil, err
}
if len(referer) > 0 {
request.Header.Set("Referer", referer)
}
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(request)
if err != nil {
return nil, nil, err
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, nil, err
}
u, _ := url.Parse("http://bilibili.com")
client.Jar.SetCookies(u, response.Cookies())
if err := client.Jar.(*cookiejar.Jar).Save(); err != nil {
log.Printf("HttpPostWithParamsReferer: cannot save cookie %v\n", err)
}
return responseBody, response.Cookies(), err
}