From 7dbfecdba15e0f7a3693fa7bd081dbfc2ed400d0 Mon Sep 17 00:00:00 2001 From: Nazar Kanaev Date: Fri, 26 Mar 2021 18:59:58 +0000 Subject: [PATCH] extract thumbnails from vimeo feeds --- src/parser/media.go | 13 +++++++++++-- src/parser/rss.go | 2 ++ src/parser/rss_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/parser/media.go b/src/parser/media.go index b8e9712..b926d1e 100644 --- a/src/parser/media.go +++ b/src/parser/media.go @@ -1,8 +1,8 @@ package parser type media struct { - MediaGroups []mediaGroup `xml:"http://search.yahoo.com/mrss/ group"` - + MediaGroups []mediaGroup `xml:"http://search.yahoo.com/mrss/ group"` + MediaContents []mediaContent `xml:"http://search.yahoo.com/mrss/ content"` MediaThumbnails []mediaThumbnail `xml:"http://search.yahoo.com/mrss/ thumbnail"` MediaDescriptions []mediaDescription `xml:"http://search.yahoo.com/mrss/ description"` } @@ -12,6 +12,10 @@ type mediaGroup struct { MediaDescriptions []mediaDescription `xml:"http://search.yahoo.com/mrss/ description"` } +type mediaContent struct { + MediaThumbnails []mediaThumbnail `xml:"http://search.yahoo.com/mrss/ thumbnail"` +} + type mediaThumbnail struct { URL string `xml:"url,attr"` } @@ -22,6 +26,11 @@ type mediaDescription struct { } func (m *media) firstMediaThumbnail() string { + for _, c := range m.MediaContents { + for _, t := range c.MediaThumbnails { + return t.URL + } + } for _, t := range m.MediaThumbnails { return t.URL } diff --git a/src/parser/rss.go b/src/parser/rss.go index 0a275ee..ac771f6 100644 --- a/src/parser/rss.go +++ b/src/parser/rss.go @@ -34,6 +34,7 @@ type rssItem struct { ItunesSubtitle string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd subtitle"` ItunesSummary string `xml:"http://www.itunes.com/dtds/podcast-1.0.dtd summary"` GoogleDescription string `xml:"http://www.google.com/schemas/play-podcasts/1.0 description"` + media } type rssLink struct { @@ -84,6 +85,7 @@ func ParseRSS(r io.Reader) (*Feed, error) { Title: srcitem.Title, Content: firstNonEmpty(srcitem.ContentEncoded, srcitem.Description), AudioURL: podcastURL, + ImageURL: srcitem.firstMediaThumbnail(), }) } return dstfeed, nil diff --git a/src/parser/rss_test.go b/src/parser/rss_test.go index 89896ca..e9e6553 100644 --- a/src/parser/rss_test.go +++ b/src/parser/rss_test.go @@ -54,3 +54,32 @@ func TestRSSFeed(t *testing.T) { t.Fatal("invalid rss") } } + +func TestRSSMediaContentThumbnail(t *testing.T) { + // see: https://vimeo.com/channels/staffpicks/videos/rss + feed, _ := Parse(strings.NewReader(` + + + + + + + + + + + + + + + `)) + have := feed.Items[0].ImageURL + want := "https://i.vimeocdn.com/video/1092705247_960.jpg" + if have != want { + t.Logf("want: %#v", want) + t.Logf("have: %#v", have) + t.FailNow() + } +}