dongmeixian9665 2018-12-24 05:23
浏览 126
已采纳

如何转换图像以上传Spotify个人资料图片?

I'm setting up an image encoder function where you input an image URL and it returns the ISO-8859-1 version of it. How would I write a function that sends an HTTP GET request to the URL and converts those bytes to ISO-8859-1? The code below is everything I have so far.

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
    }
}

other func:

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
    }

}
  • 写回答

1条回答 默认 最新

  • doujiu3097 2018-12-24 06:29
    关注

    Your claim that the Spotify profile picture image is ISO 8850-1 encoded/encrypted makes no sense.

    What would make more sense is that it is Base64 encoded.

    For example,

    Spotify for Developers: Web API: Upload a Custom Playlist Cover Image.

    Base64 encoded JPEG image data, maximum payload size is 256 KB


    In Go,

    Package base64

    import "encoding/base64"

    Package base64 implements base64 encoding as specified by RFC 4648.


    Another piece of evidence: "HTTPS requests in UTF-8 format"

    Spotify for Developers: Web API

    Requests

    The Spotify Web API is based on REST principles. Data resources are accessed via standard HTTPS requests in UTF-8 format to an API endpoint.


    For example. using your Stack Overflow profile picture:

    package main
    
    import (
        "encoding/base64"
        "fmt"
        "io/ioutil"
        "net/http"
        "os"
    )
    
    func grabImageBytes(imageURL string) ([]byte, error) {
        req, err := http.NewRequest("GET", imageURL, nil)
        if err != nil {
            return nil, err
        }
        res, err := http.DefaultClient.Do(req)
        if err != nil {
            return nil, err
        }
        defer res.Body.Close()
        body, err := ioutil.ReadAll(res.Body)
        if err != nil {
            return nil, err
        }
        enc := base64.StdEncoding
        img := make([]byte, enc.EncodedLen(len(body)))
        enc.Encode(img, body)
        return img, nil
    }
    
    func main() {
        imageURL := `https://lh5.googleusercontent.com/-P8ICR-LXoBs/AAAAAAAAAAI/AAAAAAAAE04/fVAeB6_nMeg/photo.jpg?sz=328`
        img, err := grabImageBytes(imageURL)
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
            return
        }
        fmt.Println(string(img))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题