-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
115 lines (97 loc) · 2.64 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
package easyclient
import (
`errors`
`fmt`
`io`
`io/ioutil`
`net/http`
`os`
`path/filepath`
`regexp`
`time`
`github.com/abc1236762/go-cloudflare-scraper`
`github.com/juju/persistent-cookiejar`
)
type Client scraper.Client
func SetLogOutput(w io.Writer) {
scraper.Log.SetOutput(w)
}
func New(needCookieJar bool, cookieFilename, userAgent string) (client *Client, err error) {
if cookieFilename == "" {
cookieFilename = ".cookie"
}
var jar http.CookieJar
if needCookieJar {
if jar, err = cookiejar.New(&cookiejar.Options{
Filename: cookieFilename,
}); err != nil {
return
}
}
return (*Client)(scraper.NewClient(jar, userAgent)), nil
}
func (c *Client) SaveCookie() error {
if c.Jar != nil {
return c.Jar.(*cookiejar.Jar).Save()
}
return nil
}
func (c *Client) getWithBody(url string) (resp *http.Response, body []byte, err error) {
if resp, err = (*scraper.Client)(c).Get(url); err != nil {
return
} else if resp.StatusCode != 200 {
return nil, nil, fmt.Errorf("expected status %s", resp.Status)
}
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return
}
return resp, body, resp.Body.Close()
}
func (c *Client) GetWithBody(url string, retryCnt uint64,
isRetryWhenBlank, isRetryWhenEOFNotHtml bool) (resp *http.Response, body []byte, err error) {
for i := uint64(0); i <= retryCnt; i++ {
if resp, body, err = c.getWithBody(url); err != nil {
continue
}
if isRetryWhenBlank && len(body) == 0 {
err = errors.New("body returned nothing")
} else if isRetryWhenEOFNotHtml &&
!regexp.MustCompile(`</\s*html>`).Match(body) {
err = errors.New("eof of body is not matched pattern `</\\s*html>`")
} else {
break
}
}
return
}
func (c *Client) GetWithBodyStr(url string, retryCnt uint64,
isRetryWhenBlank, isRetryWhenEOFNotHtml bool) (resp *http.Response, bodyStr string, err error) {
var body []byte
if resp, body, err = c.GetWithBody(url, retryCnt,
isRetryWhenBlank, isRetryWhenEOFNotHtml); err != nil {
return
}
return resp, string(body), nil
}
func (c *Client) Download(url, path string, retryCnt uint64, needRetryWhenBlank bool) (err error) {
var body []byte
var resp *http.Response
if resp, body, err = c.GetWithBody(
url, retryCnt, needRetryWhenBlank, false); err != nil {
return
}
if err = os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
return
}
if err = ioutil.WriteFile(path, body, 0666); err != nil {
return
}
if lastModified := resp.Header.Get("Last-Modified"); lastModified != "" {
var fileTime time.Time
if fileTime, err = time.Parse(time.RFC1123, lastModified); err != nil {
return
}
return os.Chtimes(path, fileTime, fileTime)
}
return
}