podcast.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package rss // import "miniflux.app/v2/internal/reader/rss"
  4. import (
  5. "fmt"
  6. "math"
  7. "strconv"
  8. "strings"
  9. )
  10. // PodcastFeedElement represents iTunes and GooglePlay feed XML elements.
  11. // Specs:
  12. // - https://github.com/simplepie/simplepie-ng/wiki/Spec:-iTunes-Podcast-RSS
  13. // - https://developers.google.com/search/reference/podcast/rss-feed
  14. type PodcastFeedElement struct {
  15. ItunesAuthor string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd channel>author"`
  16. Subtitle string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd channel>subtitle"`
  17. Summary string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd channel>summary"`
  18. PodcastOwner PodcastOwner `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd channel>owner"`
  19. GooglePlayAuthor string `xml:"http://www.google.com/schemas/play-podcasts/1.0 channel>author"`
  20. }
  21. // PodcastEntryElement represents iTunes and GooglePlay entry XML elements.
  22. type PodcastEntryElement struct {
  23. Subtitle string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd subtitle"`
  24. Summary string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd summary"`
  25. Duration string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd duration"`
  26. GooglePlayDescription string `xml:"http://www.google.com/schemas/play-podcasts/1.0 description"`
  27. }
  28. // PodcastOwner represents contact information for the podcast owner.
  29. type PodcastOwner struct {
  30. Name string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd name"`
  31. Email string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd email"`
  32. }
  33. // Image represents podcast artwork.
  34. type Image struct {
  35. URL string `xml:"href,attr"`
  36. }
  37. // PodcastAuthor returns the author of the podcast.
  38. func (e *PodcastFeedElement) PodcastAuthor() string {
  39. author := ""
  40. switch {
  41. case e.ItunesAuthor != "":
  42. author = e.ItunesAuthor
  43. case e.GooglePlayAuthor != "":
  44. author = e.GooglePlayAuthor
  45. case e.PodcastOwner.Name != "":
  46. author = e.PodcastOwner.Name
  47. case e.PodcastOwner.Email != "":
  48. author = e.PodcastOwner.Email
  49. }
  50. return strings.TrimSpace(author)
  51. }
  52. // PodcastDescription returns the description of the podcast.
  53. func (e *PodcastEntryElement) PodcastDescription() string {
  54. description := ""
  55. switch {
  56. case e.GooglePlayDescription != "":
  57. description = e.GooglePlayDescription
  58. case e.Summary != "":
  59. description = e.Summary
  60. case e.Subtitle != "":
  61. description = e.Subtitle
  62. }
  63. return strings.TrimSpace(description)
  64. }
  65. var invalidDurationFormatErr = fmt.Errorf("rss: invalid duration format")
  66. // normalizeDuration returns the duration tag value as a number of minutes
  67. func normalizeDuration(rawDuration string) (int, error) {
  68. var sumSeconds int
  69. durationParts := strings.Split(rawDuration, ":")
  70. if len(durationParts) > 3 {
  71. return 0, invalidDurationFormatErr
  72. }
  73. for i, durationPart := range durationParts {
  74. durationPartValue, err := strconv.Atoi(durationPart)
  75. if err != nil {
  76. return 0, invalidDurationFormatErr
  77. }
  78. sumSeconds += int(math.Pow(60, float64(len(durationParts)-i-1))) * durationPartValue
  79. }
  80. return sumSeconds / 60, nil
  81. }