-
Notifications
You must be signed in to change notification settings - Fork 0
/
thieves.go
148 lines (126 loc) · 2.97 KB
/
thieves.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
package thieves
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"regexp"
"strings"
"time"
"github.com/axgle/mahonia"
)
const (
_charset_utf8 = "utf-8"
)
type Thief struct {
url string
val string
}
func (this *Thief) Cut(from, to string) *Thief {
if len(this.val) == 0 {
return this
}
var i, j = strings.Index(this.val, from), strings.LastIndex(this.val, to)
max := len(this.val)
if i <= -1 {
i = 0
}
if j <= -1 {
j = 0
}
if j > max {
j = max
}
if i == 0 && j == 0 {
this.val = ""
return this
}
this.val = this.val[i:j]
return this
}
func (this *Thief) FindAll(expr string) [][]string {
re, _ := regexp.Compile(expr)
list := re.FindAllStringSubmatch(this.val, -1)
return list
}
func (this *Thief) HTMLToLower() *Thief {
re, _ := regexp.Compile(`\<[\S\s]+?\>`)
this.val = re.ReplaceAllStringFunc(this.val, strings.ToLower)
return this
}
func (this *Thief) Replace(expr string, repl string) *Thief {
re, _ := regexp.Compile(expr)
this.val = re.ReplaceAllString(this.val, repl)
return this
}
func (this *Thief) Trim(expr string) *Thief {
return this.Replace(expr, "")
}
func (this *Thief) TrimAll() *Thief {
return this.TrimScript().TrimStyle().TrimConsecutiveBlank().TrimNewLine()
}
func (this *Thief) TrimConsecutiveBlank() *Thief {
return this.Trim(`\s{2,}`)
}
func (this *Thief) TrimNewLine() *Thief {
return this.Trim(`\r|\n`)
}
func (this *Thief) TrimScript() *Thief {
return this.Trim(`\<(?i:script)[\S\s]+?\</(?i:script)\>`)
}
func (this *Thief) TrimStyle() *Thief {
return this.Trim(`\<(?i:style)[\S\s]+?\</(?i:style)\>`)
}
func (this *Thief) Val() string {
return this.val
}
func New(url string) *Thief {
return NewWithHeader(url, http.Header{"Accept": []string{"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"}}, 5*time.Second, _charset_utf8)
}
func NewWithHeader(url string, headers http.Header, timeout time.Duration, charset string) *Thief {
thief := &Thief{url, ""}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Printf("NewRequest %s error: %s\r\n", url, err.Error())
return thief
}
for key, values := range headers {
for _, value := range values {
req.Header.Add(key, value)
}
}
client := &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
deadline := time.Now().Add(timeout + 5*time.Second)
c, err := net.DialTimeout(netw, addr, timeout)
if err != nil {
return nil, err
}
c.SetDeadline(deadline)
return c, nil
},
},
}
res, err := client.Do(req)
if err != nil {
fmt.Printf("GET %s error: %s\r\n", url, err.Error())
return thief
}
defer res.Body.Close()
if res.StatusCode != 200 {
return thief
}
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Printf("READ res.Body error: %s\r\n", err.Error())
return thief
}
if charset == _charset_utf8 {
thief.val = string(bytes)
} else {
coder := mahonia.NewDecoder(charset)
thief.val = coder.ConvertString(string(bytes))
}
return thief
}