From 05b5da5169137184ae075f3973088071f5b38ec3 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 28 Aug 2021 21:05:38 +0200 Subject: [PATCH] update(spotify): use struct for response (json) outout je te suce --- server.go | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++- struct.go | 105 +++++++++++++++++++++++++++++++++++++ 2 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 struct.go diff --git a/server.go b/server.go index 4ed192e..8a26cba 100644 --- a/server.go +++ b/server.go @@ -1,14 +1,133 @@ package main import ( +// "bytes" "fmt" "log" + "time" "net/http" "encoding/json" ) var MyClient = &http.Client{} + +type YoutubeAPI struct { + Href string `json:"href"` + Items []struct { + AddedAt time.Time `json:"added_at"` + AddedBy struct { + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"added_by"` + IsLocal bool `json:"is_local"` + PrimaryColor interface{} `json:"primary_color"` + Track struct { + Album struct { + AlbumType string `json:"album_type"` + Artists []struct { + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"artists"` + AvailableMarkets []string `json:"available_markets"` + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + Images []struct { + Height int `json:"height"` + URL string `json:"url"` + Width int `json:"width"` + + } `json:"images"` + Name string `json:"name"` + ReleaseDate string `json:"release_date"` + ReleaseDatePrecision string `json:"release_date_precision"` + TotalTracks int `json:"total_tracks"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"album"` + Artists []struct { + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"artists"` + AvailableMarkets []string `json:"available_markets"` + DiscNumber int `json:"disc_number"` + DurationMs int `json:"duration_ms"` + Episode bool `json:"episode"` + Explicit bool `json:"explicit"` + ExternalIds struct { + Isrc string `json:"isrc"` + + } `json:"external_ids"` + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + IsLocal bool `json:"is_local"` + Name string `json:"name"` + Popularity int `json:"popularity"` + PreviewURL string `json:"preview_url"` + Track bool `json:"track"` + TrackNumber int `json:"track_number"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"track"` + VideoThumbnail struct { + URL interface{} `json:"url"` + + } `json:"video_thumbnail"` + + } `json:"items"` + Limit int `json:"limit"` + Next string `json:"next"` + Offset int `json:"offset"` + Previous interface{} `json:"previous"` + Total int `json:"total"` + +} + +type Test struct { + Tee string +} + +type ErrorSpotify struct { + href string +} + +type Error struct { + status, message string +} + /* id de la playlist */ @@ -23,13 +142,43 @@ func getListPlaylist(id string) { fmt.Printf("error:", err) return } + fmt.Printf("hm", res.Status) + /* + buf := new(bytes.Buffer) + buf.ReadFrom(res.Body) - defer res.Body.Close() - decoder := json.NewDecoder(res.Body) + fmt.Printf("coucou %+v", buf.String()) + */ + //ree := &ErrorSpotify{} + var ree YoutubeAPI + //defer res.Body.Close() + //json.Unmarshal(res.Body, ree) + err = json.NewDecoder(res.Body).Decode(&ree) + if err != nil { + fmt.Printf("error:", err) + return + } + //json.NewDecoder(res.Body).Decode(interface{}(ree)) + //decoder := json.NewDecoder(res.Body).Decode(interface{}(ree)) + + /* for token, _ := decoder.Token(); token != nil; token, _ = decoder.Token() { fmt.Printf("value:", token) } + */ + //decoder.Decode(interface{}(ree)) + fmt.Printf(ree.Href) + fmt.Printf("coucou\n") + return + + token := map[string]string{} + fmt.Printf("coucou\n") + + for _, v := range token { + fmt.Printf("coucou") + fmt.Printf("\n", v) + } } func formHandler(w http.ResponseWriter, r *http.Request) { diff --git a/struct.go b/struct.go new file mode 100644 index 0000000..c422fb9 --- /dev/null +++ b/struct.go @@ -0,0 +1,105 @@ +package main + +type SpotifyPlaylist struct { + Href string `json:"href"` + Items []struct { + AddedAt time.Time `json:"added_at"` + AddedBy struct { + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"added_by"` + IsLocal bool `json:"is_local"` + PrimaryColor interface{} `json:"primary_color"` + Track struct { + Album struct { + AlbumType string `json:"album_type"` + Artists []struct { + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"artists"` + AvailableMarkets []string `json:"available_markets"` + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + Images []struct { + Height int `json:"height"` + URL string `json:"url"` + Width int `json:"width"` + + } `json:"images"` + Name string `json:"name"` + ReleaseDate string `json:"release_date"` + ReleaseDatePrecision string `json:"release_date_precision"` + TotalTracks int `json:"total_tracks"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"album"` + Artists []struct { + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"artists"` + AvailableMarkets []string `json:"available_markets"` + DiscNumber int `json:"disc_number"` + DurationMs int `json:"duration_ms"` + Episode bool `json:"episode"` + Explicit bool `json:"explicit"` + ExternalIds struct { + Isrc string `json:"isrc"` + + } `json:"external_ids"` + ExternalUrls struct { + Spotify string `json:"spotify"` + + } `json:"external_urls"` + Href string `json:"href"` + ID string `json:"id"` + IsLocal bool `json:"is_local"` + Name string `json:"name"` + Popularity int `json:"popularity"` + PreviewURL string `json:"preview_url"` + Track bool `json:"track"` + TrackNumber int `json:"track_number"` + Type string `json:"type"` + URI string `json:"uri"` + + } `json:"track"` + VideoThumbnail struct { + URL interface{} `json:"url"` + + } `json:"video_thumbnail"` + + } `json:"items"` + Limit int `json:"limit"` + Next string `json:"next"` + Offset int `json:"offset"` + Previous interface{} `json:"previous"` + Total int `json:"total"` + +}