11package parser
22
3+ import (
4+ "strings"
5+ )
6+
37type media struct {
48 MediaGroups []mediaGroup `xml:"http://search.yahoo.com/mrss/ group"`
59 MediaContents []mediaContent `xml:"http://search.yahoo.com/mrss/ content"`
@@ -8,21 +12,26 @@ type media struct {
812}
913
1014type mediaGroup struct {
15+ MediaContent []mediaContent `xml:"http://search.yahoo.com/mrss/ content"`
1116 MediaThumbnails []mediaThumbnail `xml:"http://search.yahoo.com/mrss/ thumbnail"`
1217 MediaDescriptions []mediaDescription `xml:"http://search.yahoo.com/mrss/ description"`
1318}
1419
1520type mediaContent struct {
16- MediaThumbnails []mediaThumbnail `xml:"http://search.yahoo.com/mrss/ thumbnail"`
21+ MediaThumbnails []mediaThumbnail `xml:"http://search.yahoo.com/mrss/ thumbnail"`
22+ MediaType string `xml:"type,attr"`
23+ MediaMedium string `xml:"medium,attr"`
24+ MediaURL string `xml:"url,attr"`
25+ MediaDescription mediaDescription `xml:"http://search.yahoo.com/mrss/ description"`
1726}
1827
1928type mediaThumbnail struct {
2029 URL string `xml:"url,attr"`
2130}
2231
2332type mediaDescription struct {
24- Type string `xml:"type,attr"`
25- Description string `xml:",chardata"`
33+ Type string `xml:"type,attr"`
34+ Text string `xml:",chardata"`
2635}
2736
2837func (m * media ) firstMediaThumbnail () string {
@@ -44,12 +53,59 @@ func (m *media) firstMediaThumbnail() string {
4453
4554func (m * media ) firstMediaDescription () string {
4655 for _ , d := range m .MediaDescriptions {
47- return plain2html (d .Description )
56+ return plain2html (d .Text )
4857 }
4958 for _ , g := range m .MediaGroups {
5059 for _ , d := range g .MediaDescriptions {
51- return plain2html (d .Description )
60+ return plain2html (d .Text )
5261 }
5362 }
5463 return ""
5564}
65+
66+ func (m * media ) mediaLinks () []MediaLink {
67+ links := make ([]MediaLink , 0 )
68+ for _ , thumbnail := range m .MediaThumbnails {
69+ links = append (links , MediaLink {URL : thumbnail .URL , Type : "image" })
70+ }
71+ for _ , group := range m .MediaGroups {
72+ for _ , thumbnail := range group .MediaThumbnails {
73+ links = append (links , MediaLink {
74+ URL : thumbnail .URL ,
75+ Type : "image" ,
76+ })
77+ }
78+ }
79+ for _ , content := range m .MediaContents {
80+ if content .MediaURL != "" {
81+ url := content .MediaURL
82+ description := content .MediaDescription .Text
83+ if strings .HasPrefix (content .MediaType , "image/" ) {
84+ links = append (links , MediaLink {URL : url , Type : "image" , Description : description })
85+ } else if strings .HasPrefix (content .MediaType , "audio/" ) {
86+ links = append (links , MediaLink {URL : url , Type : "audio" , Description : description })
87+ } else if strings .HasPrefix (content .MediaType , "video/" ) {
88+ links = append (links , MediaLink {URL : url , Type : "video" , Description : description })
89+ } else if content .MediaMedium == "image" || content .MediaMedium == "audio" || content .MediaMedium == "video" {
90+ links = append (links , MediaLink {URL : url , Type : content .MediaMedium , Description : description })
91+ } else {
92+ if len (content .MediaThumbnails ) > 0 {
93+ links = append (links , MediaLink {
94+ URL : content .MediaThumbnails [0 ].URL ,
95+ Type : "image" ,
96+ })
97+ }
98+ }
99+ }
100+ for _ , thumbnail := range content .MediaThumbnails {
101+ links = append (links , MediaLink {
102+ URL : thumbnail .URL ,
103+ Type : "image" ,
104+ })
105+ }
106+ }
107+ if len (links ) == 0 {
108+ return nil
109+ }
110+ return links
111+ }
0 commit comments