Skip to content

Commit

Permalink
feat: use podcast duration tag as reading time
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain de Laage committed Aug 12, 2023
1 parent 168a870 commit b4036b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
29 changes: 28 additions & 1 deletion internal/reader/rss/podcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

package rss // import "miniflux.app/v2/internal/reader/rss"

import "strings"
import (
"fmt"
"math"
"strconv"
"strings"
)

// PodcastFeedElement represents iTunes and GooglePlay feed XML elements.
// Specs:
Expand All @@ -21,6 +26,7 @@ type PodcastFeedElement struct {
type PodcastEntryElement struct {
Subtitle string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd subtitle"`
Summary string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd summary"`
Duration string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd duration"`
GooglePlayDescription string `xml:"http://www.google.com/schemas/play-podcasts/1.0 description"`
}

Expand Down Expand Up @@ -67,3 +73,24 @@ func (e *PodcastEntryElement) PodcastDescription() string {
}
return strings.TrimSpace(description)
}

// normalizeDuration returns the duration tag value as a number of minutes
func normalizeDuration(rawDuration string) (int, error) {
var sumSeconds int

durationParts := strings.Split(rawDuration, ":")
if len(durationParts) > 3 {
return 0, fmt.Errorf("rss: invalid duration format")
}

for i, durationPart := range durationParts {
durationPartValue, err := strconv.Atoi(durationPart)
if err != nil {
return 0, fmt.Errorf("")
}

sumSeconds += int(math.Pow(60, float64(len(durationParts) - i - 1))) * durationPartValue

Check failure on line 92 in internal/reader/rss/podcast.go

View workflow job for this annotation

GitHub Actions / Golang Linter

File is not `gofmt`-ed with `-s` (gofmt)
}

return sumSeconds / 60, nil
}
3 changes: 3 additions & 0 deletions internal/reader/rss/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ func (r *rssItem) Transform() *model.Entry {
entry.Title = r.entryTitle()
entry.Enclosures = r.entryEnclosures()
entry.Tags = r.entryCategories()
if duration, err := normalizeDuration(r.Duration); err == nil {
entry.ReadingTime = duration
}

return entry
}
Expand Down

0 comments on commit b4036b0

Please sign in to comment.