-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
84 lines (69 loc) · 1.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/olekukonko/tablewriter"
)
type Team struct {
Position string
Name string
Points string
Games string
Wins string
Draws string
Looses string
GoalsFor string
GoalsAgainst string
GoalDifference string
Percentage string
}
func main() {
league := "a"
if len(os.Args) > 1 && os.Args[1] == "b" {
league = "b"
}
doc, err := goquery.NewDocument("http://globoesporte.globo.com/futebol/brasileirao-serie-" + league + "/")
if err != nil {
log.Fatal(err)
os.Exit(1)
}
var teams [20]Team
doc.Find(".tabela-times strong.tabela-times-time-nome").Each(func(i int, s *goquery.Selection) {
teams[i] = Team{Position: strconv.Itoa(i + 1), Name: s.Text()}
})
teamIndex := 0
fieldIndex := 0
doc.Find(".tabela-pontos tbody td:not(.tabela-pontos-ultimos-jogos)").Each(func(i int, s *goquery.Selection) {
if i > 0 && i%9 == 0 {
fieldIndex = 0
teamIndex = teamIndex + 1
}
// could have used directly a map but I was interested in learn how reflect works in golang
reflect.ValueOf(&teams[teamIndex]).Elem().Field(fieldIndex + 2).SetString(s.Text())
fieldIndex = fieldIndex + 1
})
table := tablewriter.NewWriter(os.Stdout)
fmt.Println("\u2605 BRASILERÃO SÉRIE " + strings.ToUpper(league) + " \u26BD")
table.SetHeader([]string{"Pos", "Time", "P", "J", "V", "E", "D", "GP", "GC", "SG", "%"})
for _, team := range teams {
table.Append([]string{
team.Position,
team.Name,
team.Points,
team.Games,
team.Wins,
team.Draws,
team.Looses,
team.GoalsFor,
team.GoalsAgainst,
team.GoalDifference,
team.Percentage,
})
}
table.Render()
}