package fetcher

import (
	"bufio"
	"fmt"
	"golang.org/x/net/html/charset"
	"golang.org/x/text/encoding"
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
	"io"
	"io/ioutil"
	"log"
	"net/http"
)

func Fetch(url string) ([]byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		//panic(err)
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		fmt.Println(" Error: status code ", resp.StatusCode)
		return nil, fmt.Errorf(" wrong status code: %d", resp.StatusCode)
	}
	// 自动发现见面编码
	e := determineEncoding(resp.Body)
	// 把内容以GBK编码读取
	utf8Reader := transform.NewReader(resp.Body, e.NewDecoder())
	return ioutil.ReadAll(utf8Reader)
}

func determineEncoding(r io.Reader) encoding.Encoding {

	bytes, err := bufio.NewReader(r).Peek(1024)
	if err != nil {
		log.Printf("Fetcher error : %v", err)
		return unicode.UTF8 //返回默认UTF8
	}
	// 返回 encoding
	e, _, _ := charset.DetermineEncoding(
		bytes,
		"",
	)
	//fmt.Printf(" name %s certain %s", name, certain)
	return e
}
