gopherss/internal/feeds/refresh.go

79 lines
1.7 KiB
Go
Raw Normal View History

2020-10-17 13:30:30 +00:00
package feeds
import (
"encoding/base64"
"fmt"
"strings"
"time"
"github.com/mmcdole/gofeed"
"github.com/spf13/viper"
)
var fp = gofeed.NewParser()
var feedStore = &FeedStore{}
func Refresh() error {
interval := viper.GetInt("refreshTimeoutMinutes")
for {
fmt.Println("Refreshing feeds...")
for _, feed := range *feedStore.GetFeeds() {
go RefreshFeed(feed.FeedURL)
}
fmt.Printf("Going to sleep for %d minutes\n", interval)
time.Sleep(time.Duration(interval) * time.Minute)
}
}
2020-10-17 18:02:52 +00:00
func RefreshFeed(url string) Feed {
2020-10-17 13:30:30 +00:00
fmt.Printf("Refreshing %s\n", url)
2020-10-17 18:02:52 +00:00
var feed Feed
2020-10-17 13:30:30 +00:00
f, err := fp.ParseURL(url)
if err != nil {
fmt.Printf("Failed to refresh %s\n", url)
} else {
imageURL := ""
if f.Image != nil {
imageURL = f.Image.URL
}
2020-10-17 18:02:52 +00:00
feed = Feed{
2020-10-17 13:30:30 +00:00
ID: strings.ReplaceAll(base64.StdEncoding.EncodeToString([]byte(url)), "/", ""),
Title: f.Title,
Description: f.Description,
HomepageURL: f.Link,
FeedURL: url,
ImageURL: imageURL,
LastUpdated: f.UpdatedParsed,
Items: []Item{},
}
for _, item := range f.Items {
imageURL := ""
if f.Image != nil {
imageURL = f.Image.URL
}
feed.Items = append(feed.Items, Item{
ID: strings.ReplaceAll(base64.StdEncoding.EncodeToString([]byte(item.GUID)), "/", ""),
Title: item.Title,
Description: item.Description,
Content: item.Content,
URL: item.Link,
ImageURL: imageURL,
LastUpdated: item.UpdatedParsed,
Created: item.PublishedParsed,
GUID: item.GUID,
FeedID: feed.ID,
})
}
feedStore.SaveFeed(feed)
fmt.Printf("Finished refreshing '%s'\n", feed.Title)
}
2020-10-17 18:02:52 +00:00
return feed
2020-10-17 13:30:30 +00:00
}