Skip to content

Commit

Permalink
(WIP) Produces valid RSS
Browse files Browse the repository at this point in the history
Still very much P.O.C quality code, i.e., crappy
  • Loading branch information
mprimi committed Mar 19, 2022
1 parent 7501da3 commit 534ba58
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 19 deletions.
25 changes: 6 additions & 19 deletions rss.go → rss-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"fmt"
"net/http"
"net/url"
"os"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/mrusme/superhighway84/config"
"github.com/mrusme/superhighway84/database"
"github.com/mrusme/superhighway84/models"
"github.com/mrusme/superhighway84/rss"
"go.uber.org/zap"
)

Expand All @@ -41,15 +41,6 @@ func NewLogger(filename string) (*zap.Logger, error) {
return cfg.Build()
}

// Apparently there's no `min` for ints in golang
// https://stackoverflow.com/questions/27516387/what-is-the-correct-way-to-find-the-min-between-two-integers-in-go
func minInt(x, y int) int {
if x < y {
return x
}
return y
}

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -118,18 +109,14 @@ func main() {

createResponse := func(w http.ResponseWriter, articles []*models.Article) {

fmt.Fprintf(w, "<RSS HEADER>\n")
feedOptions := rss.NewFeedOptions()
rssFeed := rss.NewFeed(articles, feedOptions)
err := rssFeed.Write(w)

numArticles := minInt(len(articles), 10)

if numArticles > 0 {
for i, article := range articles[0:numArticles] {
fmt.Fprintf(w, "[%d] %s\n", i, article.Subject)
fmt.Fprintf(w, " from:%s in:%s\n", article.From, article.Newsgroup)
}
if err != nil {
log.Printf("Failed to write feed in response: %v", err)
}

fmt.Fprintf(w, "<RSS FOOTER>\n")
}

articlesHandler := func(w http.ResponseWriter, r *http.Request) {
Expand Down
98 changes: 98 additions & 0 deletions rss/rss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package rss

import (
"fmt"
"io"
"github.com/mrusme/superhighway84/models"
)

// ☠️ This is Proof of concept code!
// For example, it's currently composing the feed for each request, which doesn't
// make sense given how (not) frequently the articles update.

type FeedOptions struct {
MaxArticles int
}

type Feed struct {
Options FeedOptions
Articles []*models.Article
}

func NewFeedOptions() (FeedOptions) {
return FeedOptions{10}
}

// Apparently there's no `min` for ints in golang
// https://stackoverflow.com/questions/27516387/what-is-the-correct-way-to-find-the-min-between-two-integers-in-go
func minInt(x, y int) int {
if x < y {
return x
}
return y
}

func NewFeed(articles []*models.Article, feedOptions FeedOptions) (Feed) {

numArticles := minInt(len(articles), feedOptions.MaxArticles)
articlesSlice := articles[0:numArticles]

return Feed{
Options: feedOptions,
Articles: articlesSlice,
}
}

const header = `
<rss version="2.0">
<channel>
<title>%s</title>
<link>%s</link>
<description>%s</description>
<language>%s</language>
<pubDate>%s</pubDate>
<lastBuildDate>%s</lastBuildDate>
<docs>%s</docs>
<generator>%s</generator>
`

const footer = `
</channel>
</rss>
`

const item = `
<item>
<title>%s</title>
<link>%s</link>
<description>%s</description>
<pubDate>%s</pubDate>
<guid>%s</guid>
</item>
`

func (feed *Feed) Write(w io.Writer) (error) {

title := "Superhighway84"
link := "https://xn--gckvb8fzb.com/superhighway84/"
description := "USENET-INSPIRED DECENTRALIZED INTERNET DISCUSSION SYSTEM"
language := "en-us"
pubDate := "Tue, 10 Jun 2003 04:00:00 GMT"
buildDate := "Tue, 10 Jun 2003 09:41:01 GMT"
docsLink := "http://blogs.law.harvard.edu/tech/rss"
generator := "Superhighway84 RSS Generator"
fmt.Fprintf(w, header, title, link, description, language, pubDate, buildDate, docsLink, generator)

for _, article := range feed.Articles {
title := article.Subject
link := fmt.Sprintf("superhighway84://%s", article.ID)
description := fmt.Sprintf("Posted by %s in %s<br>%d replies<br>%s", article.From, article.Newsgroup, len(article.Replies), article.Body)
pubDate := "Tue, 03 Jun 2003 09:39:21 GMT"
guid := article.ID
fmt.Fprintf(w, item, title, link, description, pubDate, guid)
}

fmt.Fprintf(w, footer)

return nil
}

0 comments on commit 534ba58

Please sign in to comment.