From 7a8e5029e85621af1740b2112809f9ee4084aaf6 Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Mon, 2 May 2022 00:36:27 -0400 Subject: [PATCH] Almost working. csv last record is not written properly --- app/app.go | 2 ++ app/db.go | 19 +++++++++++++++---- app/schedule.go | 4 ++-- telegram/telegram.go | 11 ++++++----- tss.yml | 1 + 5 files changed, 26 insertions(+), 11 deletions(-) create mode 120000 tss.yml diff --git a/app/app.go b/app/app.go index 71ade10..c81fd8c 100644 --- a/app/app.go +++ b/app/app.go @@ -71,6 +71,8 @@ func ProcessFeed(feed FeedCfg, scheduler *Scheduler, dbDir string, tgram telegra return } + scheduler.Good(feed.Name) + filteredEntries, err := db.Filter(entries) if err != nil { log.Error("failed to filter entries", "feed", feed.Name, "error", err) diff --git a/app/db.go b/app/db.go index 7575ed7..2746406 100644 --- a/app/db.go +++ b/app/db.go @@ -2,10 +2,14 @@ package app import ( "encoding/csv" + "errors" "fmt" "os" "path" + "strings" "time" + + "go.balki.me/tss/log" ) type Status string @@ -37,8 +41,14 @@ type db struct { func NewDB(storageDir, feedName string) (DB, error) { dbPath := path.Join(storageDir, fmt.Sprintf("%s.csv", feedName)) + db := db{dbPath: dbPath} + db.seenLinks = map[string]struct{}{} f, err := os.Open(dbPath) if err != nil { + if errors.Is(err, os.ErrNotExist) { + log.Info("db file does not exist, will be created", "feed", feedName, "path", dbPath) + return &db, nil + } return nil, err } defer f.Close() @@ -47,8 +57,6 @@ func NewDB(storageDir, feedName string) (DB, error) { if err != nil { return nil, fmt.Errorf("failed to parse csv, path:%v, error:%w", dbPath, err) } - db := db{dbPath: dbPath} - db.seenLinks = map[string]struct{}{} for _, rec := range records { var recStatus Status = Status(rec[2]) if recStatus == Sent || recStatus == Filtered { @@ -73,7 +81,10 @@ func (d *db) Save(records []Record) error { if err != nil { return err } - defer f.Close() + defer func() { + f.Sync() + f.Close() + }() csvw := csv.NewWriter(f) if len(d.seenLinks) == 0 { //New file, write header csvw.Write([]string{ @@ -90,7 +101,7 @@ func (d *db) Save(records []Record) error { r.FeedEntry.Link, string(r.Status), "-", - r.FeedEntry.Content, + strings.ReplaceAll(r.FeedEntry.Content, "\n", " "), }) } return nil diff --git a/app/schedule.go b/app/schedule.go index fcce36a..308d833 100644 --- a/app/schedule.go +++ b/app/schedule.go @@ -3,11 +3,11 @@ package app import ( "errors" "fmt" - "log" "os" "time" "github.com/robfig/cron/v3" + "go.balki.me/tss/log" "gopkg.in/yaml.v3" ) @@ -28,7 +28,7 @@ func NewScheduler(filePath string) (*Scheduler, error) { return nil, fmt.Errorf("path:%v does not exist and unable to create: err: %w", filePath, err) } f.Close() - log.Printf("file: %v does not exist. Will be created\n", filePath) + log.Info("scheduler file does not exist, will be created", "path", filePath) } else { err = yaml.Unmarshal(data, &s.lastSuccessTime) if err != nil { diff --git a/telegram/telegram.go b/telegram/telegram.go index d49efa6..42b7184 100644 --- a/telegram/telegram.go +++ b/telegram/telegram.go @@ -26,13 +26,14 @@ type telegramSender struct { } func (ts *telegramSender) SendLink(link string, channel string, rhash string) error { - msg := struct { - ChatID string `json:"chat_id"` - Text string `json:"text"` + ChatID string `json:"chat_id"` + Text string `json:"text"` + ParseMode string `json:"parse_mode"` }{ - ChatID: channel, - Text: fmt.Sprintf(` Link`, genIVLink(link, rhash), link), + ChatID: channel, + Text: fmt.Sprintf(` Link`, genIVLink(link, rhash), link), + ParseMode: "HTML", } data, err := json.Marshal(msg) diff --git a/tss.yml b/tss.yml new file mode 120000 index 0000000..7ccbcdf --- /dev/null +++ b/tss.yml @@ -0,0 +1 @@ +w.tmp/tss.yaml \ No newline at end of file