commit dabb312dd60ce5ef3d5ae9864481d80012a1292d
parent af7086bbbfdf552743580369403b2258f3a6e9f0
Author: MTRNord <mtrnord1@gmail.com>
Date: Sat, 9 Mar 2024 16:51:43 +0100
Dont parse old files more often than needed
Diffstat:
2 files changed, 34 insertions(+), 10 deletions(-)
diff --git a/journal/journal.go b/journal/journal.go
@@ -23,7 +23,7 @@ type FileHeader struct {
type Journal struct {
edsm *edsm.EDSM
- lastDate *time.Time
+ LastDate *time.Time
fileHeader *datatypes.FileHeader
logger *log.Logger
}
@@ -56,7 +56,7 @@ func NewJournal(edsm *edsm.EDSM, logger *log.Logger) *Journal {
// TODO: Parse the last date from the sqlite database.
// If there is no last date, then we need to start from the beginning and define it as nil.
edsm: edsm,
- lastDate: lastDate,
+ LastDate: lastDate,
fileHeader: nil,
logger: logger,
}
@@ -101,8 +101,8 @@ func (j *Journal) ParseJournal(journalPath string) error {
}
func (j *Journal) storeLastDate(timestamp time.Time) {
- if j.lastDate == nil || j.lastDate.Before(timestamp) {
- j.lastDate = ×tamp
+ if j.LastDate == nil || j.LastDate.Before(timestamp) {
+ j.LastDate = ×tamp
file, err := os.OpenFile("latest.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
j.logger.Printf("Error opening latest.txt: %s", err)
@@ -145,8 +145,8 @@ func (j *Journal) parseLine(line string) error {
if err != nil {
return errors.WithStack(err)
}
- if j.lastDate != nil {
- lastDate, err := time.Parse(time.RFC3339, j.lastDate.Format(time.RFC3339))
+ if j.LastDate != nil {
+ lastDate, err := time.Parse(time.RFC3339, j.LastDate.Format(time.RFC3339))
if err != nil {
return errors.WithStack(err)
}
diff --git a/main.go b/main.go
@@ -14,6 +14,11 @@ import (
"github.com/pkg/errors"
)
+type JournalFile struct {
+ path string
+ date time.Time
+}
+
func main() {
// Take as a first positional argument the path to the folder where the journal files are located.
// Take as a second positional argument the commander name.
@@ -29,7 +34,7 @@ func main() {
edsm := edsm.NewEDSM(commanderName, apiKey, logger)
// Find all the journal files in the folder and parse them after sorting them by date.
- files := make(map[string]string)
+ files := make(map[string]JournalFile)
err := filepath.WalkDir(jounnalPath, func(path string, d os.DirEntry, err error) error {
if err != nil {
return errors.WithStack(err)
@@ -42,8 +47,15 @@ func main() {
if d.Name()[:8] == "Journal." {
name := d.Name()
parts := strings.Split(name, ".")
- date := parts[1]
- files[date] = path
+ date := parts[1] + "." + parts[2]
+ parsedDate, err := time.Parse("2006-01-02T150405.00", date)
+ if err != nil {
+ return errors.WithStack(err)
+ }
+ files[date] = JournalFile{
+ path: path,
+ date: parsedDate,
+ }
}
}
@@ -63,7 +75,15 @@ func main() {
journal_obj := journal.NewJournal(edsm, logger)
for _, k := range keys {
// Parse the journal file.
- err := journal_obj.ParseJournal(files[k])
+ // Parse date of the journal file and ignore if older than journal_obj.lastDate.
+
+ currentDate := *journal_obj.LastDate
+ startOfDay := Bod(currentDate)
+ if journal_obj.LastDate != nil && files[k].date.Before(startOfDay) {
+ continue
+ }
+
+ err := journal_obj.ParseJournal(files[k].path)
if err != nil {
logger.Fatalf("Error parsing journal file: %+v", err)
}
@@ -73,3 +93,7 @@ func main() {
logger.Println("Done parsing journal files.")
}
+
+func Bod(t time.Time) time.Time {
+ return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
+}