Skip to content

Commit

Permalink
Unmarshal/MarshalYAML for ChapterSegment
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Oct 8, 2024
1 parent 1d78952 commit 7fbcad8
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 22 deletions.
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,50 @@ github.com/urfave/cli/v2 v2.27.4 h1:o1owoI+02Eb+K107p27wEX9Bb8eqIoZCfLXloLUSWJ8=
github.com/urfave/cli/v2 v2.27.4/go.mod h1:m4QzxcD2qpra4z7WhzEGn74WZLViBnMpb1ToCAKdGRQ=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.23.0 h1:F6D4vR+EHoL9/sWAWgAR1H2DcHr4PareCbAaCo1RpuU=
golang.org/x/term v0.23.0/go.mod h1:DgV24QBUrK6jhZXl+20l6UWznPlwAHm1Q1mGHtydmSk=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
5 changes: 0 additions & 5 deletions internal/cast/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ type Audio struct {
mediaType MediaType
}

type ChapterSegment struct {
Title string `json:"title"`
Start uint64 `json:"start"`
}

func LoadAudio(fname string) (*Audio, error) {
if _, err := os.Stat(fname); err == nil {
return readAudio(fname)
Expand Down
65 changes: 65 additions & 0 deletions internal/cast/chapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cast

import (
"fmt"
"strings"
)

type Chapter struct {
Segments []*ChapterSegment
Body string
}

type ChapterSegment struct {
Title string `json:"title"`
Start uint64 `json:"start"`
}

func convertStartToString(start uint64) string {
seconds := start % 60
minutes := (start / 60) % 60
hours := start / 3600
startTime := fmt.Sprintf("%d:%02d", minutes, seconds)
if hours > 0 {
startTime = fmt.Sprintf("%d:%02d:%02d", hours, minutes, seconds)
}
return startTime
}

func convertStringToStart(str string) (uint64, error) {
if l := len(strings.Split(str, ":")); l > 3 {
return 0, fmt.Errorf("invalid time format: %s", str)
} else if l == 2 {
str = "0:" + str
}
var h, m, s uint64
if _, err := fmt.Sscanf(str, "%d:%d:%d", &h, &m, &s); err != nil {
return 0, fmt.Errorf("invalid time format: %s", str)
}
return h*3600 + m*60 + s, nil
}

func (chs *ChapterSegment) String() string {
return fmt.Sprintf("%s %s", convertStartToString(chs.Start), chs.Title)
}

func (chs *ChapterSegment) UnmarshalYAML(b []byte) error {
str := strings.TrimSpace(string(b))
stuff := strings.SplitN(str, " ", 2)
if len(stuff) != 2 {
return fmt.Errorf("invalid chapter format: %s", str)
}
start, err := convertStringToStart(stuff[0])
if err != nil {
return fmt.Errorf("invalid chapter format: %s, %w", str, err)
}
*chs = ChapterSegment{
Title: stuff[1],
Start: start,
}
return nil
}

func (chs *ChapterSegment) MarshalYAML() ([]byte, error) {
return []byte(chs.String()), nil
}
111 changes: 111 additions & 0 deletions internal/cast/chapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package cast_test

import (
"testing"

"github.com/Songmu/podbard/internal/cast"
"github.com/goccy/go-yaml"
)

func TestChapterSegment_MarshalYAML(t *testing.T) {
t.Parallel()

tests := []struct {
name string
chapter struct {
Segment *cast.ChapterSegment
}
want string
}{{
name: "simple",
chapter: struct{ Segment *cast.ChapterSegment }{
Segment: &cast.ChapterSegment{
Title: "Chapter 1",
Start: 0,
},
},
want: "segment: 0:00 Chapter 1\n",
}, {
name: "with hours",
chapter: struct{ Segment *cast.ChapterSegment }{
Segment: &cast.ChapterSegment{
Title: "Chapter 2",
Start: 3600,
},
},
want: "segment: 1:00:00 Chapter 2\n",
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b, err := yaml.Marshal(tt.chapter)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got := string(b); got != tt.want {
t.Errorf("got %q; want %q", got, tt.want)
}
})
}
}

func TestChapterSegment_UnmarshalYAML(t *testing.T) {
t.Parallel()

tests := []struct {
name string
input string
want cast.ChapterSegment
wantErr bool
}{{
name: "simple",
input: "0:00 Chapter 1",
want: cast.ChapterSegment{
Title: "Chapter 1",
Start: 0,
},
}, {
name: "with hours",
input: "1:00:00 Chapter 2",
want: cast.ChapterSegment{
Title: "Chapter 2",
Start: 3600,
},
}, {
name: "invalid format",
input: "invalid",
wantErr: true,
}, {
name: "invalid time format",
input: "1:00:00:00 Chapter 2",
wantErr: true,
}, {
name: "invalid hours",
input: "a:00:00 Chapter 2",
wantErr: true,
}, {
name: "invalid minutes",
input: "1:a:00 Chapter 2",
wantErr: true,
}, {
name: "invalid seconds",
input: "1:00:a Chapter 2",
wantErr: true,
}}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got cast.ChapterSegment
err := yaml.Unmarshal([]byte(tt.input), &got)
if err != nil {
if !tt.wantErr {
t.Fatalf("unexpected error: %v", err)
}
return
}
if got != tt.want {
t.Errorf("got %v; want %v", got, tt.want)
}
})
}
}
25 changes: 8 additions & 17 deletions internal/cast/episode.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func LoadEpisode(
Title: title,
Subtitle: subtitle,
Date: pubDate.Format(time.RFC3339),
Chapter: au.Chapters,
}
b, err := yaml.Marshal(epm)
if err != nil {
Expand Down Expand Up @@ -323,16 +324,12 @@ type Episode struct {
audioBaseURL *url.URL
}

type Chapter struct {
Segments []*ChapterSegment
Body string
}

type EpisodeFrontMatter struct {
AudioFile string `yaml:"audio"`
Title string `yaml:"title"`
Date string `yaml:"date"`
Subtitle string `yaml:"subtitle"`
AudioFile string `yaml:"audio"`
Title string `yaml:"title"`
Date string `yaml:"date"`
Subtitle string `yaml:"subtitle"`
Chapter []*ChapterSegment `yaml:"chapter,omitempty"`

audio *Audio
pubDate time.Time
Expand Down Expand Up @@ -361,6 +358,7 @@ func (ep *Episode) init(loc *time.Location) error {
return err
}
ep.Chapter = chapter
ep.EpisodeFrontMatter.Chapter = chapter.Segments
}
md := NewMarkdown()
var buf bytes.Buffer
Expand All @@ -381,19 +379,12 @@ func (chap *Chapter) init() error {
Start string
}{}
for _, ch := range chap.Segments {
seconds := ch.Start % 60
minutes := (ch.Start / 60) % 60
hours := ch.Start / 3600
start := fmt.Sprintf("%d:%02d", minutes, seconds)
if hours > 0 {
start = fmt.Sprintf("%d:%02d:%02d", hours, minutes, seconds)
}
data = append(data, struct {
Title string
Start string
}{
Title: ch.Title,
Start: start,
Start: convertStartToString(ch.Start),
})
}
var buf bytes.Buffer
Expand Down
Binary file modified testdata/dev/audio/3.mp3
Binary file not shown.

0 comments on commit 7fbcad8

Please sign in to comment.