main.go (2527B)
1 package main 2 3 import ( 4 "os" 5 "path/filepath" 6 "sort" 7 "strings" 8 "time" 9 10 "log" 11 12 "github.com/MTRNord/edsm_uploader/edsm" 13 "github.com/MTRNord/edsm_uploader/journal" 14 "github.com/pkg/errors" 15 ) 16 17 type JournalFile struct { 18 path string 19 date time.Time 20 } 21 22 func main() { 23 // Take as a first positional argument the path to the folder where the journal files are located. 24 // Take as a second positional argument the commander name. 25 // Take as a third positional argument the EDSM API key. 26 27 logger := log.New(os.Stderr, "edsm_uploader: ", log.LstdFlags) 28 29 jounnalPath := os.Args[1] 30 commanderName := os.Args[2] 31 apiKey := os.Args[3] 32 33 // Create a new EDSM object. 34 edsm := edsm.NewEDSM(commanderName, apiKey, logger) 35 36 // Find all the journal files in the folder and parse them after sorting them by date. 37 files := make(map[string]JournalFile) 38 err := filepath.WalkDir(jounnalPath, func(path string, d os.DirEntry, err error) error { 39 if err != nil { 40 return errors.WithStack(err) 41 } 42 if !d.IsDir() { 43 // If the file starts with "Journal." then we add it to the files map. 44 // The date is after "Journal." and before the first ".". 45 // Example: "Journal.2024-03-09T104947.01.log" 46 // The date is "2024-03-09T104947.01" 47 if d.Name()[:8] == "Journal." { 48 name := d.Name() 49 parts := strings.Split(name, ".") 50 date := parts[1] + "." + parts[2] 51 parsedDate, err := time.Parse("2006-01-02T150405.00", date) 52 if err != nil { 53 return errors.WithStack(err) 54 } 55 files[date] = JournalFile{ 56 path: path, 57 date: parsedDate, 58 } 59 } 60 61 } 62 return nil 63 }) 64 if err != nil { 65 logger.Fatalf("Error walking the path: %+v", err) 66 } 67 68 // Sort the files by date. 69 keys := make([]string, 0, len(files)) 70 for k := range files { 71 keys = append(keys, k) 72 } 73 sort.Strings(keys) 74 75 journal_obj := journal.NewJournal(edsm, logger) 76 for _, k := range keys { 77 // Parse the journal file. 78 // Parse date of the journal file and ignore if older than journal_obj.lastDate. 79 80 currentDate := *journal_obj.LastDate 81 startOfDay := Bod(currentDate) 82 if journal_obj.LastDate != nil && files[k].date.Before(startOfDay) { 83 continue 84 } 85 86 err := journal_obj.ParseJournal(files[k].path) 87 if err != nil { 88 logger.Fatalf("Error parsing journal file: %+v", err) 89 } 90 // sleep for 1 second to not overload the EDSM API. 91 time.Sleep(1 * time.Second) 92 } 93 94 logger.Println("Done parsing journal files.") 95 } 96 97 func Bod(t time.Time) time.Time { 98 return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) 99 }