forked from andeya/pholcus
/
util.go
469 lines (423 loc) · 9.88 KB
/
util.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package util
import (
"crypto/md5"
"crypto/rand"
"encoding/hex"
"encoding/json"
"encoding/xml"
"fmt"
"hash/crc32"
"hash/fnv"
"io"
r "math/rand"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"unsafe"
"golang.org/x/net/html/charset"
"github.com/henrylee2cn/pholcus/logs"
)
const (
// Spider中启用Keyin的初始值
USE_KEYIN = "\r\t\n"
)
var (
re = regexp.MustCompile(">[ \t\n\v\f\r]+<")
)
// JsonpToJson modify jsonp string to json string
// Example: forbar({a:"1",b:2}) to {"a":"1","b":2}
func JsonpToJson(json string) string {
start := strings.Index(json, "{")
end := strings.LastIndex(json, "}")
start1 := strings.Index(json, "[")
if start1 > 0 && start > start1 {
start = start1
end = strings.LastIndex(json, "]")
}
if end > start && end != -1 && start != -1 {
json = json[start : end+1]
}
json = strings.Replace(json, "\\'", "", -1)
regDetail, _ := regexp.Compile("([^\\s\\:\\{\\,\\d\"]+|[a-z][a-z\\d]*)\\s*\\:")
return regDetail.ReplaceAllString(json, "\"$1\":")
}
// 创建目录
func Mkdir(Path string) {
p, _ := path.Split(Path)
if p == "" {
return
}
d, err := os.Stat(p)
if err != nil || !d.IsDir() {
if err = os.MkdirAll(p, 0777); err != nil {
logs.Log.Error("创建路径失败[%v]: %v\n", Path, err)
}
}
}
// The GetWDPath gets the work directory path.
func GetWDPath() string {
wd := os.Getenv("GOPATH")
if wd == "" {
panic("GOPATH is not setted in env.")
}
return wd
}
// The IsDirExists judges path is directory or not.
func IsDirExists(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return os.IsExist(err)
} else {
return fi.IsDir()
}
panic("util isDirExists not reached")
}
// The IsFileExists judges path is file or not.
func IsFileExists(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return os.IsExist(err)
} else {
return !fi.IsDir()
}
panic("util isFileExists not reached")
}
// 遍历文件,可指定后缀
func WalkFiles(targpath string, suffixes ...string) (filelist []string) {
if !filepath.IsAbs(targpath) {
targpath, _ = filepath.Abs(targpath)
}
err := filepath.Walk(targpath, func(retpath string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
if len(suffixes) == 0 {
filelist = append(filelist, retpath)
return nil
}
for _, suffix := range suffixes {
if strings.HasSuffix(retpath, suffix) {
filelist = append(filelist, retpath)
}
}
return nil
})
if err != nil {
logs.Log.Error("util.WalkFiles: %v\n", err)
return
}
return
}
// 遍历目录,可指定后缀
func WalkDir(targpath string, suffixes ...string) (dirlist []string) {
if !filepath.IsAbs(targpath) {
targpath, _ = filepath.Abs(targpath)
}
err := filepath.Walk(targpath, func(retpath string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if !f.IsDir() {
return nil
}
if len(suffixes) == 0 {
dirlist = append(dirlist, retpath)
return nil
}
for _, suffix := range suffixes {
if strings.HasSuffix(retpath, suffix) {
dirlist = append(dirlist, retpath)
}
}
return nil
})
if err != nil {
logs.Log.Error("util.WalkDir: %v\n", err)
return
}
return
}
// 遍历文件,可指定后缀,返回相对路径
func WalkRelFiles(targpath string, suffixes ...string) (filelist []string) {
if !filepath.IsAbs(targpath) {
targpath, _ = filepath.Abs(targpath)
}
err := filepath.Walk(targpath, func(retpath string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() {
return nil
}
if len(suffixes) == 0 {
filelist = append(filelist, RelPath(retpath))
return nil
}
_retpath := RelPath(retpath)
for _, suffix := range suffixes {
if strings.HasSuffix(_retpath, suffix) {
filelist = append(filelist, _retpath)
}
}
return nil
})
if err != nil {
logs.Log.Error("util.WalkRelFiles: %v\n", err)
return
}
return
}
// 遍历目录,可指定后缀,返回相对路径
func WalkRelDir(targpath string, suffixes ...string) (dirlist []string) {
if !filepath.IsAbs(targpath) {
targpath, _ = filepath.Abs(targpath)
}
err := filepath.Walk(targpath, func(retpath string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if !f.IsDir() {
return nil
}
if len(suffixes) == 0 {
dirlist = append(dirlist, RelPath(retpath))
return nil
}
_retpath := RelPath(retpath)
for _, suffix := range suffixes {
if strings.HasSuffix(_retpath, suffix) {
dirlist = append(dirlist, _retpath)
}
}
return nil
})
if err != nil {
logs.Log.Error("util.WalkRelDir: %v\n", err)
return
}
return
}
// 转相对路径
func RelPath(targpath string) string {
basepath, _ := filepath.Abs("./")
rel, _ := filepath.Rel(basepath, targpath)
return strings.Replace(rel, `\`, `/`, -1)
}
// The IsNum judges string is number or not.
func IsNum(a string) bool {
reg, _ := regexp.Compile("^\\d+$")
return reg.MatchString(a)
}
// simple xml to string support utf8
func XML2mapstr(xmldoc string) map[string]string {
var t xml.Token
var err error
inputReader := strings.NewReader(xmldoc)
decoder := xml.NewDecoder(inputReader)
decoder.CharsetReader = func(s string, r io.Reader) (io.Reader, error) {
return charset.NewReader(r, s)
}
m := make(map[string]string, 32)
key := ""
for t, err = decoder.Token(); err == nil; t, err = decoder.Token() {
switch token := t.(type) {
case xml.StartElement:
key = token.Name.Local
case xml.CharData:
content := Bytes2String([]byte(token))
m[key] = content
default:
// ...
}
}
return m
}
//string to hash
func MakeHash(s string) string {
const IEEE = 0xedb88320
var IEEETable = crc32.MakeTable(IEEE)
hash := fmt.Sprintf("%x", crc32.Checksum([]byte(s), IEEETable))
return hash
}
func HashString(encode string) uint64 {
hash := fnv.New64()
hash.Write([]byte(encode))
return hash.Sum64()
}
// 制作特征值方法一
func MakeUnique(obj interface{}) string {
b, _ := json.Marshal(obj)
hash := fnv.New64()
hash.Write(b)
return strconv.FormatUint(hash.Sum64(), 10)
}
// 制作特征值方法二
func MakeMd5(obj interface{}, length int) string {
if length > 32 {
length = 32
}
h := md5.New()
baseString, _ := json.Marshal(obj)
h.Write([]byte(baseString))
s := hex.EncodeToString(h.Sum(nil))
return s[:length]
}
// 将对象转为json字符串
func JsonString(obj interface{}) string {
b, _ := json.Marshal(obj)
s := fmt.Sprintf("%+v", Bytes2String(b))
r := strings.Replace(s, `\u003c`, "<", -1)
r = strings.Replace(r, `\u003e`, ">", -1)
return r
}
//检查并打印错误
func CheckErr(err error) {
if err != nil {
logs.Log.Error("%v", err)
}
}
func CheckErrPanic(err error) {
if err != nil {
panic(err)
}
}
// 将文件名非法字符替换为相似字符
func FileNameReplace(fileName string) string {
var q = 1
r := []rune(fileName)
size := len(r)
for i := 0; i < size; i++ {
switch r[i] {
case '"':
if q%2 == 1 {
r[i] = '“'
} else {
r[i] = '”'
}
q++
case ':':
r[i] = ':'
case '*':
r[i] = '×'
case '<':
r[i] = '<'
case '>':
r[i] = '>'
case '?':
r[i] = '?'
case '/':
r[i] = '/'
case '|':
r[i] = '∣'
case '\\':
r[i] = '╲'
}
}
return strings.Replace(string(r), USE_KEYIN, ``, -1)
}
// 将Excel工作表名中非法字符替换为下划线
//
func ExcelSheetNameReplace(fileName string) string {
r := []rune(fileName)
size := len(r)
for i := 0; i < size; i++ {
switch r[i] {
case ':', ':', '*', '?', '?', '/', '/', '\\', '╲', ']', '[':
r[i] = '_'
}
}
return strings.Replace(string(r), USE_KEYIN, ``, -1)
}
func Atoa(str interface{}) string {
if str == nil {
return ""
}
return strings.Trim(str.(string), " ")
}
func Atoi(str interface{}) int {
if str == nil {
return 0
}
i, _ := strconv.Atoi(strings.Trim(str.(string), " "))
return i
}
func Atoui(str interface{}) uint {
if str == nil {
return 0
}
u, _ := strconv.Atoi(strings.Trim(str.(string), " "))
return uint(u)
}
// RandomCreateBytes generate random []byte by specify chars.
func RandomCreateBytes(n int, alphabets ...byte) []byte {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
var randby bool
if num, err := rand.Read(bytes); num != n || err != nil {
r.Seed(time.Now().UnixNano())
randby = true
}
for i, b := range bytes {
if len(alphabets) == 0 {
if randby {
bytes[i] = alphanum[r.Intn(len(alphanum))]
} else {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
} else {
if randby {
bytes[i] = alphabets[r.Intn(len(alphabets))]
} else {
bytes[i] = alphabets[b%byte(len(alphabets))]
}
}
}
return bytes
}
// 切分用户输入的自定义信息
func KeyinsParse(keyins string) []string {
keyins = strings.TrimSpace(keyins)
if keyins == "" {
return []string{}
}
for _, v := range re.FindAllString(keyins, -1) {
keyins = strings.Replace(keyins, v, "><", -1)
}
m := map[string]bool{}
for _, v := range strings.Split(keyins, "><") {
v = strings.TrimPrefix(v, "<")
v = strings.TrimSuffix(v, ">")
if v == "" {
continue
}
m[v] = true
}
s := make([]string, len(m))
i := 0
for k := range m {
s[i] = k
i++
}
return s
}
// Bytes2String直接转换底层指针,两者指向的相同的内存,改一个另外一个也会变。
// 效率是string([]byte{})的百倍以上,且转换量越大效率优势越明显。
func Bytes2String(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// String2Bytes直接转换底层指针,两者指向的相同的内存,改一个另外一个也会变。
// 效率是string([]byte{})的百倍以上,且转换量越大效率优势越明显。
// 转换之后若没做其他操作直接改变里面的字符,则程序会崩溃。
// 如 b:=String2bytes("xxx"); b[1]='d'; 程序将panic。
func String2Bytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}