small pixel drawing of a pufferfish jrss

main.go

package main

import (
	"encoding/csv"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"sort"
	"strings"

	"github.com/mmcdole/gofeed"
)

func rootHandler(w http.ResponseWriter, r *http.Request) {
	feeds, err := getFeedList(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	posts := make([]gofeed.Feed, 0)
	var gottenFeed *gofeed.Feed
	for _, f := range feeds {
		gottenFeed, err = getFeed(f)
		if err != nil {
			// in the case that a feed fails
			// to be fetched, skip it.
			log.Println(err)
			continue
		}
		posts = append(posts, *gottenFeed)
	}
	beegFeed := gofeed.Feed{}
	for _, p := range posts {
		for _, i := range p.Items {
			// descriptions are sometimes set to the entire
			// content of blog posts. set it to title because
			// i don't really care about descriptions
			// maybe revisit this later and cut large
			// descriptions down to a reasonable size
			i.Description = p.Title
			beegFeed.Items = append(beegFeed.Items, i)
		}
	}
	sort.Sort(sort.Reverse(beegFeed))
	for _, i := range beegFeed.Items {
		fmt.Fprintf(w, "<p>%s: <a href=\"%s\">%s</a></p>",
			i.Description, i.Link, i.Title)
	}
}

func getFeed(url string) (*gofeed.Feed, error) {
	fp := gofeed.NewParser()
	feed, err := fp.ParseURL(url)
	return feed, err
}

func getFeedList(url string) ([]string, error) {
	res, err := http.Get(url)
	if err != nil {
		err = fmt.Errorf("getFeedList http.Get: %w", err)
		log.Fatal(err)
	}
	robots, err := ioutil.ReadAll(res.Body)
	if err != nil {
		log.Fatal(err)
	}
	res.Body.Close()
	r := csv.NewReader(strings.NewReader(string(robots)))
	feeds := make([]string, 0)
	for {
		record, err := r.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		// [0] == url to a feed
		feeds = append(feeds, record[0])
	}
	return feeds, err
}

func main() {
	// TODO: check whether ARG1 exists
	http.HandleFunc("/", rootHandler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}