diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..360e2a3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +.idea/ +data/ diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..1d755ab --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,11 @@ +build: + main: ./cmd/go-youtube-accrued-royalties/main.go + goarch: + - amd64 +brew: + github: + owner: daniel-zahariev + name: homebrew-tap + homepage: https://github.com/daniel-zahariev/go-youtube-accrued-royalties + description: Command line tool for extracting records from YouTube accrued publishing royalties. + folder: Formula \ No newline at end of file diff --git a/cmd/go-youtube-accrued-royalties/main.go b/cmd/go-youtube-accrued-royalties/main.go new file mode 100644 index 0000000..dd4de5c --- /dev/null +++ b/cmd/go-youtube-accrued-royalties/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + royalties "github.com/daniel-zahariev/go-youtube-accrued-royalties" + "os" +) + +var ( + version = "master" + date = "1970-01-01" +) + +func showVersion() { + fmt.Printf("go-youtube-accrued-royalties: %v, build %v\n", version, date) +} + +func main() { + if len(os.Args) < 2 { + fmt.Println("Usage:") + fmt.Println("Process file: ./go-youtube-accrued-royalties ") + fmt.Println("Show version: ./go-youtube-accrued-royalties (-v|--version) ") + return + } else if (os.Args[1] == "-v") || (os.Args[1] == "--version") { + showVersion() + return + } + + lines, err := royalties.ReadFile(os.Args[1]) + if err != nil { + fmt.Println(err) + } else { + fmt.Printf("Read the file '%v' - it has %v lines.\n", os.Args[1], lines) + } +} diff --git a/royalties.go b/royalties.go new file mode 100644 index 0000000..a4495d8 --- /dev/null +++ b/royalties.go @@ -0,0 +1,78 @@ +package go_youtube_accrued_royalties + +import ( + "encoding/csv" + "fmt" + "io" + "os" +) + +const AccruedRoyaltiesRecordFieldsCount = 15 + +//type AccruedRoyaltiesRecord struct { +// CustomId string +// AssetId string +// Title string +// Writers string +// Iswc string +// HfaSongCode string +// MatchPolicy string +// PublisherName string +// SyncOwnershipShare string +// SyncOwnershipTerritory string +// SyncOwnershipRestriction string +// RelatedAssetId string +// SrIsrc string +// SrArtist string +// RelatedTitle string +//} + +func ReadFile(filePath string) (int, error) { + linesCount := 0 + + royaltiesFile, err := os.Open(filePath) + if err != nil { + return linesCount, err + } + + csvReader := csv.NewReader(royaltiesFile) + + for { + line, err := csvReader.Read() + if err == io.EOF { + err = nil + break + } else if err != nil { + return linesCount, err + } else if length := len(line); length < AccruedRoyaltiesRecordFieldsCount { + err = fmt.Errorf("line %v is incomplete. It only has %v columns", linesCount, length) + return linesCount, err + } + if linesCount == 0 { + linesCount += 1 + continue + } + + //record := &AccruedRoyaltiesRecord{ + // CustomId: line[0], + // AssetId: line[1], + // Title: line[2], + // Writers: line[3], + // Iswc: line[4], + // HfaSongCode: line[5], + // MatchPolicy: line[6], + // PublisherName: line[7], + // SyncOwnershipShare: line[8], + // SyncOwnershipTerritory: line[9], + // SyncOwnershipRestriction: line[10], + // RelatedAssetId: line[11], + // SrIsrc: line[12], + // SrArtist: line[13], + // RelatedTitle: line[14], + //} + + linesCount += 1 + } + + return linesCount, nil +}