From df939a1b50fe942f230da2a7d7476ce3e16b3396 Mon Sep 17 00:00:00 2001 From: yequari Date: Wed, 6 Mar 2024 13:27:37 -0700 Subject: [PATCH] add more time formats --- feed/export_test.go | 4 ---- feed/feed.go | 8 +++++-- feed/feed_test.go | 53 +++++++++++++++++++++++++++++---------------- 3 files changed, 40 insertions(+), 25 deletions(-) delete mode 100644 feed/export_test.go diff --git a/feed/export_test.go b/feed/export_test.go deleted file mode 100644 index c5d57e7..0000000 --- a/feed/export_test.go +++ /dev/null @@ -1,4 +0,0 @@ -package feed - -var FetchPage = fetchPage -var ParseTime = parseTime diff --git a/feed/feed.go b/feed/feed.go index 83bdc26..e95fd89 100644 --- a/feed/feed.go +++ b/feed/feed.go @@ -73,6 +73,9 @@ func parseTime(timestr string) (time.Time, error) { time.RFC3339Nano, time.DateTime, time.DateOnly, + "2006-01-02 15:04", + "2006-01-02T15:04:05", + "2006-01-02T15:04", } var pagetime time.Time var err error @@ -115,9 +118,10 @@ func (f *FeedItem) ParseContent(content string) error { } etitle, err := getHtmlElement(doc, "title") if err != nil { - return err + f.Title = "" + } else { + f.Title = etitle.FirstChild.Data } - f.Title = etitle.FirstChild.Data var articleBuilder strings.Builder html.Render(&articleBuilder, earticle) diff --git a/feed/feed_test.go b/feed/feed_test.go index 93d5b5f..a5cbf2e 100644 --- a/feed/feed_test.go +++ b/feed/feed_test.go @@ -3,50 +3,65 @@ package feed_test import ( "testing" "time" - "git.32bit.cafe/yequari/webweav.ing/feed" + "git.32bit.cafe/yequari/webweav.ing/feed" ) -func TestArticleParse(t *testing.T) { - testDate, err := time.Parse("2006-Jan-02", "2004-May-14") +func TestTimeParsing(t *testing.T) { + testDate, err := time.Parse("2006-Jan-02 15:04:05 -7", "2004-May-14 07:30:55 -7") if err != nil { t.Errorf("creating test date failed: %s", err) } + var tests = []struct { name string input string - want_time *time.Time - want_article string + format string }{ { - "article stripped out of basic HTML", - "
hello world
", - nil, - "
hello world
", + "YYYY-MM-DD", + `
hello world
`, + time.DateOnly, }, { - "article and time stripped out of basic HTML", - "
hello world
", - &testDate, - "
hello world
", + "YYYY-MM-DD HH:MM", + `
hello world
`, + "2006-01-02 15:04", + }, + { + "YYYY-MM-DD HH:MM:SS", + `
hello world
`, + "2006-01-02 15:04:05", + }, + { + "YYYY-MM-DDTHH:MM:SS", + `
hello world
`, + "2006-01-02T15:04:05", + }, + { + "YYYY-MM-DDTHH:MM", + `
hello world
`, + "2006-01-02T15:04", }, } for _, tt := range tests { t.Run(tt.name, func (t *testing.T) { - article, articleTime, err := feed.ParseArticle(tt.input) + item := feed.FeedItem{} + err := item.ParseContent(tt.input) if err != nil { t.Errorf("error: %s", err) } - if article != tt.want_article { - t.Errorf("got %s, want %s", article, tt.want_article) - } - if tt.want_time != nil && !articleTime.Equal(*tt.want_time) { - t.Errorf("got %s, want %s", articleTime, *tt.want_time) + + actualTime := item.PubTime.Format(tt.format) + expectedTime := testDate.Format(tt.format) + if actualTime != expectedTime { + t.Errorf("got %s, want %s", actualTime, expectedTime) } }) } } + func TestParseFeedItem(t *testing.T) { testDate, err := time.Parse("2006-Jan-02", "2004-May-14") if err != nil { -- 2.30.2