141 lines
3 KiB
Go
141 lines
3 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"framagit.org/pksl/paddockpass/db"
|
|
"github.com/jinzhu/gorm"
|
|
)
|
|
|
|
// Location represents the details of a circuit.
|
|
type Location struct {
|
|
Country string `json:"country"`
|
|
Latitude string `json:"lat"`
|
|
Locatlity string `json:"locality"`
|
|
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 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 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 []Race `json:"Races"`
|
|
Season string `json:"season"`
|
|
}
|
|
|
|
// RaceTable is the list of races.
|
|
type RaceTable struct {
|
|
RaceTable Races `json:"RaceTable"`
|
|
}
|
|
|
|
// MRData is the general wrapper from Eargast API.
|
|
type MRData struct {
|
|
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)
|
|
}
|
|
|
|
// GetSeasonFromErgast download the data from the API.
|
|
func GetSeasonFromErgast(year string) {
|
|
resp, err := http.Get("http://ergast.com/api/f1/" + year + ".json")
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var jsonData MRData
|
|
err = json.Unmarshal(body, &jsonData)
|
|
if err != nil {
|
|
panic(err.Error())
|
|
}
|
|
|
|
races := jsonData.MRData.RaceTable.Races
|
|
|
|
tx, err := gorm.Open("sqlite3", "test.db")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
|
|
for _, race := range races {
|
|
circuit := db.Circuit{
|
|
CircuitID: race.Circuit.CircuitID,
|
|
Name: race.Circuit.Name,
|
|
URL: race.Circuit.URL,
|
|
}
|
|
|
|
race := db.Race{
|
|
Circuit: circuit,
|
|
RaceName: race.RaceName,
|
|
Round: race.Round,
|
|
Season: race.Season,
|
|
URL: race.URL,
|
|
}
|
|
|
|
tx.Assign(circuit).FirstOrCreate(&circuit)
|
|
tx.Assign(race).FirstOrCreate(&race)
|
|
}
|
|
defer tx.Close()
|
|
}
|
|
|
|
// // CheckIfEntryExists returns a boolean if the entry is in the DB.
|
|
// func CheckIfEntryExists(model interface{}) (bool, error) {
|
|
// db, err := gorm.Open("sqlite3", "test.db")
|
|
// if err != nil {
|
|
// panic(err.Error())
|
|
// }
|
|
// defer db.Close()
|
|
|
|
// var user User
|
|
// if err := db.Where("name = ?", "xxxx").First(&user).Error; err != nil {
|
|
// // error handling...
|
|
// if gorm.IsRecordNotFoundError(err) {
|
|
// db.Create(&newUser) // newUser not user
|
|
// } else {
|
|
// db.Model(&user).Where("id = ?", 3333).Update("name", "nick")
|
|
// }
|
|
// }
|
|
// }
|