Skip to content

Commit 1d2ce6d

Browse files
committed
ingest sports tournament API
Signed-off-by: Daniel Stamer <[email protected]>
1 parent cc17056 commit 1d2ce6d

File tree

4 files changed

+100
-2
lines changed

4 files changed

+100
-2
lines changed

.vscode/launch.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
78
{
89
"name": "Launch Package",
910
"type": "go",
1011
"request": "launch",
1112
"mode": "auto",
12-
"args": ["trip", "stops"],
13+
"args": ["scores"],
1314
"program": "${workspaceFolder}"
1415
}
1516
]

cmd/scores.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package cmd
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
"os"
10+
11+
"github.com/lensesio/tableprinter"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
const (
16+
scoresEndpoint = "/ticker/live-or-past/wm2022"
17+
)
18+
19+
type Score struct {
20+
Competition string `header:"Competition" json:"competition"`
21+
Venue string `header:"Venue" json:"venue"`
22+
Team1 Team `json:"team1"`
23+
Goals1 int `json:"goals1"`
24+
Team2 Team `json:"team2"`
25+
Goals2 int `json:"goals2"`
26+
Match string `header:"Match"`
27+
Goals string `header:"Goals"`
28+
Events []Event `json:"events"`
29+
MostRecentEventText string `header:"Last Event"`
30+
}
31+
32+
type Team struct {
33+
Name string `header:"Name" json:"name"`
34+
}
35+
36+
type Event struct {
37+
Text string `header:"Event" json:"eventText"`
38+
}
39+
40+
var scoresCmd = &cobra.Command{
41+
Use: "scores",
42+
Short: "Print sports tournament scores info",
43+
Long: `Print sports tournament scores info`,
44+
Run: func(cmd *cobra.Command, args []string) {
45+
s, err := refreshScores()
46+
if err != nil {
47+
fail(err)
48+
}
49+
50+
if Output == "table" {
51+
printer := tableprinter.New(os.Stdout)
52+
items := s
53+
printer.Print(items)
54+
return
55+
}
56+
if Output == "csv" {
57+
for _, score := range s {
58+
fmt.Printf("%s %s %s %s %s\n", score.Competition, score.Venue, score.Match, score.Goals, score.Events[0].Text)
59+
}
60+
return
61+
}
62+
63+
fail(errors.New("unrecognized output format"))
64+
},
65+
}
66+
67+
func refreshScores() ([]*Score, error) {
68+
resp, err := http.Get(fmt.Sprintf("%s%s", baseURL, scoresEndpoint))
69+
if err != nil {
70+
return []*Score{}, nil
71+
}
72+
73+
defer resp.Body.Close()
74+
data, err := io.ReadAll(resp.Body)
75+
if err != nil {
76+
return []*Score{}, err
77+
}
78+
79+
var scores []*Score
80+
err = json.Unmarshal(data, &scores)
81+
if err != nil {
82+
return []*Score{}, err
83+
}
84+
85+
for _, score := range scores {
86+
score.Match = fmt.Sprintf("%s vs. %s", score.Team1.Name, score.Team2.Name)
87+
score.Goals = fmt.Sprintf("%d:%d", score.Goals1, score.Goals2)
88+
score.MostRecentEventText = score.Events[0].Text
89+
}
90+
91+
return scores, nil
92+
}
93+
94+
func init() {
95+
rootCmd.AddCommand(scoresCmd)
96+
}

cmd/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
const (
10-
version = "0.0.2"
10+
version = "0.0.3"
1111
)
1212

1313
var versionCmd = &cobra.Command{

samples/scores.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"competition":"Weltmeisterschaft","venue":"Ar Rayyan","start":"2022-11-25T11:00:00","end":"2022-11-25T13:03:00","team1":{"logo":{"alt":"Flagge Wales","src":"img/grains/Sites/ICE-Portal/Germany/de/Release_2.0/Startseite_3.0/Fussball-Live-Ticker/WM2022/Logos/Gruppe_B/Team_B_WAL_TB.data.svg"},"name":"Wales"},"goals1":0,"team2":{"logo":{"alt":"Flagge Iran","src":"img/grains/Sites/ICE-Portal/Germany/de/Release_2.0/Startseite_3.0/Fussball-Live-Ticker/WM2022/Logos/Gruppe_B/Team_B_IRN_TB.data.svg"},"name":"Iran"},"goals2":2,"status":"FINISHED","events":[{"time":"90. +11","eventType":"GOAL","eventText":"Tor für Iran (R. Rezaeian)","date":"2022-11-25T13:01:00"}],"messages":[],"authors":null,"navigation":{"href":"/liveticker-weltmeisterschaft/2022-11-25_wales_iran","target":null},"machKey":"2022-11-25_wales_iran"}]

0 commit comments

Comments
 (0)