From 92be9919e3b335bc40b412a1aa999164738ff0df Mon Sep 17 00:00:00 2001
From: balki <3070606-balki@users.noreply.gitlab.com>
Date: Wed, 25 May 2022 16:02:04 -0400
Subject: [PATCH] cleanup
---
TODO.md | 2 +-
app/db.go | 35 +++++++++++++++++++----------------
2 files changed, 20 insertions(+), 17 deletions(-)
diff --git a/TODO.md b/TODO.md
index 408799f..3cd3a52 100644
--- a/TODO.md
+++ b/TODO.md
@@ -26,9 +26,9 @@
* Podcasts
## 1.0
+* ✓ concurrent processing
* Review all errors wrapped properly
* Change log from byte to string
-* concurrent processing
* Config validation
* Cron spec
* Name - No spaces
diff --git a/app/db.go b/app/db.go
index e4b37c1..faf7b1e 100644
--- a/app/db.go
+++ b/app/db.go
@@ -84,33 +84,36 @@ func (d *db) Filter(entries []parser.FeedEntry) ([]parser.FeedEntry, error) {
}
func (d *db) Save(records []Record) error {
+ if len(records) == 0 {
+ return nil
+ }
+
f, err := os.OpenFile(d.dbPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
- defer func() {
- f.Sync()
- f.Close()
- }()
+ defer f.Close()
+
csvw := csv.NewWriter(f)
- if len(d.seenLinks) == 0 { //New file, write header
+ defer csvw.Flush()
+
+ if d.IsNewFeed() {
csvw.Write([]string{
- "Date",
- "Link",
- "Status",
- "Filter",
- "Content",
+ /* 1 */ "Date",
+ /* 2 */ "Link",
+ /* 3 */ "Status",
+ /* 4 */ "Filter",
+ /* 5 */ "Content",
})
}
for _, r := range records {
csvw.Write([]string{
- r.Time.Format(TimeFormat),
- r.FeedEntry.Link,
- string(r.Status),
- r.Filter,
- fmt.Sprintf("- %s
", strings.ReplaceAll(r.FeedEntry.Content, "\n", " ")),
+ /* 1 */ r.Time.Format(TimeFormat),
+ /* 2 */ r.FeedEntry.Link,
+ /* 3 */ string(r.Status),
+ /* 4 */ r.Filter,
+ /* 5 */ fmt.Sprintf("- %s
", strings.ReplaceAll(r.FeedEntry.Content, "\n", " ")),
})
}
- csvw.Flush()
return nil
}