43 lines
817 B
Go
43 lines
817 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/xml"
|
||
|
)
|
||
|
|
||
|
type Document struct {
|
||
|
Channel Channel `xml:"channel"`
|
||
|
}
|
||
|
|
||
|
type Item struct {
|
||
|
Title string `xml:"title"`
|
||
|
Link string `xml:"link"`
|
||
|
Description string `xml:"description"`
|
||
|
PublishedDate string `xml:"pubDate"`
|
||
|
}
|
||
|
|
||
|
type Channel struct {
|
||
|
Title string `xml:"title"`
|
||
|
Description string `xml:"description"`
|
||
|
Items []Item `xml:"item"`
|
||
|
}
|
||
|
|
||
|
func (c Channel) ItemsToLinks() []Link {
|
||
|
formattedItems := []Link{}
|
||
|
|
||
|
for _, feedItem := range c.Items {
|
||
|
formattedItems = append(formattedItems, Link{Url: feedItem.Link, Title: feedItem.Title, PublishedDate: feedItem.PublishedDate})
|
||
|
}
|
||
|
|
||
|
return formattedItems
|
||
|
}
|
||
|
|
||
|
func ParseFeed(raw []byte) Document {
|
||
|
var doc Document
|
||
|
|
||
|
if err := xml.Unmarshal(raw, &doc); err != nil {
|
||
|
return Document{}
|
||
|
}
|
||
|
|
||
|
return doc
|
||
|
}
|