crawler_go/crawler.go

64 lines
1.3 KiB
Go
Raw Normal View History

2017-04-10 09:03:31 +00:00
package crawler
2017-04-12 11:14:09 +00:00
import "errors"
2017-04-11 08:42:02 +00:00
2017-04-10 09:03:31 +00:00
type Internal interface {
2017-04-12 11:14:09 +00:00
Internal(params map[string]interface{}) ([]byte, error)
2017-04-10 09:03:31 +00:00
}
type Crawler interface {
2017-04-12 11:14:09 +00:00
Init(path string) ([]byte,error)
Add(id string) ([]byte,error)
Remove(id string) ([]byte,error)
Get(id string) ([]byte,error)
2017-04-10 09:03:31 +00:00
}
type CrawlerImpl struct {
configs map[string]interface{}
internal Internal
}
2017-04-12 11:14:09 +00:00
func (c *CrawlerImpl) Init(path string) ([]byte,error) {
2017-04-10 09:03:31 +00:00
if c.configs == nil {
c.configs = make(map[string]interface{}, 0)
}
// load all file in path
2017-04-12 11:14:09 +00:00
return nil,nil
2017-04-10 09:03:31 +00:00
}
2017-04-12 11:14:09 +00:00
func (c *CrawlerImpl) Add(id string) ([]byte,error) {
2017-04-10 09:03:31 +00:00
//file load , input config
2017-04-12 11:14:09 +00:00
return nil,nil
2017-04-10 09:03:31 +00:00
}
2017-04-12 11:14:09 +00:00
func (c *CrawlerImpl) Remove(id string) ([]byte,error) {
2017-04-10 09:03:31 +00:00
//remove in config
2017-04-12 11:14:09 +00:00
return nil,nil
2017-04-10 09:03:31 +00:00
}
2017-04-12 11:14:09 +00:00
func (c *CrawlerImpl) Get(id string)([]byte,error) {
2017-04-10 09:03:31 +00:00
if c.internal != nil {
out, err := c.internal.Internal(c.GetConfig(id))
if err != nil {
//set error fail, message
2017-04-12 11:14:09 +00:00
return nil,err
2017-04-10 09:03:31 +00:00
}
2017-04-12 11:14:09 +00:00
return out, nil
2017-04-10 09:03:31 +00:00
}
2017-04-12 11:14:09 +00:00
return nil, errors.New("Not Assign")
2017-04-10 09:03:31 +00:00
}
// internal methods
func (c *CrawlerImpl) GetConfig(id string) map[string]interface{} {
return c.configs[id].(map[string]interface{})
}
func (c *CrawlerImpl) SetInternal(i Internal) {
c.internal = i
}
func (c *CrawlerImpl) PutConfig(name string, m map[string]interface{}) {
if c.configs == nil {
c.configs = make(map[string]interface{}, 0)
}
c.configs[name] = m
}