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 (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
@ -34,21 +35,43 @@ func GetDriversFromErgast(year string, client *dgo.Dgraph) {
for _, driver := range drivers {
ctx := context.Background()
mu := &api.Mutation{
CommitNow: true,
}
model := Driver{
GivenName: driver.GivenName,
FamilyName: driver.FamilyName,
Nationality: driver.Nationality,
DateOfBirth: driver.DateOfBirth,
}
GetResultsFromErgast(year, driver.FamilyName, client)
pb, err := json.Marshal(model)
if err != nil {
log.Fatal(err)
}
mu.SetJson = pb
data, err := client.NewTxn().Mutate(ctx, mu)
mu := &api.Mutation{
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 {
log.Fatal(err)
}

View file

@ -16,7 +16,7 @@ import (
// driver.
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/2020/drivers/vettel/results.json")
resp, err := http.Get("http://ergast.com/api/f1/" + year + "/drivers/" + driver + "/results.json")
if err != nil {
log.Error(err)
}
@ -41,9 +41,6 @@ func GetResultsFromErgast(year string, driver string, client *dgo.Dgraph) {
races := jsonData.MRData.RaceTable.Races
for _, race := range races {
ctx := context.Background()
mu := &api.Mutation{
CommitNow: true,
}
var results []Result
@ -95,8 +92,21 @@ func GetResultsFromErgast(year string, driver string, client *dgo.Dgraph) {
if err != nil {
log.Error(err)
}
mu.SetJson = pb
data, err := client.NewTxn().Mutate(ctx, mu)
mu := &api.Mutation{
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 {
log.Error(err)
}

View file

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

View file

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