-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpublish_time.go
110 lines (96 loc) · 3.38 KB
/
publish_time.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
package textractor
import (
"regexp"
"sort"
"strings"
"unicode/utf8"
"github.com/PuerkitoBio/goquery"
)
type meta struct {
attrName string
attrVal string
timeAttr string
}
var timePattern = []string{
`(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9])`,
`(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9])`,
`(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[1-24]\d时[0-60]\d分)([1-24]\d时)`,
`(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9])`,
`(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9])`,
`(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2}\s*?[1-24]\d时[0-60]\d分)([1-24]\d时)`,
`(\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{1,2}[-|/|.]\d{1,2}\s*?[0-1]?[0-9]:[0-5]?[0-9])`,
`(\d{1,2}[-|/|.]\d{1,2}\s*?[2][0-3]:[0-5]?[0-9])`,
`(\d{1,2}[-|/|.]\d{1,2}\s*?[1-24]\d时[0-60]\d分)([1-24]\d时)`,
`(\d{4}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{4}年\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{4}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9])`,
`(\d{4}年\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9])`,
`(\d{4}年\d{1,2}月\d{1,2}日\s*?[1-24]\d时[0-60]\d分)([1-24]\d时)`,
`(\d{2}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{2}年\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{2}年\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9])`,
`(\d{2}年\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9])`,
`(\d{2}年\d{1,2}月\d{1,2}日\s*?[1-24]\d时[0-60]\d分)([1-24]\d时)`,
`(\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9]:[0-5]?[0-9])`,
`(\d{1,2}月\d{1,2}日\s*?[0-1]?[0-9]:[0-5]?[0-9])`,
`(\d{1,2}月\d{1,2}日\s*?[2][0-3]:[0-5]?[0-9])`,
`(\d{1,2}月\d{1,2}日\s*?[1-24]\d时[0-60]\d分)([1-24]\d时)`,
`(\d{4}[-|/|.]\d{1,2}[-|/|.]\d{1,2})`,
`(\d{2}[-|/|.]\d{1,2}[-|/|.]\d{1,2})`,
`(\d{1,2}[-|/|.]\d{1,2})`,
`(\d{4}年\d{1,2}月\d{1,2}日)`,
`(\d{2}年\d{1,2}月\d{1,2}日)`,
`(\d{1,2}月\d{1,2}日)`,
}
var timeRx []*regexp.Regexp
func init() {
for _, v := range timePattern {
if rx, err := regexp.Compile(v); err != nil {
panic(err)
} else {
timeRx = append(timeRx, rx)
}
}
}
// Extract 提取发布时间
func timeExtract(headText []*headEntry, body *goquery.Selection) string {
var mats []string
for _, v := range headText {
if timeVal, ok := matchTime(v.val); ok {
mats = append(mats, timeVal)
}
}
for _, v := range iterator(body) {
if goquery.NodeName(v) == "#text" {
t := strings.TrimSpace(v.Text())
length := utf8.RuneCountInString(t)
if t != "" && length >= 4 && length <= 50 {
if timeVal, ok := matchTime(t); ok {
mats = append(mats, timeVal)
}
}
}
}
if len(mats) > 0 {
sort.Slice(mats, func(i, j int) bool {
return len(mats[i]) > len(mats[j])
})
return mats[0]
}
return ""
}
func matchTime(text string) (string, bool) {
for _, rx := range timeRx {
if rx.MatchString(text) {
return rx.FindString(text), true
}
}
return "", false
}