2023-09-24 20:57:56 +00:00
|
|
|
// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-10-25 04:14:32 +00:00
|
|
|
package rss
|
2023-09-21 18:03:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-26 00:16:36 +00:00
|
|
|
"strings"
|
2023-10-25 04:14:32 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/microcosm-cc/bluemonday"
|
2023-09-21 18:03:21 +00:00
|
|
|
|
|
|
|
"github.com/mmcdole/gofeed"
|
|
|
|
)
|
|
|
|
|
2023-10-25 04:14:32 +00:00
|
|
|
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)
|
2023-10-25 04:14:32 +00:00
|
|
|
return nil, err
|
2023-09-21 18:03:21 +00:00
|
|
|
}
|
|
|
|
|
2023-10-25 04:14:32 +00:00
|
|
|
releases := make([]Release, 0)
|
|
|
|
|
2023-09-21 18:03:21 +00:00
|
|
|
for _, item := range feed.Items {
|
2023-10-25 04:14:32 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-25 04:14:32 +00:00
|
|
|
return releases, nil
|
2023-09-21 18:03:21 +00:00
|
|
|
}
|