willow/rss/rss.go

51 lines
916 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"
2023-10-26 00:16:36 +00:00
"strings"
"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()
2023-10-26 00:16:36 +00:00
feed, err := fp.ParseURL(strings.TrimSuffix(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,
})
}
return releases, nil
2023-09-21 18:03:21 +00:00
}