update(spotify): use struct for response (json)

outout je te suce
This commit is contained in:
rick 2021-08-28 21:05:38 +02:00
parent de43e8981b
commit 05b5da5169
Signed by: Rick
GPG key ID: 2B593F087240EE99
2 changed files with 256 additions and 2 deletions

153
server.go
View file

@ -1,14 +1,133 @@
package main package main
import ( import (
// "bytes"
"fmt" "fmt"
"log" "log"
"time"
"net/http" "net/http"
"encoding/json" "encoding/json"
) )
var MyClient = &http.Client{} 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 id de la playlist
*/ */
@ -23,13 +142,43 @@ func getListPlaylist(id string) {
fmt.Printf("error:", err) fmt.Printf("error:", err)
return return
} }
fmt.Printf("hm", res.Status)
/*
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
defer res.Body.Close() fmt.Printf("coucou %+v", buf.String())
decoder := json.NewDecoder(res.Body) */
//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() { for token, _ := decoder.Token(); token != nil; token, _ = decoder.Token() {
fmt.Printf("value:", 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) { func formHandler(w http.ResponseWriter, r *http.Request) {

105
struct.go Normal file
View file

@ -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"`
}