Skip to content

Commit

Permalink
add support for value time splits
Browse files Browse the repository at this point in the history
  • Loading branch information
joksas committed Oct 24, 2023
1 parent 1d950db commit acd6469
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/rssblue/types
go 1.18

require github.com/google/go-cmp v0.5.9

require github.com/google/uuid v1.3.1 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
48 changes: 43 additions & 5 deletions namespace_podcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package types
import (
"encoding/xml"
"fmt"
"math"
"strconv"
"strings"
"time"

"github.com/google/uuid"
)

// NamespacePodcast is the Podcasting 2.0 namespace.
Expand Down Expand Up @@ -36,11 +39,12 @@ type PodcastChapters struct {
// PodcastValue enables to describe Value 4 Value payments. Read more at
// https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md#value
type PodcastValue struct {
XMLName xml.Name `xml:"podcast:value"`
Type string `xml:"type,attr"`
Method string `xml:"method,attr"`
Suggested *float64 `xml:"suggested,attr,omitempty"`
Recipients []PodcastValueRecipient
XMLName xml.Name `xml:"podcast:value"`
Type string `xml:"type,attr"`
Method string `xml:"method,attr"`
Suggested *float64 `xml:"suggested,attr,omitempty"`
Recipients []PodcastValueRecipient
ValueTimeSplits []PodcastValueTimeSplit
}

// PodcastValueRecipient describes the recipient of Value 4 Value payments.
Expand All @@ -57,6 +61,29 @@ type PodcastValueRecipient struct {
Fee *bool `xml:"bool,attr"`
}

// PodcastValueTimeSplit describes value splits that are valid for a certain period of time
// Read more at
// https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md#value-time-split
type PodcastValueTimeSplit struct {
XMLName xml.Name `xml:"podcast:valueTimeSplit"`
StartTime DurationInteger `xml:"startTime,attr"`
Duration DurationInteger `xml:"duration,attr"`
RemoteStartTime *DurationInteger `xml:"remoteStartTime,attr,omitempty"`
RemotePercentage *uint `xml:"remotePercentage,attr,omitempty"`
Recipients []PodcastValueRecipient
RemoteItem PodcastRemoteItem
}

// PodcastRemoteItem provides a way to "point" to another feed or item in it.
// Read more at
// https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md#remote-item
type PodcastRemoteItem struct {
XMLName xml.Name `xml:"podcast:remoteItem"`
ItemGUID *string `xml:"itemGuid,attr"`
FeedGUID uuid.UUID `xml:"feedGuid,attr"`
Medium *PodcastMedium `xml:"medium,attr"`
}

// PodcastLocked tells podcast hosting platforms whether they are allowed to import
// the feed. Read more at
// https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md#locked
Expand Down Expand Up @@ -170,6 +197,17 @@ func (duration Duration) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{Name: xml.Name{Local: name.Local}, Value: s}, nil
}

// DurationInteger denotes timestamps and durations during a podcast episode, but which are converted to integer seconds.
type DurationInteger time.Duration

func (duration DurationInteger) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
seconds := time.Duration(duration).Seconds()
seconds = math.Round(seconds)
s := strconv.Itoa(int(seconds))

return xml.Attr{Name: xml.Name{Local: name.Local}, Value: s}, nil
}

// PodcastPerson specifies a person of interest to the podcast. Read more at
// https://github.com/Podcastindex-org/podcast-namespace/blob/main/docs/1.0.md#person
type PodcastPerson struct {
Expand Down
30 changes: 30 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/rssblue/types"
)

Expand Down Expand Up @@ -91,6 +92,29 @@ func TestMarshal(t *testing.T) {
Split: 10,
},
},
ValueTimeSplits: []types.PodcastValueTimeSplit{
{
StartTime: types.DurationInteger(60 * time.Second),
Duration: types.DurationInteger(237 * time.Second),
RemoteItem: types.PodcastRemoteItem{
ItemGUID: pointer("https://podcastindex.org/podcast/4148683#1"),
FeedGUID: uuid.MustParse("a94f5cc9-8c58-55fc-91fe-a324087a655b"),
Medium: pointer(types.PodcastMediumMusic),
},
RemotePercentage: pointer[uint](95),
},
{
StartTime: types.DurationInteger(330 * time.Second),
Duration: types.DurationInteger(53 * time.Second),
RemoteItem: types.PodcastRemoteItem{
ItemGUID: pointer("https://podcastindex.org/podcast/4148683#3"),
FeedGUID: uuid.MustParse("a94f5cc9-8c58-55fc-91fe-a324087a655b"),
Medium: pointer(types.PodcastMediumMusic),
},
RemoteStartTime: pointer(types.DurationInteger(174 * time.Second)),
RemotePercentage: pointer[uint](95),
},
},
},
PodcastGUID: pointer(types.PodcastGUID("cda647ce-56b8-5d7c-9448-ba1993ab46b7")),
PodcastMedium: &types.PodcastMediumPodcast,
Expand Down Expand Up @@ -375,6 +399,12 @@ func TestMarshal(t *testing.T) {
<podcast:valueRecipient name="Co-Host #1" type="node" address="02d5c1bf8b940dc9cadca86d1b0a3c37fbe39cee4c7e839e33bef9174531d27f52" split="50"></podcast:valueRecipient>
<podcast:valueRecipient name="Co-Host #2" type="node" address="032f4ffbbafffbe51726ad3c164a3d0d37ec27bc67b29a159b0f49ae8ac21b8508" split="40"></podcast:valueRecipient>
<podcast:valueRecipient name="Producer" type="node" address="03ae9f91a0cb8ff43840e3c322c4c61f019d8c1c3cea15a25cfc425ac605e61a4a" split="10"></podcast:valueRecipient>
<podcast:valueTimeSplit startTime="60" duration="237" remotePercentage="95">
<podcast:remoteItem itemGuid="https://podcastindex.org/podcast/4148683#1" feedGuid="a94f5cc9-8c58-55fc-91fe-a324087a655b" medium="music"></podcast:remoteItem>
</podcast:valueTimeSplit>
<podcast:valueTimeSplit startTime="330" duration="53" remoteStartTime="174" remotePercentage="95">
<podcast:remoteItem itemGuid="https://podcastindex.org/podcast/4148683#3" feedGuid="a94f5cc9-8c58-55fc-91fe-a324087a655b" medium="music"></podcast:remoteItem>
</podcast:valueTimeSplit>
</podcast:value>
<item>
<description>This is a simple episode &amp; its description.</description>
Expand Down

0 comments on commit acd6469

Please sign in to comment.