Add support for RSS <media:category> element

This commit is contained in:
Frédéric Guillot 2024-03-13 21:26:39 -07:00
parent f4746a7306
commit 5948786b15
3 changed files with 60 additions and 5 deletions

View file

@ -13,11 +13,12 @@ var textLinkRegex = regexp.MustCompile(`(?mi)(\bhttps?:\/\/[-A-Z0-9+&@#\/%?=~_|!
// Specs: https://www.rssboard.org/media-rss
type MediaItemElement struct {
MediaGroups []Group `xml:"http://search.yahoo.com/mrss/ group"`
MediaContents []Content `xml:"http://search.yahoo.com/mrss/ content"`
MediaThumbnails []Thumbnail `xml:"http://search.yahoo.com/mrss/ thumbnail"`
MediaDescriptions DescriptionList `xml:"http://search.yahoo.com/mrss/ description"`
MediaPeerLinks []PeerLink `xml:"http://search.yahoo.com/mrss/ peerLink"`
MediaCategories MediaCategoryList `xml:"http://search.yahoo.com/mrss/ category"`
MediaGroups []Group `xml:"http://search.yahoo.com/mrss/ group"`
MediaContents []Content `xml:"http://search.yahoo.com/mrss/ content"`
MediaThumbnails []Thumbnail `xml:"http://search.yahoo.com/mrss/ thumbnail"`
MediaDescriptions DescriptionList `xml:"http://search.yahoo.com/mrss/ description"`
MediaPeerLinks []PeerLink `xml:"http://search.yahoo.com/mrss/ peerLink"`
}
// AllMediaThumbnails returns all thumbnail elements merged together.
@ -173,3 +174,20 @@ func (dl DescriptionList) First() string {
}
return ""
}
type MediaCategoryList []MediaCategory
func (mcl MediaCategoryList) Labels() []string {
var labels []string
for _, category := range mcl {
label := strings.TrimSpace(category.Label)
if label != "" {
labels = append(labels, label)
}
}
return labels
}
type MediaCategory struct {
Label string `xml:"label,attr"`
}

View file

@ -122,6 +122,7 @@ func (r *RSSAdapter) BuildFeed(feedURL string) *model.Feed {
// Populate entry categories.
entry.Tags = append(entry.Tags, item.Categories...)
entry.Tags = append(entry.Tags, item.MediaCategories.Labels()...)
entry.Tags = append(entry.Tags, r.rss.Channel.Categories...)
entry.Tags = append(entry.Tags, r.rss.Channel.GetItunesCategories()...)

View file

@ -1681,6 +1681,42 @@ func TestParseFeedWithGooglePlayCategory(t *testing.T) {
}
}
func TestParseEntryWithMediaCategories(t *testing.T) {
data := `<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>Example</title>
<link>https://example.org/</link>
<item>
<title>Test</title>
<link>https://example.org/item</link>
<media:category label="Visual Art">visual_art</media:category>
<media:category scheme="http://search.yahoo.com/mrss/category_ schema">music/artist/album/song</media:category>
<media:category scheme="urn:flickr:tags">ycantpark mobile</media:category>
<media:category scheme="http://dmoz.org" label="Ace Ventura - Pet Detective">Arts/Movies/Titles/A/Ace_Ventura_Series/Ace_Ventura_ -_Pet_Detective</media:category>
</item>
</channel>
</rss>`
feed, err := Parse("https://example.org/", bytes.NewReader([]byte(data)))
if err != nil {
t.Fatal(err)
}
if len(feed.Entries[0].Tags) != 2 {
t.Errorf("Incorrect number of tags, got: %d", len(feed.Entries[0].Tags))
}
expected := []string{"Visual Art", "Ace Ventura - Pet Detective"}
result := feed.Entries[0].Tags
for i, tag := range result {
if tag != expected[i] {
t.Errorf("Incorrect tag, got: %q", tag)
}
}
}
func TestParseFeedWithTTLField(t *testing.T) {
data := `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">