diff options
author | Joop Kiefte <ikojba@gmail.com> | 2023-08-08 19:03:52 +0100 |
---|---|---|
committer | Joop Kiefte <ikojba@gmail.com> | 2023-08-08 19:03:52 +0100 |
commit | 402ac4ea5895d9cc9e2158a4aed3a2f2314bc324 (patch) | |
tree | 3ed0eea32ecead318368e1ba557ef05a9751ec78 | |
parent | 0a23f0354a86d0f123d3287d4ae6f785f895bffa (diff) |
Correct code
-rw-r--r-- | main.go | 32 |
1 files changed, 30 insertions, 2 deletions
@@ -4,12 +4,14 @@ package main import ( "archive/zip" + "bytes" "fmt" "io" "net/http" "os" "path/filepath" "strings" + "encoding/csv" ) type GTFS map[string][]string @@ -23,8 +25,16 @@ func readFromURL(url string) GTFS { } defer resp.Body.Close() + // Read all into a byte slice, then make a ReaderAt from it + body, err := io.ReadAll(resp.Body) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + reader := bytes.NewReader(body) + // Read zip file - return readFromZip(resp.Body) + return readFromZip(reader) } func readFromFile(filename string) GTFS { @@ -40,7 +50,7 @@ func readFromFile(filename string) GTFS { return readFromZip(file) } -func readFromZip(file io.Reader) GTFS { +func readFromZip(file io.ReaderAt) GTFS { // Open zip file r, err := zip.NewReader(file, 0) if err != nil { @@ -67,6 +77,24 @@ func readFromZip(file io.Reader) GTFS { return gtfs } +func readFromCSV(file io.Reader) []string { + // Read CSV file + r := csv.NewReader(file) + rows, err := r.ReadAll() + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // Convert rows to []string + var result []string + for _, row := range rows { + result = append(result, strings.Join(row, ",")) + } + + return result +} + func writeToDir(gtfs GTFS) { for table, rows := range gtfs { // Open file |