Fix friendpost formatting, make rss fetching parallel
Jes Olson j3s@c3f.net
Mon, 04 Apr 2022 14:53:33 -0400
3 files changed,
57 insertions(+),
25 deletions(-)
M
feed/feed.go
→
feed/feed.go
@@ -1,7 +1,10 @@
package feed import ( + "fmt" "log" + "sync" + "time" "github.com/SlyMarbo/rss" )@@ -25,7 +28,6 @@ "https://sanine.net/rss.xml",
"https://sequentialread.com/rss", "https://spencerkrum.com/index.xml", "https://www.bitquabit.com/index.xml", - "https://www.namecoin.org/feed.rss", "https://www.ryanprior.com/posts/index.xml", } f.CompanyFeeds = []string{@@ -35,6 +37,7 @@ "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",@@ -53,19 +56,45 @@ }
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 { - f, err := rss.Fetch(feed) - if err != nil { - log.Printf(err.Error()) - continue - } - log.Printf("%s\n", f.Title) + 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 +} - fe := *f - friendFeeds = append(friendFeeds, fe) - // todo: make a sort func for []rss.Feed +func fetchFeed(url string) (rss.Feed, error) { + f, err := rss.Fetch(url) + if err != nil { + var empty rss.Feed + return empty, err } - return friendFeeds + return *f, nil +} + +func elapsed(what string) func() { + start := time.Now() + return func() { + fmt.Printf("%s took %v\n", what, time.Since(start)) + } }
M
templates/index.html
→
templates/index.html
@@ -1,7 +1,7 @@
-{{ define "title" }}j3s.sh{{ end }} -{{ define "body" }} +{{- define "title" }}j3s.sh{{- end }} +{{- define "body" }} <p>hello. my name is jes. welcome to my little piece of the internet.</p> -<marquee><p><i>gir: i am Government Man, come from the government. the government has sent me. <3</p></i></marquee> +<marquee><p><i>gir: i am Government Man, come from the government. the government has sent me</p></i></marquee> <h4>email me!</h4> <p>i love getting email. from anyone. from you! it's my favorite way to talk. email me anything! seriously!</p> <p>j3s@c3f.net</p>@@ -10,13 +10,15 @@ <p><i>the only way out is through</p></i>
<p><i>fear is the mind-killer</p></i> <p><i>everything i do is stitched with her color</p></i> <h4>recent posts from my friends on the web</h4> -{{ range $feed := .FriendFeeds }} -<li><a href="{{ (index .Items 0).Link }}"> - {{ if $feed.Title }} - {{ $feed.Title }}: - {{ else }} - {{ $feed.Description }}: - {{ end }} - {{ (index .Items 0).Title }}</a></li> -{{ end }} -{{ end }} +{{- range $feed := .FriendFeeds }} +<li> + <a href="{{ (index .Items 0).Link }}">{{ (index .Items 0).Title }}</a> + {{- if $feed.Title }} + <small> | {{ $feed.Title }}</small> + {{- else if $feed.Description }} + <small> | {{ $feed.Description }}</small> + {{- end }} +</li> +{{- end }} +<p><i>note to my friends: fix your rss author settings :D</i></p> +{{- end }}