Move feed sort to feed package
Jes Olson j3s@c3f.net
Mon, 18 Jul 2022 02:29:02 -0500
2 files changed,
18 insertions(+),
13 deletions(-)
M
feed/feed.go
→
feed/feed.go
@@ -2,6 +2,8 @@ package feed
import ( "log" + "sort" + "github.com/SlyMarbo/rss" )@@ -51,3 +53,18 @@ feeds = append(feeds, *feed)
} return feeds } + +func SortItems(f []rss.Feed) []rss.Item { + var posts []rss.Item + for _, f := range f { + for _, i := range f.Items { + posts = append(posts, *i) + } + } + + // magick slice sorter by date + sort.Slice(posts, func(i, j int) bool { + return posts[i].Date.After(posts[j].Date) + }) + return posts +}
M
main.go
→
main.go
@@ -10,7 +10,6 @@ "math/rand"
"net/http" "os" "path/filepath" - "sort" "strings" "time"@@ -237,19 +236,8 @@ <body>
<table> <tbody>`) - var posts []rss.Item - for _, f := range data.Feeds { - for _, i := range f.Items { - posts = append(posts, *i) - } - } - - // magick slice sorter by date - sort.Slice(posts, func(i, j int) bool { - return posts[i].Date.After(posts[j].Date) - }) - // printf the shit + posts := feed.SortItems(data.Feeds) for _, p := range posts { fmt.Fprintf(w, "<tr><td>") fmt.Fprintf(w, `<a href="%s">%s</a>`, p.Link, p.Title)