From 9b88c0b56b9e25c3c521642e5c6df82fe7b21b34 Mon Sep 17 00:00:00 2001 From: zeGrandpa Date: Thu, 19 Sep 2019 11:07:16 +0200 Subject: [PATCH] Add the ability to acces the raw feed data --- feed.go | 2 ++ parser.go | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/feed.go b/feed.go index 9ee1eaa8..c5d6125d 100644 --- a/feed.go +++ b/feed.go @@ -1,6 +1,7 @@ package gofeed import ( + "bytes" "encoding/json" "time" @@ -11,6 +12,7 @@ import ( // and rss.Feed gets translated to. It represents // a web feed. type Feed struct { + RawFeed bytes.Buffer `json:"-"` Title string `json:"title,omitempty"` Description string `json:"description,omitempty"` Link string `json:"link,omitempty"` diff --git a/parser.go b/parser.go index 9a71239f..f4f31dde 100644 --- a/parser.go +++ b/parser.go @@ -55,19 +55,27 @@ func (f *Parser) Parse(feed io.Reader) (*Feed, error) { // DetectFeedType function and construct a new // reader with those bytes intact for when we // attempt to parse the feeds. - var buf bytes.Buffer + var buf, copyBuffer bytes.Buffer tee := io.TeeReader(feed, &buf) feedType := DetectFeedType(tee) // Glue the read bytes from the detect function // back into a new reader - r := io.MultiReader(&buf, feed) + r := io.TeeReader(io.MultiReader(&buf, feed), ©Buffer) switch feedType { case FeedTypeAtom: - return f.parseAtomFeed(r) + atomFeed, err := f.parseAtomFeed(r) + if atomFeed != nil { + atomFeed.RawFeed = copyBuffer + } + return atomFeed, err case FeedTypeRSS: - return f.parseRSSFeed(r) + rssFeed, err := f.parseRSSFeed(r) + if rssFeed != nil { + rssFeed.RawFeed = copyBuffer + } + return rssFeed, err } return nil, ErrFeedTypeNotDetected