From d12d8d9889155bc72079117d506638ffb17b8aec Mon Sep 17 00:00:00 2001 From: JJ Style Date: Wed, 28 Aug 2024 23:07:43 +0100 Subject: [PATCH] fix: podcast duration in rss --- .../internal/biz/globalplayer/usecase_test.go | 6 ++-- go.mod | 3 +- go.sum | 4 +-- pkg/globalplayer/feeds/feeds.go | 13 ++++++++- pkg/globalplayer/feeds/feeds_test.go | 29 +++++++++++++++++++ 5 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 pkg/globalplayer/feeds/feeds_test.go diff --git a/cmd/gobal-player-server/internal/biz/globalplayer/usecase_test.go b/cmd/gobal-player-server/internal/biz/globalplayer/usecase_test.go index 356a0eb..0c2bd1d 100644 --- a/cmd/gobal-player-server/internal/biz/globalplayer/usecase_test.go +++ b/cmd/gobal-player-server/internal/biz/globalplayer/usecase_test.go @@ -388,7 +388,7 @@ func Test_useCase_GetEpisodesFeed(t *testing.T) { Enclosure: &feeds.Enclosure{Url: "episode.mp3", Type: "audio/mpeg", Length: "100"}, Link: &feeds.Link{Href: "episode.mp3"}, ITunes: &feeds.ITunesItem{ - Duration: "30m0s", + Duration: "00:30:00", EpisodeType: feeds.ITunesEpisodeTypeFull, }, }, @@ -498,7 +498,7 @@ func Test_useCase_GetAllShowsFeed(t *testing.T) { Enclosure: &feeds.Enclosure{Url: "s1ep1.mp3", Type: "audio/mpeg", Length: "100"}, Link: &feeds.Link{Href: "s1ep1.mp3"}, ITunes: &feeds.ITunesItem{ - Duration: "30m0s", + Duration: "00:30:00", EpisodeType: feeds.ITunesEpisodeTypeFull, }, }, @@ -509,7 +509,7 @@ func Test_useCase_GetAllShowsFeed(t *testing.T) { Enclosure: &feeds.Enclosure{Url: "s2ep1.mp3", Type: "audio/mpeg", Length: "200"}, Link: &feeds.Link{Href: "s2ep1.mp3"}, ITunes: &feeds.ITunesItem{ - Duration: "30m0s", + Duration: "00:30:00", EpisodeType: feeds.ITunesEpisodeTypeFull, }, }, diff --git a/go.mod b/go.mod index 9842184..d7cf49a 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,7 @@ module github.com/jj-style/gobal-player go 1.22.0 -// TODO: remove when https://github.com/gorilla/feeds/pull/110 complete -replace github.com/gorilla/feeds => github.com/jj-style/feeds v0.0.0-20240404232609-6cdbd9ef1ed0 +replace github.com/gorilla/feeds => github.com/jj-style/feeds v0.0.0-20240828212116-0463d0c161ce require ( github.com/adrg/libvlc-go/v3 v3.1.5 diff --git a/go.sum b/go.sum index d55fc04..3cbfca3 100644 --- a/go.sum +++ b/go.sum @@ -84,8 +84,8 @@ github.com/influxdata/influxdb-client-go/v2 v2.13.0 h1:ioBbLmR5NMbAjP4UVA5r9b5xG github.com/influxdata/influxdb-client-go/v2 v2.13.0/go.mod h1:k+spCbt9hcvqvUiz0sr5D8LolXHqAAOfPw9v/RIRHl4= github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf h1:7JTmneyiNEwVBOHSjoMxiWAqB992atOeepeFYegn5RU= github.com/influxdata/line-protocol v0.0.0-20210922203350-b1ad95c89adf/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo= -github.com/jj-style/feeds v0.0.0-20240404232609-6cdbd9ef1ed0 h1:5wD5FEULWtGopHjzpDRr3TCfKrQn6CSNZhXotYJR08I= -github.com/jj-style/feeds v0.0.0-20240404232609-6cdbd9ef1ed0/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y= +github.com/jj-style/feeds v0.0.0-20240828212116-0463d0c161ce h1:mTjHvxiC/7cTbXjUwLsGik0CHiRQvONVopgOHJijRTY= +github.com/jj-style/feeds v0.0.0-20240828212116-0463d0c161ce/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= diff --git a/pkg/globalplayer/feeds/feeds.go b/pkg/globalplayer/feeds/feeds.go index 51965cc..f2f055e 100644 --- a/pkg/globalplayer/feeds/feeds.go +++ b/pkg/globalplayer/feeds/feeds.go @@ -63,7 +63,7 @@ func ToFeed(hc resty.HttpClient, show *models.Show, episodes []*models.Episode, Description: fmt.Sprintf("%s

Available until %s.", item.Description, item.Until.Format("Monday 02 January 2006 15:04:05")), Enclosure: &feeds.Enclosure{Url: item.StreamUrl, Type: "audio/mpeg", Length: fmt.Sprint(contentLength)}, ITunes: &feeds.ITunesItem{ - Duration: fmt.Sprint(duration), + Duration: fmtDuration(duration), EpisodeType: feeds.ITunesEpisodeTypeFull, }, } @@ -79,3 +79,14 @@ func ToFeed(hc resty.HttpClient, show *models.Show, episodes []*models.Episode, return feed, nil } + +// formats a duration as "HH:MM:SS" +func fmtDuration(d time.Duration) string { + d = d.Round(time.Second) + h := d / time.Hour + d -= h * time.Hour + m := d / time.Minute + d -= m * time.Minute + s := d / time.Second + return fmt.Sprintf("%02d:%02d:%02d", h, m, s) +} diff --git a/pkg/globalplayer/feeds/feeds_test.go b/pkg/globalplayer/feeds/feeds_test.go new file mode 100644 index 0000000..a35694c --- /dev/null +++ b/pkg/globalplayer/feeds/feeds_test.go @@ -0,0 +1,29 @@ +package feeds + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestFmtDuration(t *testing.T) { + t.Parallel() + tests := []struct { + name string + given time.Duration + want string + }{ + {name: "seconds", given: time.Second * 3, want: "00:00:03"}, + {name: "minutes and seconds", given: time.Minute + (time.Second * 15), want: "00:01:15"}, + {name: "hours minutes and seconds", given: (time.Hour * 2) + (time.Minute * 30) + (time.Second * 5), want: "02:30:05"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + assert.Equal(t, tt.want, fmtDuration(tt.given)) + }) + } +}