This commit is contained in:
Balakrishnan Balasubramanian 2022-05-25 16:02:04 -04:00
parent 40cbaadf83
commit 92be9919e3
2 changed files with 20 additions and 17 deletions

View File

@ -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

View File

@ -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("<item>%s</item>", 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("<item>%s</item>", strings.ReplaceAll(r.FeedEntry.Content, "\n", " ")),
})
}
csvw.Flush()
return nil
}