Skip to content

Instantly share code, notes, and snippets.

@ThomasJClark
Created February 22, 2015 17:11
Show Gist options
  • Save ThomasJClark/2597825d7e36d4ece04d to your computer and use it in GitHub Desktop.
Save ThomasJClark/2597825d7e36d4ece04d to your computer and use it in GitHub Desktop.
Convert data from the FRC-Spy feed to a CSV file
package main
import (
"fmt"
"log"
"os"
)
func main() {
if len(os.Args) != 2 {
log.Fatalf("Usage: %s input.xml", os.Args[0])
}
inputFile, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
matches, err := ReadMatches(inputFile)
if err != nil {
log.Fatal(err)
}
fmt.Println("event,type,red1,red2,red3,redFinal,redFoul,redAuto," +
"redTeleop,blue1,blue2,blue3,blueFinal,blueFoul,blueAuto,blueTeleop")
for _, match := range matches {
fmt.Printf("%s,%s,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
match.Event,
match.Type,
match.Red1,
match.Red2,
match.Red3,
match.RedFinal,
match.RedFoul,
match.RedTeleop,
match.RedAuto,
match.Blue1,
match.Blue2,
match.Blue3,
match.BlueFinal,
match.BlueFoul,
match.BlueTeleop,
match.BlueAuto)
}
}
package main
import (
"encoding/xml"
"io"
"time"
"code.google.com/p/go-charset/charset"
_ "code.google.com/p/go-charset/data" // Character set data
)
type matchType string
const (
Practice matchType = "P"
Qualification matchType = "Q"
Elimination matchType = "E"
)
const (
dateFormat string = "Mon, 2 Jan 2006 15:04:05 -0700"
)
// Match contains information and results from a single match, including time, location, teams, and scores
type Match struct {
XMLName xml.Name `xml:"match"`
PubDateString string `xml:"pubdate"`
PubDate time.Time
Event string `xml:"event"`
Type matchType `xml:"typ"`
Match int `xml:"mch"`
Red1 int `xml:"red1"`
Red2 int `xml:"red2"`
Red3 int `xml:"red3"`
RedFinal int `xml:"rfin"`
RedFoul int `xml:"rfpts"`
RedAuto int `xml:"rhpts"`
RedTeleop int `xml:"rtpts"`
Blue1 int `xml:"blue1"`
Blue2 int `xml:"blue2"`
Blue3 int `xml:"blue3"`
BlueFinal int `xml:"bfin"`
BlueFoul int `xml:"bfpts"`
BlueAuto int `xml:"bhpts"`
BlueTeleop int `xml:"btpts"`
}
// ReadMatches reads XML data in the FRC-Spy format into a new array of Matches
func ReadMatches(r io.Reader) (matches []Match, err error) {
decoder := xml.NewDecoder(r)
decoder.CharsetReader = charset.NewReader
var root struct {
XMLName xml.Name `xml:"matches"`
Matches []Match `xml:"match"`
}
err = decoder.Decode(&root)
if err != nil {
return nil, err
}
matches = root.Matches
/* The encoding/xml package won't parse times, so manually parse the string
version of the match time for each match */
for i := range matches {
matches[i].PubDate, _ = time.Parse(dateFormat, matches[i].PubDateString)
}
return matches, err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment