-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharticler.go
59 lines (48 loc) · 1.2 KB
/
articler.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
package articler
import (
"errors"
"io/ioutil"
"strings"
)
type Articler struct {
conf *Config
fetcher Fetcher
defaultArticleParser *DefaultArticleParser
}
func New(configs ...*Config) (art *Articler, e error) {
art = &Articler{}
art.defaultArticleParser = NewDefaultArticleParser()
RegisterArticleParser("default", art.defaultArticleParser)
art.fetcher = &DefaultFetcher{}
if len(configs) == 1 {
art.conf = configs[0]
if filepath := art.conf.DefaultArticleParserConf; filepath != "" {
e = art.defaultArticleParser.LoadRules(filepath)
if e != nil {
return
}
}
}
return
}
/*func NewFromFile(filepath string) (art *Articler, e error) {
}*/
func (a *Articler) ParseArticleFromUrl(URL string) (*Article, error) {
resp, e := a.fetcher.Get(URL)
if e != nil {
return nil, e
}
defer resp.Body.Close()
contentType := resp.Header.Get("Content-Type")
if !strings.Contains(contentType, "text/html") {
return nil, errors.New("Content-type not text/html")
}
bts, e := ioutil.ReadAll(resp.Body)
if e != nil {
return nil, e
}
return ParseArticle(URL, bts)
}
func (a *Articler) ParseArticle(urlContext string, data []byte) (*Article, error) {
return ParseArticle(urlContext, data)
}