Skip to content

Commit

Permalink
Merge pull request #1 from Iune/excel-parsing
Browse files Browse the repository at this point in the history
Update input file formats
  • Loading branch information
Iune authored Dec 29, 2019
2 parents 830a740 + fe3fb15 commit 3bac85d
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 112 deletions.
54 changes: 52 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,63 @@

# Created by https://www.gitignore.io/api/go,macos,visualstudiocode
# Edit at https://www.gitignore.io/?templates=go,macos,visualstudiocode

### Go ###
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
dist/

# Test binary, build with `go test -c`
# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

### Go Patch ###
/vendor/
/Godeps/

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### VisualStudioCode ###
.vscode/*

### VisualStudioCode Patch ###
# Ignore all local history of files
.history

# End of https://www.gitignore.io/api/go,macos,visualstudiocode

resources/*.xlsx
46 changes: 22 additions & 24 deletions contest/contest.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package contest

import (
"bufio"
"encoding/csv"
"os"
"strings"
"github.com/tealeg/xlsx"

log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -34,7 +32,7 @@ func (c Contest) FindSong(song string) (entry Entry, found bool) {

func (c Contest) FindCountryByName(name string) (entry Entry, found bool) {
for _, e := range c.Entries {
if e.Country.Find(name) {
if e.Country.HasName(name) {
return e, true
}
}
Expand All @@ -60,44 +58,44 @@ func (c Contest) GetEntryIndex(entry Entry) (index int, found bool) {
}

func LoadContest(contestFilePath string, countries []Country) Contest {
csvFile, err := os.Open(contestFilePath)
if err != nil {
log.Fatal(err)
}
reader := csv.NewReader(bufio.NewReader(csvFile))
reader.FieldsPerRecord = -1
reader.Comma = '\t'
csvFileContents, err := reader.ReadAll()
excelFile, err := xlsx.OpenFile(contestFilePath)
if err != nil {
log.Fatal(err)
}

voters := getVoters(csvFileContents)
entries := getEntries(csvFileContents, countries)
sheet := excelFile.Sheets[0]
voters := getVoters(sheet.Rows[0])
entries := getEntries(sheet, countries)

log.WithFields(log.Fields{
"file": contestFilePath,
}).Info("Loaded contest details from file")
return Contest{Entries: entries, Voters: voters}
}

func getVoters(csv [][]string) []string {
func getVoters(row *xlsx.Row) []string {
var voters []string
if len(csv[0]) >= 6 {
voters = csv[0][6:len(csv[0])]
cells := row.Cells
if len(cells) >= 6 {
for _, cell := range row.Cells[6:len(cells)] {
voters = append(voters, cell.String())
}
} else {
log.Fatal("No voters were defined in contest file")
}
return voters
}

func getEntries(csv [][]string, countries []Country) []Entry {
func getEntries(sheet *xlsx.Sheet, countries []Country) []Entry {
var entries []Entry
for _, line := range csv[1:] {
country, found := GetCountry(countries, line[1])
for _, row := range sheet.Rows[1:] {
country, found := GetCountry(countries, row.Cells[1].String())
if !found {
log.Fatalf("Could not find country %s in countries file", line[1])
log.Fatalf("Could not find country %s in countries file", row.Cells[1])
}
artist := line[3]
song := line[4]


artist := row.Cells[3].String()
song := row.Cells[4].String()
entries = append(entries, Entry{
Country: country,
Artist: artist,
Expand Down
51 changes: 23 additions & 28 deletions contest/country.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package contest

import (
"bufio"
"encoding/csv"
"io"
"encoding/json"
"io/ioutil"
"os"
"strings"

log "github.com/sirupsen/logrus"
)

type countries struct {
Countries []Country `json:"countries"`
}

type Country struct {
Iso string
Forum string
Names []string
Forum string `json:"forum"`
Names []string `json:"names"`
}

func (c Country) Find(name string) bool {
func (c Country) HasName(name string) bool {
for _, n := range c.Names {
if strings.Contains(strings.ToLower(name), strings.ToLower(n)) {
return true
Expand All @@ -29,39 +31,32 @@ func (c Country) GetPrimaryName() string {
if len(c.Names) > 0 {
return c.Names[0]
}
log.Fatal("A country must have at least one name")
return ""
}

func GetCountry(countries []Country, name string) (country Country, found bool) {
for _, c := range countries {
if c.Find(name) {
if c.HasName(name) {
return c, true
}
}
return Country{}, false
}

func LoadCountries(countryFilePath string) []Country {
csvFile, err := os.Open(countryFilePath)
func LoadCountries(countriesFilePath string) []Country {
jsonFile, err := os.Open(countriesFilePath)
if err != nil {
log.Fatal(err)
}
reader := csv.NewReader(bufio.NewReader(csvFile))
reader.Comma = '\t'
var countries []Country
for {
line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()

countries = append(countries, Country{
Iso: line[0],
Forum: line[1],
Names: strings.Split(line[2], ";"),
})
}
return countries
}
byteValue, _ := ioutil.ReadAll(jsonFile)
var countries countries
json.Unmarshal(byteValue, &countries)

log.WithFields(log.Fields{
"file": countriesFilePath,
}).Info("Loaded country details from file")
return countries.Countries
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/Iune/ida

go 1.13

require (
github.com/alexflint/go-arg v1.2.0
github.com/atotto/clipboard v0.1.2
github.com/sirupsen/logrus v1.4.2
github.com/tealeg/xlsx v1.0.5
)
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/alexflint/go-arg v1.2.0 h1:TOFkN8/Cn1VvbHhsCuYq+P6ol3P5FAmjKIqsk7D2U/Q=
github.com/alexflint/go-arg v1.2.0/go.mod h1:3Rj4baqzWaGGmZA2+bVTV8zQOZEjBQAPBnL5xLT+ftY=
github.com/alexflint/go-scalar v1.0.0 h1:NGupf1XV/Xb04wXskDFzS0KWOLH632W/EO4fAFi+A70=
github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw=
github.com/atotto/clipboard v0.1.2 h1:YZCtFu5Ie8qX2VmVTBnrqLSiU9XOWwqNRmdT3gIQzbY=
github.com/atotto/clipboard v0.1.2/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Binary file removed ida
Binary file not shown.
15 changes: 8 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import (
"github.com/alexflint/go-arg"
"github.com/atotto/clipboard"

"github.com/iune/ida/contest"
"github.com/iune/ida/results"
"github.com/iune/ida/voting"
"github.com/Iune/ida/contest"
"github.com/Iune/ida/results"
"github.com/Iune/ida/voting"

log "github.com/sirupsen/logrus"
)

type args struct {
Countries string `arg:"positional" help:"Path to tab-separated file with country information"`
Spreadsheet string `arg:"positional" help:"Path to tab-separated contest file"`
Countries string `arg:"positional" help:"Path to JSON file with country information"`
Spreadsheet string `arg:"positional" help:"Path to contest Excel spreadsheet"`
}

func (args) Version() string {
return "ida 0.1.1"
return "ida 0.2.0"
}

func main() {
Expand All @@ -37,7 +38,7 @@ func main() {

// Set up logging
log.SetOutput(os.Stdin)
log.SetLevel(log.WarnLevel)
log.SetLevel(log.InfoLevel)

countries := contest.LoadCountries(args.Countries)
contest := contest.LoadContest(args.Spreadsheet, countries)
Expand Down
23 changes: 0 additions & 23 deletions resources/1991.csv

This file was deleted.

Loading

0 comments on commit 3bac85d

Please sign in to comment.