Skip to content

Commit f8f8bd9

Browse files
qiuhantyTanYongSql
andauthored
Fixed episodes out-of-order in XML #151 #133
Fixed episodes out-of-order in XML #133 Co-authored-by: Yong Tan <yong.tan@tigergraph.com>
1 parent 13e77d0 commit f8f8bd9

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

pkg/feed/xml.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package feed
33
import (
44
"context"
55
"fmt"
6+
"sort"
67
"strconv"
78
"time"
89

@@ -13,6 +14,22 @@ import (
1314
"github.com/mxpv/podsync/pkg/model"
1415
)
1516

17+
// sort.Interface implementation
18+
type timeSlice []*model.Episode
19+
20+
func (p timeSlice) Len() int {
21+
return len(p)
22+
}
23+
24+
// In descending order
25+
func (p timeSlice) Less(i, j int) bool {
26+
return p[i].PubDate.After(p[j].PubDate)
27+
}
28+
29+
func (p timeSlice) Swap(i, j int) {
30+
p[i], p[j] = p[j], p[i]
31+
}
32+
1633
func Build(ctx context.Context, feed *model.Feed, cfg *config.Feed, provider urlProvider) (*itunes.Podcast, error) {
1734
const (
1835
podsyncGenerator = "Podsync generator (support us at https://github.com/mxpv/podsync)"
@@ -51,6 +68,15 @@ func Build(ctx context.Context, feed *model.Feed, cfg *config.Feed, provider url
5168
p.Language = cfg.Custom.Language
5269
}
5370

71+
for _, episode := range feed.Episodes {
72+
if episode.PubDate.IsZero() {
73+
episode.PubDate = now
74+
}
75+
}
76+
77+
// Sort all episodes in descending order
78+
sort.Sort(timeSlice(feed.Episodes))
79+
5480
for i, episode := range feed.Episodes {
5581
if episode.Status != model.EpisodeDownloaded {
5682
// Skip episodes that are not yet downloaded
@@ -63,16 +89,11 @@ func Build(ctx context.Context, feed *model.Feed, cfg *config.Feed, provider url
6389
Title: episode.Title,
6490
Description: episode.Description,
6591
ISubtitle: episode.Title,
66-
IOrder: strconv.Itoa(i),
92+
// Some app prefer 1-based order
93+
IOrder: strconv.Itoa(i + 1),
6794
}
6895

69-
pubDate := episode.PubDate
70-
if pubDate.IsZero() {
71-
pubDate = now
72-
}
73-
74-
item.AddPubDate(&pubDate)
75-
96+
item.AddPubDate(&episode.PubDate)
7697
item.AddSummary(episode.Description)
7798
item.AddImage(episode.Thumbnail)
7899
item.AddDuration(episode.Duration)

0 commit comments

Comments
 (0)