Skip to content

Commit

Permalink
Fixed episodes out-of-order in XML #151 #133
Browse files Browse the repository at this point in the history
Fixed episodes out-of-order in XML #133

Co-authored-by: Yong Tan <yong.tan@tigergraph.com>
  • Loading branch information
qiuhanty and TanYongSql authored Jun 8, 2020
1 parent 13e77d0 commit f8f8bd9
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions pkg/feed/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package feed
import (
"context"
"fmt"
"sort"
"strconv"
"time"

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

// sort.Interface implementation
type timeSlice []*model.Episode

func (p timeSlice) Len() int {
return len(p)
}

// In descending order
func (p timeSlice) Less(i, j int) bool {
return p[i].PubDate.After(p[j].PubDate)
}

func (p timeSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}

func Build(ctx context.Context, feed *model.Feed, cfg *config.Feed, provider urlProvider) (*itunes.Podcast, error) {
const (
podsyncGenerator = "Podsync generator (support us at https://github.com/mxpv/podsync)"
Expand Down Expand Up @@ -51,6 +68,15 @@ func Build(ctx context.Context, feed *model.Feed, cfg *config.Feed, provider url
p.Language = cfg.Custom.Language
}

for _, episode := range feed.Episodes {
if episode.PubDate.IsZero() {
episode.PubDate = now
}
}

// Sort all episodes in descending order
sort.Sort(timeSlice(feed.Episodes))

for i, episode := range feed.Episodes {
if episode.Status != model.EpisodeDownloaded {
// Skip episodes that are not yet downloaded
Expand All @@ -63,16 +89,11 @@ func Build(ctx context.Context, feed *model.Feed, cfg *config.Feed, provider url
Title: episode.Title,
Description: episode.Description,
ISubtitle: episode.Title,
IOrder: strconv.Itoa(i),
// Some app prefer 1-based order
IOrder: strconv.Itoa(i + 1),
}

pubDate := episode.PubDate
if pubDate.IsZero() {
pubDate = now
}

item.AddPubDate(&pubDate)

item.AddPubDate(&episode.PubDate)
item.AddSummary(episode.Description)
item.AddImage(episode.Thumbnail)
item.AddDuration(episode.Duration)
Expand Down

0 comments on commit f8f8bd9

Please sign in to comment.