-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaodou.go
218 lines (188 loc) · 4.57 KB
/
maodou.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
package maodou
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/mnhkahn/gogogo/logger"
"github.com/mnhkahn/maodou/dao"
"github.com/mnhkahn/maodou/models"
)
type Handler interface {
Init(link string)
Cawl(paras ...interface{}) (*Response, error)
Start(link string) (*Response, error)
Index(resp *Response, jobs chan string) error
Detail(resp *Response) (*models.Result, error)
Result(result *models.Result) error
Config() *HandlerConfig
}
type HandlerConfig struct {
d bool
link string // start crawl link
dao_name string
dsn string
cawl_every time.Duration
interval time.Duration
ip string
jobs chan string
wg sync.WaitGroup
}
type MaoDou struct {
Dao dao.DaoContainer
req *Request
resp *Response
settings *HandlerConfig
Debug bool
}
func (this *MaoDou) SetRate(times ...time.Duration) {
if len(times) == 1 {
this.settings.cawl_every = times[0]
} else if len(times) == 2 {
this.settings.cawl_every = times[0]
this.req.Interval = times[1]
}
}
func (this *MaoDou) SetProxy(proxy_name, proxy_dsn string) {
this.req.InitProxy(proxy_name, proxy_dsn)
}
func (this *MaoDou) SetD(d bool) {
this.settings.d = d
}
func (this *MaoDou) SetDao(dao_name, dsn string) {
if this.Dao != nil {
this.Dao.Close()
}
var err error
this.settings.dao_name, this.settings.dsn = dao_name, dsn
this.Dao, err = dao.NewDao(this.settings.dao_name, this.settings.dsn)
if err != nil {
panic(err)
}
}
func (this *MaoDou) SetJobLen(l int) {
if l <= 0 {
l = 1
}
this.settings.jobs = make(chan string, l)
}
func (this *MaoDou) Init(link string) {
var err error
this.settings = new(HandlerConfig)
this.settings.link = link
this.settings.cawl_every = 0
this.settings.interval = 0
this.req = NewRequest(0)
this.SetJobLen(1)
this.SetDao("peanut", "./maodou.db")
this.SetD(true)
if err != nil {
panic(err)
}
}
func (this *MaoDou) Start(link string) (*Response, error) {
resp, err := this.Cawl(link)
if err != nil {
return resp, err
}
return resp, nil
}
func (this *MaoDou) Cawl(paras ...interface{}) (*Response, error) {
return this.req.Cawl(paras...)
}
func (this *MaoDou) Index(resp *Response, jobs chan string) error {
return fmt.Errorf("the Index Method is not override")
}
func (this *MaoDou) AddJob(job string) {
this.settings.jobs <- job
}
func (this *MaoDou) Detail(resp *Response) (*models.Result, error) {
return nil, fmt.Errorf("the Detail is not override")
}
func (this *MaoDou) Result(result *models.Result) error {
if this.Dao != nil {
return this.Dao.AddResult(result)
}
return fmt.Errorf("dao is not init")
}
func (this *MaoDou) Config() *HandlerConfig {
return this.settings
}
var APP *App
type App struct {
handler Handler
}
func NewController(handler Handler) *App {
app := new(App)
app.handler = handler
return app
}
func (this *App) Run() error {
duration := time.Duration(0)
if this.handler.Config() != nil && this.handler.Config().cawl_every > 0 {
duration = this.handler.Config().cawl_every
}
APP.run()
if duration > 0 {
timer := time.NewTicker(duration)
for {
select {
case <-timer.C:
go func() {
APP.run()
}()
}
}
}
return nil
}
func (this *App) run() {
var done = make(chan struct{})
logger.Infof("[INFO] Start parse at %s...\n", time.Now().Format(time.RFC3339))
if resp, err := this.handler.Start(this.handler.Config().link); err == nil {
// produce
go this.handler.Index(resp, this.handler.Config().jobs)
// consume
go func() {
for job := range this.handler.Config().jobs {
logger.Debug("do job:", job)
if resp, err := this.handler.Cawl(job); err == nil {
if res, err := this.handler.Detail(resp); err == nil {
if err := this.handler.Result(res); err != nil {
logger.Warn(err)
}
} else {
logger.Warn(err)
}
} else {
logger.Warn(err)
}
}
if this.handler.Config().d {
done <- struct{}{}
}
}()
if this.handler.Config().d {
<-done
}
} else {
logger.Warn(err)
}
logger.Infof("[SUCC] Parse end.\n")
}
func Register(handler Handler) {
APP = NewController(handler)
APP.Run()
}
func init() {
rand.Seed(time.Now().UnixNano())
println(CYEAM_LOG)
}
var CYEAM_LOG = `
______ _
| ___ \ | |
| | _ | | ____ ___ _ | | ___ _ _ ____ _____ _ _ _
| || || |/ _ |/ _ \ / || |/ _ \| | | | ___ / _\ V / __| / \ | \_/ |
| || || ( ( | | |_| ( (_| | |_| | |_| | (___) ( (_ \ /| _| | o || \_/ |
|_||_||_|\_||_|\___/ \____|\___/ \____| \__||_||___||_n_||_| |_|
`