forked from mediahome/javscrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.go
231 lines (202 loc) · 4.36 KB
/
scrape.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package scrape
import (
"strings"
"sync"
"github.com/goextension/log"
"github.com/goextension/log/zap"
)
// RangeFunc ...
type RangeFunc func(key string, content Content) error
// IScrape ...
type IScrape interface {
Cache() *Cache
IsGrabSample() (b bool)
Find(name string) (e error)
Clear()
Range(rangeFunc RangeFunc) error
Output() error
}
type scrapeImpl struct {
contents map[string][]Content
grabs []IGrab
sample bool
cache *Cache
output string
infoName string
exact bool
}
var debug = false
// DefaultInfoName ...
var DefaultInfoName = ".info"
// Options ...
type Options func(impl *scrapeImpl)
// IsGrabSample ...
func (impl *scrapeImpl) IsGrabSample() bool {
return impl.sample
}
// CacheOption ...
func CacheOption(cache *Cache) Options {
return func(impl *scrapeImpl) {
impl.cache = cache
}
}
// MergeOptimize ...
func MergeOptimize(id string, contents []*Content) *Content {
var content *Content
for _, c := range contents {
if strings.ToUpper(c.ID) == strings.ToUpper(id) {
if content == nil {
content = c
continue
}
}
if content == nil {
continue
}
if strings.ToUpper(content.ID) == strings.ToUpper(c.ID) {
if len(content.Sample) < len(c.Sample) {
log.Infow("optimize", "field", "sample")
content.Sample = c.Sample
}
if len(content.Genres) < len(c.Genres) {
log.Infow("optimize", "field", "genre")
content.Genres = c.Genres
}
if len(content.Actors) < len(c.Actors) {
log.Infow("optimize", "field", "actor")
content.Actors = c.Actors
}
}
}
return content
}
// SampleOption ...
func SampleOption(b bool) Options {
return func(impl *scrapeImpl) {
impl.sample = b
}
}
// ExactOption ...
func ExactOption(b bool) Options {
return func(impl *scrapeImpl) {
impl.exact = b
}
}
// GrabOption ...
func GrabOption(grab IGrab) Options {
return func(impl *scrapeImpl) {
impl.grabs = append(impl.grabs, grab)
}
}
// DebugOn ...
func DebugOn() {
debug = true
}
// NewScrape ...
func NewScrape(opts ...Options) IScrape {
scrape := &scrapeImpl{
contents: make(map[string][]Content),
sample: true,
exact: false,
output: DefaultOutputPath,
infoName: DefaultInfoName,
}
for _, opt := range opts {
opt(scrape)
}
scrape.init()
return scrape
}
// Clear ...
func (impl *scrapeImpl) Clear() {
impl.contents = make(map[string][]Content)
}
// Output ...
func (impl *scrapeImpl) Output() error {
return impl.Range(func(key string, content Content) (e error) {
e = copyInfo(&content, DefaultOutputPath, DefaultInfoName)
if e != nil {
log.Errorw("copy info", "error", e, "output", key)
}
e = copyCache(impl.cache, &content, impl.sample, DefaultOutputPath)
if e != nil {
log.Errorw("copy cache", "error", e, "output", key)
}
return nil
})
}
func init() {
zap.InitZapSugar()
}
// Cache ...
func (impl *scrapeImpl) Cache() *Cache {
return impl.cache
}
// Range ...
func (impl *scrapeImpl) Range(rangeFunc RangeFunc) error {
for key, value := range impl.contents {
log.Infow("range", "key", key)
for _, v := range value {
if v.ID == "" {
continue
}
e := rangeFunc(key, v)
if e != nil {
return e
}
}
}
return nil
}
// Find ...
func (impl *scrapeImpl) Find(name string) (e error) {
chanContent := make(chan Content, 1)
wg := &sync.WaitGroup{}
for _, grab := range impl.grabs {
wg.Add(1)
go func(grab IGrab, cctx chan<- Content) {
defer wg.Done()
grab.SetExact(impl.exact)
grab.SetSample(impl.sample)
iGrab, e := grab.Find(name)
if e != nil {
log.Errorw("error", "error", e, "name", grab.Name(), "find", name)
return
}
cs, e := iGrab.Result()
if e != nil {
log.Errorw("error", "error", e, "name", grab.Name(), "decode", name)
}
if debug {
log.Infow("find", "result", cs)
}
for _, c := range cs {
cctx <- c
}
}(grab, chanContent)
}
go func(cctx chan<- Content) {
wg.Wait()
close(cctx)
}(chanContent)
for content := range chanContent {
e = imageCache(impl.cache, content, impl.sample)
if e != nil {
log.Errorw("error", "cache", content.ID, "error", e)
}
if v, b := impl.contents[content.ID]; b {
impl.contents[content.ID] = append(v, content)
} else {
impl.contents[content.ID] = []Content{content}
}
if debug {
log.Infow("find", "content", content)
}
}
return nil
}
func (impl *scrapeImpl) init() {
if impl.cache == nil {
impl.cache = NewCache()
}
}