-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
149 lines (130 loc) · 3.44 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
package facebook
import (
"bytes"
"compress/gzip"
"net/http"
"regexp"
"time"
"github.com/tamboto2000/jsonextract/v3"
)
func findObj(jsons []*jsonextract.JSON, onFound func(json *jsonextract.JSON) bool) bool {
for _, json := range jsons {
if json.Kind() == jsonextract.Object {
if onFound(json) {
return true
}
}
if json.Kind() == jsonextract.Array {
if findObj(json.Array(), onFound) {
return true
}
}
}
return false
}
func findKeyObj(jsons []*jsonextract.JSON, key string, onFound func(parent *jsonextract.JSON, obj *jsonextract.JSON) bool) bool {
for _, json := range jsons {
if json.Kind() == jsonextract.Object {
if val, ok := json.Object()[key]; ok {
if val.Kind() == jsonextract.Object {
if onFound(json, val) {
return true
}
}
}
for _, val := range json.Object() {
if findKeyObj([]*jsonextract.JSON{val}, key, onFound) {
return true
}
}
}
if json.Kind() == jsonextract.Array {
if findKeyObj(json.Array(), key, onFound) {
return true
}
}
}
return false
}
func decompressResponseBody(resp *http.Response) (*bytes.Buffer, error) {
buff := new(bytes.Buffer)
switch resp.Header.Get("Content-Encoding") {
case "gzip":
reader, err := gzip.NewReader(resp.Body)
if err != nil {
return nil, err
}
buff.ReadFrom(reader)
default:
buff.ReadFrom(resp.Body)
}
return buff, nil
}
func extractDate(text string) (dates []time.Time, datesStr []string) {
dPattern1 := `[a-zA-Z]+ \d{1,2}, \d{4}`
dPattern2 := `[a-zA-Z]+ \d{4}`
dPattern3 := `\d{4}`
// try to get dates from dPattern1
rgx, _ := regexp.Compile(dPattern1)
if matchs := rgx.FindAllString(text, -1); len(matchs) > 0 {
for _, match := range matchs {
if date, err := time.Parse("January 02, 2006", match); err == nil {
dates = append(dates, date)
datesStr = append(datesStr, match)
} else if date, err := time.Parse("January 2, 2006", match); err == nil {
dates = append(dates, date)
datesStr = append(datesStr, match)
}
}
}
// try to get dates from dPattern2
if len(dates) < 2 && len(datesStr) < 2 {
rgx, _ = regexp.Compile(dPattern2)
if matchs := rgx.FindAllString(text, -1); len(matchs) > 0 {
for _, match := range matchs {
if date, err := time.Parse("January 2006", match); err == nil {
dates = append(dates, date)
datesStr = append(datesStr, match)
}
}
}
}
// try to get dates from dPattern3
if len(dates) < 2 && len(datesStr) < 2 {
rgx, _ = regexp.Compile(dPattern3)
if matchs := rgx.FindAllString(text, -1); len(matchs) > 0 {
if len(dates) == 1 {
if len(matchs) == 2 {
match := matchs[len(matchs)-1]
if date, err := time.Parse("2006", match); err == nil {
dates = append(dates, date)
datesStr = append(datesStr, match)
}
}
}
if len(dates) == 0 {
for _, match := range matchs {
if date, err := time.Parse("2006", match); err == nil {
dates = append(dates, date)
datesStr = append(datesStr, match)
}
}
}
// if len(dates) > 1 {
// match := matchs[len(matchs)-1]
// if date, err := time.Parse("2006", match); err == nil {
// dates = append(dates, date)
// datesStr = append(datesStr, match)
// }
// } else {
// for _, match := range matchs {
// if date, err := time.Parse("2006", match); err == nil {
// dates = append(dates, date)
// datesStr = append(datesStr, match)
// }
// }
// }
}
}
return dates, datesStr
}