Skip to content

Commit

Permalink
Ability to parse custom fields in rss that are not part of the spec (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sjawaji authored Apr 20, 2021
1 parent 41f47c9 commit 5d81d4b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions rss/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type Item struct {
DublinCoreExt *ext.DublinCoreExtension `json:"dcExt,omitempty"`
ITunesExt *ext.ITunesItemExtension `json:"itunesExt,omitempty"`
Extensions ext.Extensions `json:"extensions,omitempty"`
Custom map[string]string `json:"custom,omitempty"`
}

// Image is an image that represents the feed
Expand Down
10 changes: 8 additions & 2 deletions rss/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,14 @@ func (rp *Parser) parseItem(p *xpp.XMLPullParser) (item *Item, err error) {
}
categories = append(categories, result)
} else {
// Skip any elements not part of the item spec
p.Skip()
result, err := shared.ParseText(p)
if err != nil {
continue
}
if item.Custom == nil {
item.Custom = make(map[string]string, 0)
}
item.Custom[name] = result
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions testdata/parser/rss/rss_channel_item_custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"items": [
{
"custom": {
"apcategory": "s",
"test": "test"
}
}
],
"version": "2.0"
}
11 changes: 11 additions & 0 deletions testdata/parser/rss/rss_channel_item_custom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!--
Description: rss item categories
-->
<rss version="2.0">
<channel>
<item>
<apcategory>s</apcategory>
<test>test</test>
</item>
</channel>
</rss>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"items": [
{
"custom": {
"apcategory": "s",
"test": "test"
}
}
],
"feedType": "rss",
"feedVersion": "2.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<rss version="2.0">
<channel>
<item>
<test>test</test>
<apcategory>s</apcategory>
</item>
</channel>
</rss>
1 change: 1 addition & 0 deletions translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (t *DefaultRSSTranslator) translateFeedItem(rssItem *rss.Item) (item *Item)
item.DublinCoreExt = rssItem.DublinCoreExt
item.ITunesExt = rssItem.ITunesExt
item.Extensions = rssItem.Extensions
item.Custom = rssItem.Custom
return
}

Expand Down

0 comments on commit 5d81d4b

Please sign in to comment.