-
Notifications
You must be signed in to change notification settings - Fork 1
/
article.go
82 lines (69 loc) · 1.64 KB
/
article.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
package articler
import (
"fmt"
"net/url"
"sync"
"time"
)
type Article struct {
Title string
Text string
Published time.Time
Source string
Parsed string
/* Images []string
Videos []string
Links []string
Authors []string
Tags []string
Catigories []string
Photographers []string*/
}
type Articles []*Article
func (a Articles) Len() int { return len(a) }
func (a Articles) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Articles) Less(i, j int) bool { return a[i].Published.Sub(a[j].Published) < 0 }
type ArticleParserAdapter interface {
Parse(string, []byte) (*Article, error)
IsArticle(string) bool
}
var (
articleAdaptersMu sync.Mutex
articleAdapters = map[string]ArticleParserAdapter{}
)
func RegisterArticleParser(name string, adapter ArticleParserAdapter) {
if adapter == nil {
panic("adapter is nil")
}
articleAdaptersMu.Lock()
defer articleAdaptersMu.Unlock()
if _, dup := articleAdapters[name]; dup {
panic("sql: Register called twice for adapter " + name)
}
articleAdapters[name] = adapter
}
var dp = &DefaultArticleParser{}
func ParseArticle(URL string, in []byte) (art *Article, e error) {
var (
u *url.URL
parser ArticleParserAdapter
)
u, e = url.Parse(URL)
if e != nil {
return
}
for k := range articleAdapters {
if k == u.Host {
parser = articleAdapters[k]
}
}
if parser != nil && parser.IsArticle(u.RequestURI()) {
return parser.Parse(URL, in)
} else {
if parser, ok := articleAdapters["default"]; ok {
return parser.Parse(URL, in)
}
return dp.Parse(URL, in)
}
return nil, fmt.Errorf("Adapter not found")
}