// Extract favicon
func extract(r *Req) (*Res, error) {
	e := gofavicon.NewExtractor()

	d, err := resolveDomain(r.Domain, 10*time.Second)
	if err != nil {
		return nil, err
	}

	ico, err := e.Extract(d)
	if err != nil {
		return nil, err
	}

	var changed bool

	hash := fmt.Sprintf("%x", sha1.Sum(ico.Image))

	if !strings.EqualFold(hash, r.PreviousHash) {
		changed = true
	}

	var fpath string

	if changed {
		file, _ := ioutil.TempFile(outputDir, "")
		file.Write(ico.Image)
		fpath = file.Name() + ico.ImageExt
		if file.Name() != fpath {
			os.Rename(file.Name(), fpath)
		}
	}

	// run icon processor
	if len(iconProcessor) > 0 {
		abspath, err := filepath.Abs(fpath)
		if err != nil {
			return nil, err
		}

		b, err := execProcessor(abspath)
		if err != nil {
			return nil, fmt.Errorf("can't process file %s, error: %s, domain: %s", abspath, err, d)
		}

		fpath = strings.TrimSpace(string(b))
	}

	res := &Res{
		Domain:            d,
		ID:                r.ID,
		PreviousHash:      r.PreviousHash,
		PreviousFetchTime: r.PreviousFetchTime,
		NewFetchTime:      time.Now().Format(time.RFC3339),
		NewHash:           hash,
		Changed:           changed,
		IconFile:          fpath,
	}

	return res, nil
}
func TestExtractor(t *testing.T) {
	extractor := gofavicon.NewExtractor()
	ico, err := extractor.Extract("http://amazon.com")
	if err != nil {
		t.Error(err)
	}
	_ = ico
	//	if len(ico.Image) == 0 {
	//		t.Error("Image len is zero")
	//	}
}