1
0
Fork 0

Get the right comments URL when having multiple namespaces

This commit is contained in:
Frédéric Guillot 2018-04-09 20:30:55 -07:00
parent 7640a8cbab
commit f76093690c
2 changed files with 34 additions and 16 deletions

View file

@ -583,13 +583,16 @@ func TestParseEntryWithRelativeURL(t *testing.T) {
func TestParseEntryWithCommentsURL(t *testing.T) { func TestParseEntryWithCommentsURL(t *testing.T) {
data := `<?xml version="1.0" encoding="utf-8"?> data := `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"> <rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel> <channel>
<link>https://example.org/</link> <link>https://example.org/</link>
<item> <item>
<title>Item 1</title> <title>Item 1</title>
<link>https://example.org/item1</link> <link>https://example.org/item1</link>
<comments>https://example.org/comments</comments> <comments>
https://example.org/comments
</comments>
<slash:comments>42</slash:comments>
</item> </item>
</channel> </channel>
</rss>` </rss>`

View file

@ -38,20 +38,25 @@ type rssLink struct {
Rel string `xml:"rel,attr"` Rel string `xml:"rel,attr"`
} }
type rssCommentLink struct {
XMLName xml.Name
Data string `xml:",chardata"`
}
type rssItem struct { type rssItem struct {
GUID string `xml:"guid"` GUID string `xml:"guid"`
Title string `xml:"title"` Title string `xml:"title"`
Links []rssLink `xml:"link"` Links []rssLink `xml:"link"`
OriginalLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origLink"` OriginalLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origLink"`
Comments string `xml:"comments"` CommentLinks []rssCommentLink `xml:"comments"`
Description string `xml:"description"` Description string `xml:"description"`
Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"` Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"`
PubDate string `xml:"pubDate"` PubDate string `xml:"pubDate"`
Date string `xml:"http://purl.org/dc/elements/1.1/ date"` Date string `xml:"http://purl.org/dc/elements/1.1/ date"`
Authors []rssAuthor `xml:"author"` Authors []rssAuthor `xml:"author"`
Creator string `xml:"http://purl.org/dc/elements/1.1/ creator"` Creator string `xml:"http://purl.org/dc/elements/1.1/ creator"`
Enclosures []rssEnclosure `xml:"enclosure"` Enclosures []rssEnclosure `xml:"enclosure"`
OrigEnclosureLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origEnclosureLink"` OrigEnclosureLink string `xml:"http://rssnamespace.org/feedburner/ext/1.0 origEnclosureLink"`
} }
type rssAuthor struct { type rssAuthor struct {
@ -217,10 +222,20 @@ func (r *rssItem) GetEnclosures() model.EnclosureList {
return enclosures return enclosures
} }
func (r *rssItem) CommentsURL() string {
for _, commentLink := range r.CommentLinks {
if commentLink.XMLName.Space == "" {
return strings.TrimSpace(commentLink.Data)
}
}
return ""
}
func (r *rssItem) Transform() *model.Entry { func (r *rssItem) Transform() *model.Entry {
entry := new(model.Entry) entry := new(model.Entry)
entry.URL = r.GetURL() entry.URL = r.GetURL()
entry.CommentsURL = r.Comments entry.CommentsURL = r.CommentsURL()
entry.Date = r.GetDate() entry.Date = r.GetDate()
entry.Author = r.GetAuthor() entry.Author = r.GetAuthor()
entry.Hash = r.GetHash() entry.Hash = r.GetHash()