small pixel drawing of a pufferfish j3s.sh

Make /feeds work
Jes Olson j3s@c3f.net
Sun, 17 Jul 2022 23:30:11 -0500
commit

76d03b177347f60446f17ea4ba2addf1f42370fa

parent

eca2730ab85d3aa56ab8341c10ffade1a9fe56d1

3 files changed, 27 insertions(+), 12 deletions(-)

jump to
M feed/feed.gofeed/feed.go

@@ -1,6 +1,7 @@

package feed import ( + "log" "github.com/SlyMarbo/rss" )

@@ -37,19 +38,16 @@ "https://www.namecoin.org/feed.rss",

"https://www.ryanprior.com/posts/index.xml", } -// todo: update feeds once in awhile? every 30 mins? hour? -// todo: async -// feed.GetAll +// todo: async (nevermind, not really needed now tbh with new update method) func GetAllFeeds() []rss.Feed { var feeds []rss.Feed for _, f := range lovelyFeedList { feed, err := rss.Fetch(f) if err != nil { - panic(err) + log.Println(err) + continue } feeds = append(feeds, *feed) - // fetch the feed, add it to the list } return feeds } -
M main.gomain.go

@@ -5,6 +5,7 @@ "bytes"

"fmt" "time" "html/template" + "sort" "io" "log" "math/rand"

@@ -39,7 +40,8 @@ // the populate function populates the global "data" variable

// with ... data. with which to pass into templates. func (t *templateData) populate() { t.Feeds = feed.GetAllFeeds() - // refresh all rss feeds periodically + // refresh all rss feeds periodically idk 60 mins seems + // like a fine-ass interval for range time.Tick(time.Minute * 60) { log.Println("updating feeds tbh") for i := range t.Feeds {

@@ -57,7 +59,7 @@

fs := http.FileServer(http.Dir("./static")) http.Handle("/static/", http.StripPrefix("/static/", fs)) - http.HandleFunc("/feed", feedHandler) + http.HandleFunc("/feeds", feedHandler) http.HandleFunc("/feed.atom", atom.Handler) http.HandleFunc("/age.html", serveAge) http.HandleFunc("/ip", ipHandler)

@@ -231,12 +233,26 @@ <head>

<meta charset=utf-8> <title>j3s's rss feeds</title> </head> -<body>`) - // todo: finish this - // todo: sort them +<body> +<table> +<tbody>`) + + var posts []rss.Item for _, f := range data.Feeds { for _, i := range f.Items { - fmt.Fprintf(w, "%s\n", i.Link) + 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 + for _, p := range posts { + fmt.Fprintf(w, "<tr><td>") + fmt.Fprintf(w, `<a href="%s">%s</a>`, p.Link, p.Title) + fmt.Fprintf(w, "</tr></td>") } }
M templates/index.htmltemplates/index.html

@@ -65,6 +65,7 @@

of their divinity</pre> </details> <h4>posts from my friends</h4> +<p>note: this list is too big rn, it'll be trimmed to just my friends soon</p> {{- range $feed := .Feeds }} <li> <a href="{{ (index .Items 0).Link }}">{{ (index .Items 0).Title }}</a>