-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
42 lines (38 loc) · 1.22 KB
/
parser.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
package parser
import (
"errors"
"github.com/SunMaybo/go-readability"
"github.com/SunMaybo/jewel-crawler/logs"
"github.com/storyicon/graphquery"
"strings"
"time"
)
func Parser(content, pattern string) (interface{}, error) {
response := graphquery.ParseFromString(content, pattern)
if response.Data != nil {
return response.Data, nil
} else {
logs.S.Warnw("parser error ", "response", response)
return nil, errors.New("parser error data is nil")
}
}
func ParserArticleWithReadability(html string, url string) (map[string]interface{}, error) {
starTime := time.Now()
parser := readability.NewParser()
article, err := parser.Parse(strings.NewReader(html), url)
logs.S.Infow("解析耗时", "interval", time.Since(starTime).String())
if err != nil {
logs.S.Warn(err)
logs.S.Warnw("规则匹配耗时", "interval", time.Since(starTime).String())
return nil, err
} else {
resultMap := make(map[string]interface{})
resultMap["title"] = article.Title
resultMap["author"] = article.Byline
resultMap["source_name"] = article.SourceName
resultMap["content"] = article.Content
resultMap["image"] = article.Image
logs.S.Infow("规则匹配耗时", "interval", time.Since(starTime).String())
return resultMap, nil
}
}