Skip to content

Commit 9c478ef

Browse files
committed
upd: historical race data
1 parent a381b25 commit 9c478ef

File tree

6 files changed

+257
-2
lines changed

6 files changed

+257
-2
lines changed

drs

7.09 MB
Binary file not shown.

drs.exe

5.19 MB
Binary file not shown.

internal/helpers/help.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package helpers
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func Help() {
8+
fmt.Printf(" -year <year>: Specify the race year (default is current year).")
9+
fmt.Printf(" -race <race number>: Specify the race data flag.")
10+
fmt.Printf(" --drivers: Display driver's championship standings for the given year.")
11+
fmt.Printf(" --constructors: Display constructor's championship standings for the given year.")
12+
}

internal/helpers/history.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package helpers
2+
3+
import (
4+
"encoding/xml"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"os"
9+
"text/tabwriter"
10+
)
11+
12+
type RaceResult struct {
13+
RaceName string `xml:"RaceTable>Race>RaceName"`
14+
Result []struct {
15+
Driver struct {
16+
GivenName string `xml:"GivenName"`
17+
FamilyName string `xml:"FamilyName"`
18+
} `xml:"Driver"`
19+
Constructor struct {
20+
Name string `xml:"Name"`
21+
} `xml:"Constructor"`
22+
} `xml:"RaceTable>Race>ResultsList>Result"`
23+
}
24+
25+
func getRace(year int, race int) [][]string {
26+
raceURL := fmt.Sprintf("https://ergast.com/api/f1/%d/%d/results", year, race)
27+
28+
resp, err := http.Get(raceURL)
29+
if err != nil {
30+
fmt.Println("error fetching: ", err)
31+
return nil
32+
}
33+
34+
defer resp.Body.Close()
35+
36+
body, err := ioutil.ReadAll(resp.Body)
37+
if err != nil {
38+
fmt.Println("error reading resp: ", err)
39+
}
40+
41+
var data RaceResult
42+
43+
err = xml.Unmarshal(body, &data)
44+
if err != nil {
45+
fmt.Printf("error parsing: ", err)
46+
}
47+
48+
fmt.Printf("%d %s\n", year, data.RaceName)
49+
var standings [][]string
50+
for _, standing := range data.Result {
51+
row := []string{
52+
fmt.Sprintf(standing.Driver.GivenName + " " + standing.Driver.FamilyName),
53+
standing.Constructor.Name,
54+
}
55+
standings = append(standings, row)
56+
}
57+
58+
return standings
59+
}
60+
61+
// Greet prints a greeting message.
62+
func Race(year int, race int) {
63+
data := getRace(year, race)
64+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', tabwriter.TabIndent)
65+
66+
for i, row := range data {
67+
fmt.Fprintln(w, fmt.Sprintf("%d.", i+1)+"\t"+row[0]+"\t"+row[1])
68+
}
69+
w.Flush()
70+
71+
}

internal/helpers/standings.go

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package helpers
2+
3+
import (
4+
"encoding/xml"
5+
"fmt"
6+
"io/ioutil"
7+
"net/http"
8+
"os"
9+
"text/tabwriter"
10+
)
11+
12+
type constructorData struct {
13+
StandingsTable struct {
14+
StandingsList struct {
15+
ConstructorStandings []struct {
16+
Points string `xml:"points,attr"`
17+
Constructor struct {
18+
Name string `xml:"Name"`
19+
} `xml:"Constructor"`
20+
} `xml:"ConstructorStanding"`
21+
} `xml:"StandingsList"`
22+
} `xml:"StandingsTable"`
23+
}
24+
25+
type driverStruct struct {
26+
DriverStandings []struct {
27+
Points string `xml:"points,attr"`
28+
Driver struct {
29+
GivenName string `xml:"GivenName"`
30+
FamilyName string `xml:"FamilyName"`
31+
} `xml:"Driver"`
32+
Team struct {
33+
Name string `xml:"Name"`
34+
} `xml:"Constructor"`
35+
} `xml:"StandingsTable>StandingsList>DriverStanding"`
36+
}
37+
38+
// Greet prints a greeting message.
39+
40+
func getDriverStandings(year int) [][]string {
41+
driverURL := fmt.Sprintf("http://ergast.com/api/f1/%d/driverstandings", year)
42+
43+
resp, err := http.Get(driverURL)
44+
if err != nil {
45+
fmt.Println("error fetching: ", err)
46+
return nil
47+
}
48+
49+
defer resp.Body.Close()
50+
51+
body, err := ioutil.ReadAll(resp.Body)
52+
if err != nil {
53+
fmt.Printf("error reading resp: ", err)
54+
}
55+
56+
var data driverStruct
57+
58+
err = xml.Unmarshal(body, &data)
59+
if err != nil {
60+
fmt.Printf("error parsing resp: ", err)
61+
}
62+
63+
var standings [][]string
64+
65+
for _, standing := range data.DriverStandings {
66+
row := []string{
67+
fmt.Sprintf(standing.Driver.GivenName + " " + standing.Driver.FamilyName),
68+
standing.Team.Name,
69+
standing.Points,
70+
}
71+
standings = append(standings, row)
72+
}
73+
74+
return standings
75+
}
76+
77+
func getConstructorStandings(year int) [][]string {
78+
constructorURL := fmt.Sprintf("http://ergast.com/api/f1/%d/constructorStandings", year)
79+
80+
resp, err := http.Get(constructorURL)
81+
if err != nil {
82+
fmt.Println("error fetching: ", err)
83+
return nil
84+
}
85+
86+
defer resp.Body.Close()
87+
88+
body, err := ioutil.ReadAll(resp.Body)
89+
if err != nil {
90+
fmt.Printf("error reading resp: ", err)
91+
}
92+
93+
var data constructorData
94+
95+
err = xml.Unmarshal(body, &data)
96+
if err != nil {
97+
fmt.Printf("error parsing resp: ", err)
98+
}
99+
100+
var standings [][]string
101+
102+
for _, standing := range data.StandingsTable.StandingsList.ConstructorStandings {
103+
row := []string{
104+
fmt.Sprintf(standing.Constructor.Name),
105+
standing.Points,
106+
}
107+
standings = append(standings, row)
108+
}
109+
110+
return standings
111+
}
112+
113+
// TODO: format returned into table: input:year
114+
func DriverStandings(year int) {
115+
data := getDriverStandings(year)
116+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', tabwriter.TabIndent)
117+
118+
for i, row := range data {
119+
fmt.Fprintln(w, fmt.Sprintf("%d.", i+1)+"\t"+row[0]+"\t"+row[1]+"\t"+row[2])
120+
}
121+
w.Flush()
122+
}
123+
124+
// TODO: constructor standings: input:year
125+
func ConstructorStandings(year int) {
126+
// return fmt.Sprintf("constructr standings")
127+
data := getConstructorStandings(year)
128+
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', tabwriter.TabIndent)
129+
130+
for i, row := range data {
131+
fmt.Fprintln(w, fmt.Sprintf("%d.", i+1)+"\t"+row[0]+"\t"+row[1])
132+
}
133+
fmt.Println("")
134+
135+
w.Flush()
136+
}

main.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"flag"
5+
"fmt"
6+
"time"
7+
8+
"example.com/drs/internal/helpers"
9+
)
410

511
func main() {
6-
fmt.Println("Hello, Go!")
12+
yearFlag := flag.Int("year", time.Now().Year(), "race year")
13+
raceFlag := flag.Int("race", 0, "race data flag")
14+
driversChampionshipFlag := flag.Bool("drivers", false, "drvier's championship for given year")
15+
constructorsChampionshipFlag := flag.Bool("constructors", false, "constructor's championship for given year")
16+
helpFlag := flag.Bool("help", false, "show help")
17+
flag.Parse()
18+
19+
if flag.NFlag() == 0 {
20+
fmt.Printf("Welcome to DRS!\nUsage:")
21+
helpers.Help() // Call Help function to show the usage
22+
return
23+
}
24+
25+
if *helpFlag {
26+
helpers.Help()
27+
return
28+
}
29+
30+
if *raceFlag != 0 {
31+
helpers.Race(*yearFlag, *raceFlag)
32+
}
33+
if *driversChampionshipFlag {
34+
helpers.DriverStandings(*yearFlag)
35+
fmt.Printf("\n")
36+
}
37+
38+
fmt.Printf("\n")
39+
if *constructorsChampionshipFlag {
40+
helpers.ConstructorStandings(*yearFlag)
41+
fmt.Printf("\n")
42+
}
743
}

0 commit comments

Comments
 (0)