From e3ee93d8b7175fd03bd543bb6a7ff2d24bb47e2d Mon Sep 17 00:00:00 2001 From: christalib Date: Sat, 25 Jul 2020 00:52:24 +0200 Subject: [PATCH] adding drivers to the upload --- crawlers/driver.go | 30 ++++++++++++++++++++---------- crawlers/race.go | 7 +++---- db/types.go | 3 +++ graph/main.go | 4 ++++ main.go | 2 +- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/crawlers/driver.go b/crawlers/driver.go index 7ce83f1..6ad9565 100644 --- a/crawlers/driver.go +++ b/crawlers/driver.go @@ -1,14 +1,16 @@ package crawlers import ( + "context" "encoding/json" - "fmt" "io/ioutil" + "log" "net/http" "time" "git.postblue.info/chris/paddockpass/db" - "github.com/jinzhu/gorm" + "github.com/dgraph-io/dgo/v2" + "github.com/dgraph-io/dgo/v2/protos/api" ) // Driver represents a structure of driver entry in the Eargast API. @@ -35,7 +37,7 @@ type MRDriverData struct { } // GetDriversFromErgast gets the driver data from the API. -func GetDriversFromErgast(year string) { +func GetDriversFromErgast(year string, client *dgo.Dgraph) { resp, err := http.Get("http://ergast.com/api/f1/" + year + "/drivers.json") if err != nil { panic(err.Error()) @@ -55,22 +57,30 @@ func GetDriversFromErgast(year string) { drivers := jsonData.MRData.DriverTable.Drivers - tx, err := gorm.Open("sqlite3", "test.db") - if err != nil { - fmt.Println(err) - } for _, driver := range drivers { - fmt.Println(driver) + ctx := context.Background() + mu := &api.Mutation{ + CommitNow: true, + } model := db.Driver{ FirstName: driver.FirstName, LastName: driver.LastName, Nationality: driver.Nationality, DoB: (driver.DateOrBirth).Format(time.RFC3339), + Type: "driver", } - tx.Where(Driver{FirstName: driver.FirstName, LastName: driver.LastName}).Assign(model).FirstOrCreate(&model) + pb, err := json.Marshal(model) + if err != nil { + log.Fatal(err) + } + mu.SetJson = pb + data, err := client.NewTxn().Mutate(ctx, mu) + if err != nil { + log.Fatal(err) + } + log.Print(data) } - defer tx.Close() } // { diff --git a/crawlers/race.go b/crawlers/race.go index b5ee914..ab04fa5 100644 --- a/crawlers/race.go +++ b/crawlers/race.go @@ -106,6 +106,7 @@ func GetSeasonFromErgast(year string, client *dgo.Dgraph) { CircuitID: race.Circuit.CircuitID, Name: race.Circuit.Name, URL: race.Circuit.URL, + Type: "circuit", } race := db.Race{ @@ -114,19 +115,17 @@ func GetSeasonFromErgast(year string, client *dgo.Dgraph) { Round: race.Round, Season: race.Season, URL: race.URL, + Type: "race", } pb, err := json.Marshal(race) if err != nil { log.Fatal(err) } mu.SetJson = pb - data, err := client.NewTxn().Mutate(ctx, mu) + _, err = client.NewTxn().Mutate(ctx, mu) if err != nil { log.Fatal(err) } - log.Print(race.RaceName, race.Circuit.Name) - log.Print(data) - } } diff --git a/db/types.go b/db/types.go index 72527e6..bae98e0 100644 --- a/db/types.go +++ b/db/types.go @@ -13,6 +13,7 @@ type Race struct { Circuit Circuit `json:"circuit,omitempty"` Date time.Time `json:"date,omitempty"` URL string `json:"url,omitempty"` + Type string `json:"type,omitempty"` } // Circuit model describe a circuit. @@ -22,6 +23,7 @@ type Circuit struct { Location string `json:"location,omitempty"` CircuitID string `json:"circuitID,omitempty"` URL string `json:"url,omitempty"` + Type string `json:"type,omitempty"` } // Driver describes a driver. @@ -31,4 +33,5 @@ type Driver struct { LastName string `json:"lastName,omitempty"` Nationality string `json:"nationality,omitempty"` DoB string `json:"doB,omitempty"` + Type string `json:"type,omitempty"` } diff --git a/graph/main.go b/graph/main.go index 0603dab..606717f 100644 --- a/graph/main.go +++ b/graph/main.go @@ -27,12 +27,14 @@ firstName: string . lastName: string @index(term) . nationality: string @index(term) . dob: string . +type: string @index(term) . type Driver { firstName: string lastName: string nationality: string dob: string +type: string } season: string @index(term) . @@ -46,6 +48,7 @@ type Circuit { name: string location: string url: string +type: string } type Race { @@ -55,6 +58,7 @@ raceName: string circuit: Circuit date: string url: string +type: string } name: string @index(term) . diff --git a/main.go b/main.go index c97980e..4e8e31c 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,6 @@ func main() { fmt.Println("Hello there! It is", year) crawlers.GetSeasonFromErgast(year, client) - // crawlers.GetDriversFromErgast(year) + crawlers.GetDriversFromErgast(year, client) // crawlers.GetResultsFromErgast(year) }