paddockpass/crawlers/race.go
2020-07-26 22:59:23 +02:00

102 lines
2 KiB
Go

package crawlers
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"github.com/sirupsen/logrus"
)
// 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, client *dgo.Dgraph) {
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 {
logrus.Error(err)
}
races := jsonData.MRData.RaceTable.Races
for _, race := range races {
ctx := context.Background()
mu := &api.Mutation{
CommitNow: true,
}
circuit := Circuit{
CircuitID: race.Circuit.CircuitID,
Name: race.Circuit.Name,
URL: race.Circuit.URL,
}
race := Race{
Circuit: circuit,
RaceName: race.RaceName,
Round: race.Round,
Season: race.Season,
URL: race.URL,
}
pb, err := json.Marshal(race)
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
_, err = client.NewTxn().Mutate(ctx, mu)
if err != nil {
log.Fatal(err)
}
}
}
// // 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")
// }
// }
// }