From b33407c2e904ce9afe385c57e7687695cbdfe49c Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 22 Dec 2019 11:10:11 +0100 Subject: [PATCH] Only show entries from the last week #2 --- go.mod | 1 + go.sum | 2 ++ main.go | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/go.mod b/go.mod index 31e373d..b374ee6 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module p83.nl/go/weekly go 1.12 require ( + github.com/jinzhu/now v1.1.1 github.com/pkg/errors v0.8.1 // indirect golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect p83.nl/go/ekster v0.0.0-20191119211024-4511657daa0b diff --git a/go.sum b/go.sum index 4e3dad9..9ef4814 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E= +github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= diff --git a/main.go b/main.go index b50c5e4..c2d7357 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,10 @@ import ( "net/http" "net/url" "os" + "time" + "github.com/jinzhu/now" + "p83.nl/go/ekster/pkg/microsub" "willnorris.com/go/microformats" "p83.nl/go/ekster/pkg/jf2" @@ -31,6 +34,8 @@ func main() { md := microformats.Parse(resp.Body, u) items := jf2.SimplifyMicroformatDataItems(md) + items = filterEntriesAfter(items, now.With(time.Now()).Monday()) + t, err := template.ParseFiles("templates/weekly.html") if err != nil { log.Fatal(err) @@ -41,3 +46,17 @@ func main() { log.Fatal(err) } } + +func filterEntriesAfter(items []microsub.Item, from time.Time) []microsub.Item { + var result []microsub.Item + for _, item := range items { + published, err := time.Parse(time.RFC3339, item.Published) + if err != nil { + continue + } + if published.After(from) { + result = append(result, item) + } + } + return result +}