update download api

This commit is contained in:
Christopher Talib 2019-09-06 13:39:35 +02:00 committed by christalib
parent 1b90da8304
commit 7356234344

View file

@ -5,9 +5,11 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
// Location represents the details of a circuit.
type Location struct {
Country string `json:"country"`
Latitude string `json:"lat"`
@ -15,34 +17,58 @@ type Location struct {
Longitude string `json:"long"`
}
// Circuit represents the details of a circuit.
type Circuit struct {
CircuitID string `json:"circuitID"`
URL string `json:"url"`
Name string `json:"circuitName"`
Location map[string]Location `json:"Location"`
CircuitID string `json:"circuitID"`
URL string `json:"url"`
Name string `json:"circuitName"`
Location Location `json:"Location"`
}
// Race represents the details of a race.
type Race struct {
Season string `json:"season"`
Round string `json:"round"`
RaceName string `json:"raceName"`
Circuit map[string]Circuit `json:"Circuit"`
Date time.Time `json:"date"`
Time time.Time `json:"time"`
ULR string `json:"url"`
Season string `json:"season"`
Round string `json:"round"`
RaceName string `json:"raceName"`
Circuit Circuit `json:"Circuit"`
Date Date `json:"date"`
Time string `json:"time"`
URL string `json:"url"`
}
// Races is the list of races.
type Races struct {
Races map[string]Race `json:"Race"`
Season string `json:"season"`
Races []Race `json:"Races"`
Season string `json:"season"`
}
// RaceTable is the list of races.
type RaceTable struct {
RaceTable map[string]Races `json:"Races"`
RaceTable Races `json:"RaceTable"`
}
// MRData is the general wrapper from Eargast API.
type MRData struct {
MRData map[string]RaceTable `json:"RaceTable"`
MRData RaceTable `json:"MRData"`
}
// Date is the date struct.
type Date struct{ time.Time }
// UnmarshalJSON is something.
func (d *Date) UnmarshalJSON(data []byte) error {
t, err := time.Parse("2006-01-02", strings.Trim(string(data), "\""))
if err != nil {
return err
}
d.Time = t
return nil
}
func (d Date) String() string {
return d.Format(time.RFC3339)
}
// GetDataFromErgast download the data from the API.
@ -58,14 +84,11 @@ func GetDataFromErgast() {
}
defer resp.Body.Close()
var jsonData map[string]MRData
// var jsonData map[string]RaceTable
err = json.Unmarshal([]byte(body), &jsonData)
var jsonData MRData
err = json.Unmarshal(body, &jsonData)
if err != nil {
panic(err.Error())
}
for races := range jsonData {
fmt.Println(races)
}
fmt.Printf("%+s\n", jsonData.MRData.RaceTable.Races[0])
}