small pixel drawing of a pufferfish j3s.sh

feed/feed.go

package feed

import (
	"fmt"
	"log"
	"sync"
	"time"

	"github.com/SlyMarbo/rss"
)

type feeds struct {
	FriendFeeds  []string
	CompanyFeeds []string
	RandomFeeds  []string
}

func GetFeeds() feeds {
	f := feeds{}
	f.FriendFeeds = []string{
		"https://begriffs.com/atom.xml",
		"https://blog.jse.li/index.xml",
		"https://coderanger.net/atom.xml",
		"https://davebucklin.com/feed.xml",
		"https://facklambda.dev/atom.xml",
		"https://kattraxler.github.io/feed.xml",
		"https://sanine.net/rss.xml",
		"https://sequentialread.com/rss",
		"https://spencerkrum.com/index.xml",
		"https://www.bitquabit.com/index.xml",
		"https://www.ryanprior.com/posts/index.xml",
		"https://100r.co/links/rss.xml",
	}
	f.CompanyFeeds = []string{
		"https://blog.tildes.net/all.atom.xml",
		"https://cyberia.club/blog/blog.xml",
		"https://frame.work/blog.rss",
		"https://go.dev/blog/feed.atom",
		"https://matrix.org/blog/feed",
		"https://sourcehut.org/blog/index.xml",
		"https://www.namecoin.org/feed.rss",
	}
	f.RandomFeeds = []string{
		"http://antirez.com/rss",
		"https://andrewkelley.me/rss.xml",
		"https://drewdevault.com/feed.xml",
		"https://emersion.fr/blog/rss.xml",
		"https://emilydamstra.com/feed",
		"https://georgerrmartin.com/notablog/feed",
		"https://linuxwit.ch/feed.xml",
		"https://rodarmor.com/feed/feed.xml",
		"https://staffeng.com/rss",
		"https://tilde.team/~kiedtl/atom.xml",
		"https://herman.bearblog.dev/feed/",
	}
	return f
}

func FetchFriendFeeds() []rss.Feed {
	feeds := GetFeeds()
	var friendFeeds []rss.Feed
	var wg sync.WaitGroup
	var m = sync.Mutex{}
	wg.Add(len(feeds.FriendFeeds))
	for _, feed := range feeds.FriendFeeds {
		go func(feed string) {
			defer elapsed(feed)()
			defer wg.Done()
			log.Printf("feed is being fetched %s\n", feed)
			f, err := fetchFeed(feed)
			log.Printf("feed has been fetched %s\n", feed)
			// maybe remove this if it becomes unuseful
			if err != nil {
				log.Printf("error fetching feed %s", feed)
				return
			}
			log.Printf("locking mutex for %s\n", feed)
			m.Lock()
			friendFeeds = append(friendFeeds, f)
			log.Printf("unlocking mutex for %s\n", feed)
			m.Unlock()
		}(feed)
	}
	wg.Wait()
	return friendFeeds
}

func fetchFeed(url string) (rss.Feed, error) {
	f, err := rss.Fetch(url)
	if err != nil {
		var empty rss.Feed
		return empty, err
	}
	return *f, nil
}

func elapsed(what string) func() {
	start := time.Now()
	return func() {
		fmt.Printf("%s took %v\n", what, time.Since(start))
	}
}