Created
July 12, 2024 20:06
-
-
Save bashbunni/02956997046a8fc84317d19592486902 to your computer and use it in GitHub Desktop.
gofeed sorting issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"sort" | |
"time" | |
"github.com/mmcdole/gofeed" | |
) | |
// RSSEntry represents a single RSS entry. | |
type RSSEntry struct { | |
Title string | |
Author string | |
Description string | |
URL string | |
PublishedAt time.Time | |
} | |
func rssFeed(url string, count int) []RSSEntry { | |
var r []RSSEntry | |
fp := gofeed.NewParser() | |
feed, err := fp.ParseURL(url) | |
if err != nil { | |
panic(err) | |
} | |
sort.Sort(feed) | |
for _, v := range feed.Items { | |
// fmt.Printf("%+v\n", v) | |
r = append(r, RSSEntry{ | |
Title: v.Title, | |
Author: v.Author.Name, | |
Description: v.Description, | |
URL: v.Link, | |
PublishedAt: *v.PublishedParsed, | |
}) | |
if len(r) == count { | |
break | |
} | |
} | |
return r | |
} | |
func main() { | |
rssFeed("https://charm.sh/blog/rss.xml", 10) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment