willow/rss/rss.go

52 lines
1016 B
Go
Raw Normal View History

2023-09-24 20:57:56 +00:00
// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: Apache-2.0
package rss
2023-09-21 18:03:21 +00:00
import (
"fmt"
"time"
"github.com/microcosm-cc/bluemonday"
2023-09-21 18:03:21 +00:00
"github.com/mmcdole/gofeed"
)
type Release struct {
Tag string
Content string
URL string
Date time.Time
}
var (
bmUGC = bluemonday.UGCPolicy()
bmStrict = bluemonday.StrictPolicy()
)
func GetReleases(feedURL string) ([]Release, error) {
2023-09-21 18:03:21 +00:00
fp := gofeed.NewParser()
feed, err := fp.ParseURL(feedURL + "/releases.atom")
2023-09-21 18:03:21 +00:00
if err != nil {
fmt.Println(err)
return nil, err
2023-09-21 18:03:21 +00:00
}
releases := make([]Release, 0)
2023-09-21 18:03:21 +00:00
for _, item := range feed.Items {
releases = append(releases, Release{
2023-09-21 18:03:21 +00:00
Tag: bmStrict.Sanitize(item.Title),
Content: bmUGC.Sanitize(item.Content),
URL: bmStrict.Sanitize(item.Link),
Date: *item.PublishedParsed,
})
}
// TODO: Doesn't seem to work?
// sort.Slice(p.Releases, func(i, j int) bool { return p.Releases[i].Date.After(p.Releases[j].Date) })
return releases, nil
2023-09-21 18:03:21 +00:00
}