morning-coffee/feed_fetch.go

39 lines
702 B
Go

package main
import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
)
func fetchFeed(url string) {
slog.Info((fmt.Sprintf("Fetching %s\n", url)))
re, err := http.Get(url)
if err != nil {
fmt.Printf("%+v", err)
return
}
defer re.Body.Close()
b, _ := io.ReadAll(re.Body)
d := ParseFeed(b)
links := []Link{}
for _, feedItem := range d.Channel.Items {
links = append(links, Link{Url: feedItem.Link, Title: feedItem.Title, PublishedDate: feedItem.GetPublishedDate()})
}
b, _ = json.Marshal(links)
cacheKey := fmt.Sprintf("feeds:%s", url)
SharedCache.Set(cacheKey, string(b))
}
func refreshFeeds() {
for _, url := range SharedCache.List("feedurl") {
fetchFeed(url)
}
}