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 {
|
2020-10-17 19:55:05 +00:00
|
|
|
interval := viper.GetInt("REFRESH_TIMEOUT")
|
2020-10-17 13:30:30 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
fmt.Println("Refreshing feeds...")
|
|
|
|
|
|
|
|
for _, feed := range *feedStore.GetFeeds() {
|
|
|
|
go RefreshFeed(feed.FeedURL)
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:51:05 +00:00
|
|
|
fmt.Println("Reaping old items...")
|
|
|
|
feedStore.DeleteOldReadItems()
|
|
|
|
|
2020-10-17 13:30:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-21 09:09:25 +00:00
|
|
|
createdTime := item.PublishedParsed
|
|
|
|
if createdTime == nil {
|
|
|
|
createdTime = item.UpdatedParsed
|
|
|
|
}
|
|
|
|
|
2020-10-17 13:30:30 +00:00
|
|
|
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,
|
2021-02-21 09:09:25 +00:00
|
|
|
Created: createdTime,
|
2020-10-17 13:30:30 +00:00
|
|
|
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
|
|
|
}
|