-
Notifications
You must be signed in to change notification settings - Fork 41
/
spider.go
33 lines (28 loc) · 903 Bytes
/
spider.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
package antch
import (
"io"
"io/ioutil"
"net/http"
)
// Handler is the HTTP Response handler interface that defines
// how to extract scraped items from their pages.
//
// ServeSpider should be write got Item to the Channel.
type Handler interface {
ServeSpider(chan<- Item, *http.Response)
}
// HandlerFunc is an adapter to allow the use of ordinary
// functions as Spider.
type HandlerFunc func(chan<- Item, *http.Response)
// ServeSpider performs extract data from received HTTP response and
// write it into the Channel c.
func (f HandlerFunc) ServeSpider(c chan<- Item, resp *http.Response) {
f(c, resp)
}
// VoidHandler returns a Handler that without doing anything.
func VoidHandler() Handler {
return HandlerFunc(func(_ chan<- Item, resp *http.Response) {
// https://stackoverflow.com/questions/17948827/reusing-http-connections-in-golang
io.Copy(ioutil.Discard, resp.Body)
})
}