Я настраиваю функцию кодировщика изображений, при которой вы вводите URL-адрес изображения, и он возвращает версию ISO-8859-1.Как написать функцию, которая отправляет HTTP-запрос GET на URL-адрес и преобразует эти байты в ISO-8859-1?Код ниже - это все, что у меня есть.
func grabImageBytes(imageURL string) ([]byte, error) {
req, _ := http.NewRequest("GET", imageURL, nil)
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
} else {
return body, nil
}
}
другие функции:
func getRandomImage(keyword string) (string, error) {
req, _ := http.NewRequest("GET", "https://www.google.com/search?tbm=isch&q="+keyword, nil)
req.Header.Add("authority", "www.google.com")
req.Header.Add("upgrade-insecure-requests", "1")
req.Header.Add("referer", "https://images.google.com/")
req.Header.Add("accept-language", "en-US,en;q=0.9")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
var imageURL string
if strings.Contains(string(body), ",\"ou\":\"") {
imageURL = strings.Split(strings.Split(string(body), ",\"ou\":\"")[1], "\",\"ow\":")[0]
} else {
return "", errors.New("Image not found.")
}
req2, _ := http.NewRequest("GET", imageURL, nil)
res2, _ := http.DefaultClient.Do(req2)
defer res2.Body.Close()
if res2.StatusCode == 404 {
return "", errors.New("Image not found.")
} else {
return imageURL, nil
}
}