loading drivers

This commit is contained in:
christalib 2020-07-28 00:01:06 +02:00
parent 0ff9de602e
commit 61096ba613
4 changed files with 46 additions and 13 deletions

View file

@ -3,6 +3,7 @@ package crawlers
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -34,21 +35,43 @@ func GetDriversFromErgast(year string, client *dgo.Dgraph) {
for _, driver := range drivers { for _, driver := range drivers {
ctx := context.Background() ctx := context.Background()
mu := &api.Mutation{
CommitNow: true,
}
model := Driver{ model := Driver{
GivenName: driver.GivenName, GivenName: driver.GivenName,
FamilyName: driver.FamilyName, FamilyName: driver.FamilyName,
Nationality: driver.Nationality, Nationality: driver.Nationality,
DateOfBirth: driver.DateOfBirth, DateOfBirth: driver.DateOfBirth,
} }
GetResultsFromErgast(year, driver.FamilyName, client)
pb, err := json.Marshal(model) pb, err := json.Marshal(model)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
mu.SetJson = pb mu := &api.Mutation{
data, err := client.NewTxn().Mutate(ctx, mu) SetJson: pb,
Cond: `@if(eq(len(driver), 0))`,
}
if driver.FamilyName == "Pérez" {
driver.FamilyName = "Perez"
}
if driver.FamilyName == "Räikkönen" {
driver.FamilyName = "Raikkonen"
}
query := fmt.Sprintf(`query {
driver as var(func: eq(familyName, %s))
}`, driver.FamilyName)
req := &api.Request{
Query: query,
Mutations: []*api.Mutation{mu},
CommitNow: true,
}
data, err := client.NewTxn().Do(ctx, req)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View file

@ -16,7 +16,7 @@ import (
// driver. // driver.
func GetResultsFromErgast(year string, driver string, client *dgo.Dgraph) { func GetResultsFromErgast(year string, driver string, client *dgo.Dgraph) {
// resp, err := http.Get("http://ergast.com/api/f1/" + year + "/" + driver + ".json") // resp, err := http.Get("http://ergast.com/api/f1/" + year + "/" + driver + ".json")
resp, err := http.Get("http://ergast.com/api/f1/2020/drivers/vettel/results.json") resp, err := http.Get("http://ergast.com/api/f1/" + year + "/drivers/" + driver + "/results.json")
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }
@ -41,9 +41,6 @@ func GetResultsFromErgast(year string, driver string, client *dgo.Dgraph) {
races := jsonData.MRData.RaceTable.Races races := jsonData.MRData.RaceTable.Races
for _, race := range races { for _, race := range races {
ctx := context.Background() ctx := context.Background()
mu := &api.Mutation{
CommitNow: true,
}
var results []Result var results []Result
@ -95,8 +92,21 @@ func GetResultsFromErgast(year string, driver string, client *dgo.Dgraph) {
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }
mu.SetJson = pb mu := &api.Mutation{
data, err := client.NewTxn().Mutate(ctx, mu) SetJson: pb,
Cond: fmt.Sprintf(`@if(eq(round, %s)`, model.Round),
}
query := fmt.Sprintf(`query {
race as var(func: eq(round, %s))
}`, model.Round)
req := &api.Request{
Query: query,
Mutations: []*api.Mutation{mu},
CommitNow: true,
}
data, err := client.NewTxn().Do(ctx, req)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }

View file

@ -29,7 +29,7 @@ circuit: uid .
constructor: uid . constructor: uid .
constructorId: string . constructorId: string .
date: string . date: string .
dateOfBirth: string . dateOfBirth: string @index(term) .
driver: uid . driver: uid .
fastestLap: uid . fastestLap: uid .
givenName: string . givenName: string .

View file

@ -30,7 +30,7 @@ func main() {
fmt.Println("Hello there! It is", year) fmt.Println("Hello there! It is", year)
crawlers.GetDriversFromErgast(year, client) crawlers.GetDriversFromErgast(year, client)
crawlers.GetResultsFromErgast(year, "alonso", client)
// crawlers.GetSeasonFromErgast(year, client) // crawlers.GetSeasonFromErgast(year, client)
// crawlers.GetResultsFromErgast(year) // crawlers.GetResultsFromErgast(year)
} }