go-podcast-proxy/filter-published.go

45 lines
1.4 KiB
Go
Raw Normal View History

package main
import (
"github.com/mmcdole/gofeed"
)
func (ctx* ServerContext) FeedFilter_published(feed PodcastConfig, doc gofeed.Feed) ([]*gofeed.Item, error) {
merge := func(left []*gofeed.Item, right []*gofeed.Item) []*gofeed.Item {
buffer := []*gofeed.Item{}
idxl := 0
idxr := 0
for idxl < len(left) && idxr < len(right) {
if (left[idxl].PublishedParsed.After(*right[idxr].PublishedParsed) && !feed.FilterDirAsc) ||
(left[idxl].PublishedParsed.Before(*right[idxr].PublishedParsed) && feed.FilterDirAsc) {
buffer = append(buffer, left[idxl])
idxl++
} else {
buffer = append(buffer, right[idxr])
idxr++
}
}
for idxl < len(left) {
buffer = append(buffer, left[idxl])
idxl++
}
for idxr < len(right) {
buffer = append(buffer, right[idxr])
idxr++
}
return buffer
}
var sort func(items []*gofeed.Item) []*gofeed.Item
sort = func(items []*gofeed.Item) []*gofeed.Item {
if len(items) < 2 {
return items
} else {
left := sort(items[:len(items)/2])
right := sort(items[len(items)/2:])
return merge(left, right)
}
}
doc.Items = sort(doc.Items)[:feed.FilterLimit]
return doc.Items, nil
}