forked from lauyoume/gopiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
piper.go
876 lines (818 loc) · 20.4 KB
/
piper.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
package gopiper
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/admpub/regexp2"
"github.com/PuerkitoBio/goquery"
simplejson "github.com/bitly/go-simplejson"
)
const (
// begin new version
PT_RAW = "raw"
PT_INT = "int"
PT_FLOAT = "float"
PT_BOOL = "bool"
PT_STRING = "string"
PT_INT_ARRAY = "int-array"
PT_FLOAT_ARRAY = "float-array"
PT_BOOL_ARRAY = "bool-array"
PT_STRING_ARRAY = "string-array"
PT_HTML_ARRAY = "html-array"
PT_MAP = "map"
PT_ARRAY = "array"
PT_JSON_VALUE = "json"
PT_JSON_PARSE = "jsonparse"
// end new version
// begin compatible old version
PT_TEXT = "text"
PT_HREF = "href"
PT_HTML = "html"
PT_ATTR = `attr\[([\w\W]+)\]`
PT_ATTR_ARRAY = `attr-array\[([\w\W]+)\]`
PT_IMG_SRC = "src"
PT_IMG_ALT = "alt"
PT_TEXT_ARRAY = "text-array"
PT_HREF_ARRAY = "href-array"
PT_OUT_HTML = "outhtml"
// end compatible old version
PAGE_JSON = "json"
PAGE_HTML = "html"
PAGE_JS = "js"
PAGE_XML = "xml"
PAGE_TEXT = "text"
REGEXP_PRE = "regexp:"
REGEXP2_PRE = "regexp2:"
)
var (
attrExp = regexp.MustCompile(PT_ATTR)
attrArrayExp = regexp.MustCompile(PT_ATTR_ARRAY)
fnExp = regexp.MustCompile(`([a-z_]+)(\(([\w\W+]+)\))?`)
jsonNumberIndexExp = regexp.MustCompile(`^\[(\d+)\]$`)
)
// VerifySelector 验证正则表达式
func VerifySelector(selector string) (err error) {
if strings.HasPrefix(selector, REGEXP_PRE) {
_, err = regexp.Compile(strings.TrimPrefix(selector, REGEXP_PRE))
} else if strings.HasPrefix(selector, REGEXP2_PRE) {
_, err = regexp2.Compile(strings.TrimPrefix(selector, REGEXP2_PRE), 0)
}
return
}
type PipeItem struct {
Name string `json:"name,omitempty"` //只有类型为map的时候才会用到
Selector string `json:"selector,omitempty"`
Type string `json:"type"`
Filter string `json:"filter,omitempty"`
SubItem []PipeItem `json:"subitem,omitempty"`
fetcher Fether
storer Storer
pageType string
doc *goquery.Document
}
type Fether func(pageURL string) (body []byte, err error)
type Storer func(fileURL, savePath string, fetched bool) (newPath string, err error)
type htmlSelector struct {
*goquery.Selection
attr string
selector string
}
func (p *PipeItem) SetFetcher(fetcher Fether) {
p.fetcher = fetcher
}
func (p *PipeItem) SetStorer(storer Storer) {
p.storer = storer
}
func (p *PipeItem) CopyFrom(from *PipeItem) {
p.SetFetcher(from.fetcher)
p.SetStorer(from.storer)
p.doc = from.doc
}
func (p *PipeItem) Fetcher() Fether {
return p.fetcher
}
func (p *PipeItem) Storer() Storer {
return p.storer
}
func (p *PipeItem) PipeBytes(body []byte, pageType string) (interface{}, error) {
p.pageType = pageType
switch pageType {
case PAGE_HTML:
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(body))
if err != nil {
return nil, err
}
p.doc = doc
return p.pipeSelection(doc.Selection)
case PAGE_JSON:
return p.pipeJSON(body)
case PAGE_TEXT:
return p.pipeText(body)
}
return nil, nil
}
func (p *PipeItem) parseRegexp(body string, useRegexp2 bool) (interface{}, error) {
var (
preLen int
sv []string
rs string
)
if useRegexp2 {
preLen = len(REGEXP2_PRE)
} else {
preLen = len(REGEXP_PRE)
}
s := p.Selector[preLen:]
if useRegexp2 {
exp, err := regexp2.Compile(s, regexp2.None)
if err != nil {
return nil, err
}
mch, err := exp.FindStringMatch(body)
if err != nil {
return nil, err
}
if mch != nil {
sv = mch.Slice()
//fmt.Println(`[regexp2][matched:`+strconv.Itoa(mch.GroupCount())+`]`, mch.String(), com.Dump(sv, false))
}
} else {
exp, err := regexp.Compile(s)
if err != nil {
return nil, err
}
sv = exp.FindStringSubmatch(body)
}
if len(sv) == 1 {
rs = sv[0]
} else if len(sv) > 1 {
rs = sv[1]
sv = sv[1:]
}
switch p.Type {
case PT_INT, PT_FLOAT, PT_BOOL:
val, err := parseTextValue(rs, p.Type)
if err != nil {
return nil, err
}
return callFilter(p, val, p.Filter)
case PT_INT_ARRAY, PT_FLOAT_ARRAY, PT_BOOL_ARRAY:
val, err := parseTextValue(sv, p.Type)
if err != nil {
return nil, err
}
return callFilter(p, val, p.Filter)
case PT_TEXT, PT_STRING:
return callFilter(p, rs, p.Filter)
case PT_TEXT_ARRAY, PT_STRING_ARRAY:
return callFilter(p, sv, p.Filter)
case PT_JSON_PARSE:
if p.SubItem == nil || len(p.SubItem) <= 0 {
return nil, ErrJsonparseNeedSubItem
}
body, err := text2JSONByte(rs)
if err != nil {
return nil, errors.New("jsonparse: text is not a json string: " + err.Error())
}
parseItem := p.SubItem[0]
parseItem.CopyFrom(p)
res, err := parseItem.pipeJSON(body)
if err != nil {
return nil, err
}
return callFilter(p, res, p.Filter)
case PT_JSON_VALUE:
res, err := text2JSON(rs)
if err != nil {
return nil, err
}
return callFilter(p, res, p.Filter)
case PT_MAP:
if p.SubItem == nil || len(p.SubItem) <= 0 {
return nil, ErrArrayNeedSubItem
}
res := make(map[string]interface{})
for _, subitem := range p.SubItem {
if len(subitem.Name) == 0 {
continue
}
subitem.CopyFrom(p)
subitem.Name = replaceName(subitem.Name, res)
res[subitem.Name], _ = subitem.pipeText([]byte(rs))
}
return callFilter(p, res, p.Filter)
case PT_RAW:
return callFilter(p, p.Selector, p.Filter)
}
return nil, ErrNotSupportPipeType
}
var namePlaceholder = regexp.MustCompile(`#([^#]*)#`)
func replaceName(name string, data map[string]interface{}) string {
vt := namePlaceholder.FindAllStringSubmatch(name, -1)
for _, v := range vt {
if len(v) < 2 {
continue
}
placeholder := v[1]
var value string
val, ok := data[placeholder]
if ok {
value, ok = val.(string)
if !ok {
value = fmt.Sprint(val)
}
}
name = strings.Replace(name, v[0], value, -1)
}
return name
}
func (p *PipeItem) pipeSelection(s *goquery.Selection) (interface{}, error) {
if p.Type == PT_RAW {
return callFilter(p, p.Selector, p.Filter)
}
var (
sel = htmlSelector{s, "", p.Selector}
err error
)
if strings.HasPrefix(p.Selector, REGEXP_PRE) {
body, _ := sel.Html()
return p.parseRegexp(body, false)
}
if strings.HasPrefix(p.Selector, REGEXP2_PRE) {
body, _ := sel.Html()
return p.parseRegexp(body, true)
}
selector := p.Selector
if len(selector) > 0 {
sel, err = p.parseHTMLSelector(s, selector)
if err != nil {
return nil, err
}
selector = sel.selector
}
if sel.Size() == 0 {
return nil, errors.New("Selector can't Find node: " + selector)
}
if attrExp.MatchString(p.Type) { // 例如:attr[href] 或 attr[src] 等
vt := attrExp.FindStringSubmatch(p.Type)
res, has := sel.Attr(vt[1])
if !has {
return nil, errors.New("Can't Find attribute: " + p.Type + " selector: " + selector)
}
return callFilter(p, res, p.Filter)
}
if attrArrayExp.MatchString(p.Type) { // 例如:attr-array[href] 或 attr-array[src] 等
vt := attrArrayExp.FindStringSubmatch(p.Type)
res := make([]string, 0)
sel.Each(func(index int, child *goquery.Selection) {
href, has := child.Attr(vt[1])
if has {
res = append(res, href)
}
})
return callFilter(p, res, p.Filter)
}
switch p.Type {
case PT_INT, PT_FLOAT, PT_BOOL, PT_STRING, PT_TEXT, PT_INT_ARRAY, PT_FLOAT_ARRAY, PT_BOOL_ARRAY, PT_STRING_ARRAY:
val, err := parseHTMLAttr(sel, p.Type)
if err != nil {
return nil, err
}
return callFilter(p, val, p.Filter)
case PT_HTML_ARRAY:
res := make([]string, 0)
sel.Each(func(index int, child *goquery.Selection) {
str, _ := child.Html()
res = append(res, str)
})
return callFilter(p, res, p.Filter)
case PT_HTML:
var html string
sel.Each(func(idx int, child *goquery.Selection) {
str, _ := child.Html()
html += str
})
return callFilter(p, html, p.Filter)
case PT_OUT_HTML:
var html string
sel.Each(func(idx int, child *goquery.Selection) {
str, _ := goquery.OuterHtml(child)
html += str
})
return callFilter(p, html, p.Filter)
case PT_HREF, PT_IMG_SRC, PT_IMG_ALT:
res, has := sel.Attr(p.Type)
if !has {
return nil, errors.New("Can't Find attribute: " + p.Type + " selector: " + selector)
}
return callFilter(p, res, p.Filter)
case PT_TEXT_ARRAY:
res := make([]string, 0)
sel.Each(func(index int, child *goquery.Selection) {
res = append(res, child.Text())
})
return callFilter(p, res, p.Filter)
case PT_HREF_ARRAY:
res := make([]string, 0)
sel.Each(func(index int, child *goquery.Selection) {
href, has := child.Attr("href")
if has {
res = append(res, href)
}
})
return callFilter(p, res, p.Filter)
case PT_ARRAY:
if p.SubItem == nil || len(p.SubItem) <= 0 {
return nil, ErrArrayNeedSubItem
}
arrayItem := p.SubItem[0]
arrayItem.CopyFrom(p)
res := make([]interface{}, 0)
sel.Each(func(index int, child *goquery.Selection) {
v, _ := arrayItem.pipeSelection(child)
res = append(res, v)
})
return callFilter(p, res, p.Filter)
case PT_MAP:
if p.SubItem == nil || len(p.SubItem) <= 0 {
return nil, ErrArrayNeedSubItem
}
res := make(map[string]interface{})
for _, subitem := range p.SubItem {
if len(subitem.Name) == 0 {
continue
}
subitem.CopyFrom(p)
subitem.Name = replaceName(subitem.Name, res)
res[subitem.Name], _ = subitem.pipeSelection(sel.Selection)
}
return callFilter(p, res, p.Filter)
default:
return callFilter(p, 0, p.Filter)
}
}
func (p *PipeItem) CallFilter(src interface{}, filters string) (interface{}, error) {
return callFilter(p, src, filters)
}
func (p *PipeItem) parseHTMLSelector(s *goquery.Selection, selector string) (htmlSelector, error) {
var attr string
if len(selector) == 0 {
return htmlSelector{s, attr, selector}, nil
}
if strings.HasPrefix(selector, `$.`) {
selector = strings.TrimPrefix(selector, `$`)
s = p.doc.Selection
}
// html: <a class="bn-sharing" data-type="book"></a>
// selector: a.bn-sharing//attr[data-type]
if idx := strings.Index(selector, "//"); idx > 0 {
attr = strings.TrimSpace(selector[idx+2:])
selector = strings.TrimSpace(selector[:idx])
}
// selector: ul > li | eq(2)
subs := SplitParams(selector, `|`)
leng := len(subs)
if leng < 2 {
return htmlSelector{s.Find(selector), attr, selector}, nil
}
subs[0] = strings.TrimSpace(subs[0])
s = s.Find(subs[0])
for i := 1; i < leng; i++ {
subs[i] = strings.TrimSpace(subs[i])
if !fnExp.MatchString(subs[i]) {
return htmlSelector{s, attr, selector}, errors.New("error parse html selector: " + subs[i])
}
vt := fnExp.FindStringSubmatch(subs[i])
fn := vt[1]
var params string
if len(vt) > 3 {
params = strings.TrimSpace(vt[3])
}
switch fn {
case "eq":
pm, _ := strconv.Atoi(params)
s = s.Eq(pm)
case "next":
if len(params) > 0 {
s = s.NextFiltered(params)
} else {
s = s.Next()
}
case "prev":
if len(params) > 0 {
s = s.PrevFiltered(params)
} else {
s = s.Prev()
}
case "first":
s = s.First()
case "last":
s = s.Last()
case "siblings":
if len(params) > 0 {
s = s.SiblingsFiltered(params)
} else {
s = s.Siblings()
}
case "nextall":
if len(params) > 0 {
s = s.NextAllFiltered(params)
} else {
s = s.NextAll()
}
case "children":
if len(params) > 0 {
s = s.ChildrenFiltered(params)
} else {
s = s.Children()
}
case "parent":
if len(params) > 0 {
s = s.ParentFiltered(params)
} else {
s = s.Parent()
}
case "parents":
if len(params) > 0 {
s = s.ParentsFiltered(params)
} else {
s = s.Parents()
}
case "not":
if len(params) > 0 {
s = s.Not(params)
}
case "filter":
if len(params) > 0 {
s = s.Filter(params)
}
case "prevall":
if len(params) > 0 {
s = s.PrevAllFiltered(params)
} else {
s = s.PrevAll()
}
case "rm", "remove":
if len(params) > 0 {
s.Find(params).Remove()
}
case "attr":
if len(params) > 0 {
return htmlSelector{s, `attr[` + params + `]`, selector}, nil
}
}
}
return htmlSelector{s, attr, selector}, nil
}
func parseTextValue(text interface{}, tp string) (interface{}, error) {
switch tp {
case PT_INT, PT_INT_ARRAY:
return text2int(text)
case PT_FLOAT, PT_FLOAT_ARRAY:
return text2float(text)
case PT_BOOL, PT_BOOL_ARRAY:
return text2bool(text)
}
return text, nil
}
func parseHTMLAttr(sel htmlSelector, tp string) (interface{}, error) {
switch tp {
case PT_INT, PT_FLOAT, PT_BOOL, PT_TEXT, PT_STRING:
text, err := getHTMLAttr(sel.Selection, sel.attr, sel.selector)
if err != nil {
return nil, err
}
return parseTextValue(text, tp)
case PT_INT_ARRAY, PT_FLOAT_ARRAY, PT_BOOL_ARRAY, PT_STRING_ARRAY:
text, err := getHTMLAttrArray(sel.Selection, sel.attr, sel.selector)
if err != nil {
return nil, err
}
return parseTextValue(text, tp)
}
return nil, ErrUnknowHTMLAttr
}
func getHTMLAttrArray(sel *goquery.Selection, attr, selector string) ([]string, error) {
res := make([]string, 0)
if len(attr) == 0 {
sel.Each(func(index int, child *goquery.Selection) {
res = append(res, child.Text())
})
return res, nil
}
if attrExp.MatchString(attr) {
vt := attrExp.FindStringSubmatch(attr)
sel.Each(func(index int, child *goquery.Selection) {
text, has := child.Attr(vt[1])
if has {
res = append(res, text)
}
})
return res, nil
}
if attr == "html" {
sel.Each(func(idx int, s1 *goquery.Selection) {
str, _ := s1.Html()
res = append(res, str)
})
return res, nil
}
if attr == "outhtml" {
sel.Each(func(idx int, s1 *goquery.Selection) {
str, _ := goquery.OuterHtml(s1)
res = append(res, str)
})
return res, nil
}
return res, nil
}
func getHTMLAttr(sel *goquery.Selection, attr, selector string) (string, error) {
if len(attr) == 0 {
return sel.Text(), nil
}
if attrExp.MatchString(attr) {
vt := attrExp.FindStringSubmatch(attr)
res, has := sel.Attr(vt[1])
if !has {
return "", errors.New("Can't Find attribute: " + attr + " selector: " + selector)
}
return res, nil
}
if attr == "html" {
var html string
sel.Each(func(idx int, s1 *goquery.Selection) {
str, _ := s1.Html()
html += str
})
return html, nil
}
if attr == "outhtml" {
var html string
sel.Each(func(idx int, s1 *goquery.Selection) {
str, _ := goquery.OuterHtml(s1)
html += str
})
return html, nil
}
return sel.Text(), nil
}
func parseJSONSelector(js *simplejson.Json, selector string) (*simplejson.Json, error) {
subs := strings.Split(selector, ".")
for _, s := range subs {
if index := strings.Index(s, "["); index >= 0 {
if index > 0 {
k := s[:index]
if k != "this" {
js = js.Get(k)
}
}
s = s[index:]
if !jsonNumberIndexExp.MatchString(s) {
return nil, errors.New("parse json selector error: " + selector)
}
v := jsonNumberIndexExp.FindStringSubmatch(s)
intV, err := strconv.Atoi(v[1])
if err != nil {
return nil, err
}
js = js.GetIndex(intV)
} else {
if s == "this" {
continue
}
js = js.Get(s)
}
}
return js, nil
}
func (p *PipeItem) pipeJSON(body []byte) (interface{}, error) {
if p.Type == PT_RAW {
return callFilter(p, p.Selector, p.Filter)
}
js, err := simplejson.NewJson(body)
if err != nil {
return nil, err
}
if len(p.Selector) > 0 {
js, err = parseJSONSelector(js, p.Selector)
if err != nil {
return nil, err
}
}
switch p.Type {
case PT_INT:
return callFilter(p, js.MustInt64(0), p.Filter)
case PT_FLOAT:
return callFilter(p, js.MustFloat64(0.0), p.Filter)
case PT_BOOL:
return callFilter(p, js.MustBool(false), p.Filter)
case PT_TEXT, PT_STRING:
return callFilter(p, js.MustString(""), p.Filter)
case PT_TEXT_ARRAY, PT_STRING_ARRAY:
v, err := js.StringArray()
if err != nil {
return nil, err
}
return callFilter(p, v, p.Filter)
case PT_JSON_VALUE:
return callFilter(p, js.Interface(), p.Filter)
case PT_JSON_PARSE:
if p.SubItem == nil || len(p.SubItem) <= 0 {
return nil, ErrJsonparseNeedSubItem
}
bodyStr := strings.TrimSpace(js.MustString(""))
if len(bodyStr) == 0 {
return nil, nil
}
body, err := text2JSONByte(bodyStr)
if err != nil {
return nil, errors.New("jsonparse: text is not a json string: " + err.Error())
}
parseItem := p.SubItem[0]
parseItem.CopyFrom(p)
res, err := parseItem.pipeJSON(body)
if err != nil {
return nil, err
}
return callFilter(p, res, p.Filter)
case PT_ARRAY:
v, err := js.Array()
if err != nil {
return nil, err
}
if p.SubItem == nil || len(p.SubItem) <= 0 {
return nil, ErrArrayNeedSubItem
}
arrayItem := p.SubItem[0]
arrayItem.CopyFrom(p)
res := make([]interface{}, 0)
for _, r := range v {
data, _ := json.Marshal(r)
vl, _ := arrayItem.pipeJSON(data)
res = append(res, vl)
}
return callFilter(p, res, p.Filter)
case PT_MAP:
if p.SubItem == nil || len(p.SubItem) <= 0 {
return nil, ErrArrayNeedSubItem
}
data, _ := json.Marshal(js)
res := make(map[string]interface{})
for _, subitem := range p.SubItem {
if len(subitem.Name) == 0 {
continue
}
subitem.CopyFrom(p)
subitem.Name = replaceName(subitem.Name, res)
res[subitem.Name], _ = subitem.pipeJSON(data)
}
return callFilter(p, res, p.Filter)
default:
return callFilter(p, 0, p.Filter)
}
}
func (p *PipeItem) pipeText(body []byte) (interface{}, error) {
if p.Type == PT_RAW {
return callFilter(p, p.Selector, p.Filter)
}
bodyStr := string(body)
if strings.HasPrefix(p.Selector, REGEXP_PRE) {
return p.parseRegexp(bodyStr, false)
}
if strings.HasPrefix(p.Selector, REGEXP2_PRE) {
return p.parseRegexp(bodyStr, true)
}
switch p.Type {
case PT_INT, PT_FLOAT, PT_BOOL:
val, err := parseTextValue(bodyStr, p.Type)
if err != nil {
return nil, err
}
return callFilter(p, val, p.Filter)
case PT_TEXT, PT_STRING:
return callFilter(p, bodyStr, p.Filter)
case PT_JSON_PARSE:
if p.SubItem == nil || len(p.SubItem) <= 0 {
return nil, ErrJsonparseNeedSubItem
}
body, err := text2JSONByte(bodyStr)
if err != nil {
return nil, errors.New("jsonparse: text is not a json string: " + err.Error())
}
parseItem := p.SubItem[0]
parseItem.CopyFrom(p)
res, err := parseItem.pipeJSON(body)
if err != nil {
return nil, err
}
return callFilter(p, res, p.Filter)
case PT_JSON_VALUE:
res, err := text2JSON(string(body))
if err != nil {
return nil, err
}
return callFilter(p, res, p.Filter)
case PT_MAP:
if p.SubItem == nil || len(p.SubItem) <= 0 {
return nil, ErrArrayNeedSubItem
}
res := make(map[string]interface{})
for _, subitem := range p.SubItem {
if len(subitem.Name) == 0 {
continue
}
subitem.Name = replaceName(subitem.Name, res)
res[subitem.Name], _ = subitem.pipeText(body)
}
return callFilter(p, res, p.Filter)
default:
return callFilter(p, 0, p.Filter)
}
}
func text2int(text interface{}) (interface{}, error) {
switch val := text.(type) {
case string:
return strconv.ParseInt(val, 10, 64)
case []string:
vs := make([]int64, 0)
for _, v := range val {
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, err
}
vs = append(vs, n)
}
return vs, nil
}
return nil, ErrUnsupportText2intType
}
func text2float(text interface{}) (interface{}, error) {
switch val := text.(type) {
case string:
return strconv.ParseFloat(val, 64)
case []string:
vs := make([]float64, 0)
for _, v := range val {
n, err := strconv.ParseFloat(v, 64)
if err != nil {
return nil, err
}
vs = append(vs, n)
}
return vs, nil
}
return nil, ErrUnsupportText2floatType
}
func text2bool(text interface{}) (interface{}, error) {
switch val := text.(type) {
case string:
return strconv.ParseBool(val)
case []string:
vs := make([]bool, 0)
for _, v := range val {
n, err := strconv.ParseBool(v)
if err != nil {
return nil, err
}
vs = append(vs, n)
}
return vs, nil
}
return nil, ErrUnsupportText2boolType
}
func text2JSON(text string) (interface{}, error) {
res, err := textJSONValue(text)
if err != nil {
return untextJSONValue(text)
}
return res, nil
}
func text2JSONByte(text string) ([]byte, error) {
val, err := text2JSON(text)
if err != nil {
return nil, err
}
return json.Marshal(val)
}
func textJSONValue(text string) (interface{}, error) {
res := map[string]interface{}{}
if err := json.Unmarshal([]byte(text), &res); err != nil {
resarray := make([]interface{}, 0)
if err = json.Unmarshal([]byte(text), &resarray); err != nil {
return nil, errors.New("parse json value error, text is not json value: " + err.Error())
}
return resarray, nil
}
return res, nil
}
func untextJSONValue(text string) (interface{}, error) {
text, err := strconv.Unquote(`"` + text + `"`)
if err != nil {
return nil, err
}
return textJSONValue(text)
}