Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrFeedTypeNotDetected = errors.New("failed to detect feed type")
ErrFeedTypeNotDetected is returned when the detection system can not figure out the Feed format
var ErrResponseTooLarge = errors.New("gofeed: response body exceeds MaxByteSize")
ErrResponseTooLarge is returned when a response body exceeds Parser.MaxByteSize.
Functions ¶
This section is empty.
Types ¶
type Auth ¶ added in v1.2.0
Auth is a structure allowing to use the BasicAuth during the HTTP request It must be instantiated with your new Parser
type DefaultAtomTranslator ¶
type DefaultAtomTranslator struct{}
DefaultAtomTranslator converts an atom.Feed struct into the generic Feed struct.
This default implementation defines a set of mapping rules between atom.Feed -> Feed for each of the fields in Feed.
func (*DefaultAtomTranslator) Translate ¶
func (t *DefaultAtomTranslator) Translate(feed interface{}) (*Feed, error)
Translate converts an Atom feed into the universal feed type.
type DefaultJSONTranslator ¶ added in v1.1.0
type DefaultJSONTranslator struct{}
DefaultJSONTranslator converts an json.Feed struct into the generic Feed struct.
This default implementation defines a set of mapping rules between json.Feed -> Feed for each of the fields in Feed.
func (*DefaultJSONTranslator) Translate ¶ added in v1.1.0
func (t *DefaultJSONTranslator) Translate(feed interface{}) (*Feed, error)
Translate converts an JSON feed into the universal feed type.
type DefaultRSSTranslator ¶
type DefaultRSSTranslator struct {
// DisableContentImageScan turns off the fallback that parses item and
// feed HTML (content, description) to find a first <img> when no
// explicit image is present. The scan runs a full HTML parse per item,
// so large feeds may want it off. Default off (the scan runs), matching
// historical behavior.
DisableContentImageScan bool
}
DefaultRSSTranslator converts an rss.Feed struct into the generic Feed struct.
This default implementation defines a set of mapping rules between rss.Feed -> Feed for each of the fields in Feed.
func (*DefaultRSSTranslator) Translate ¶
func (t *DefaultRSSTranslator) Translate(feed interface{}) (*Feed, error)
Translate converts an RSS feed into the universal feed type.
type Enclosure ¶
type Enclosure struct {
URL string `json:"url,omitempty"`
Length string `json:"length,omitempty"`
Type string `json:"type,omitempty"`
}
Enclosure is a file associated with a given Item.
type Feed ¶
type Feed struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Link string `json:"link,omitempty"`
FeedLink string `json:"feedLink,omitempty"`
Links []string `json:"links,omitempty"`
Updated string `json:"updated,omitempty"`
UpdatedParsed *time.Time `json:"updatedParsed,omitempty"`
Published string `json:"published,omitempty"`
PublishedParsed *time.Time `json:"publishedParsed,omitempty"`
Author *Person `json:"author,omitempty"` // Deprecated: Use feed.Authors instead
Authors []*Person `json:"authors,omitempty"`
Language string `json:"language,omitempty"`
Image *Image `json:"image,omitempty"`
Copyright string `json:"copyright,omitempty"`
Generator string `json:"generator,omitempty"`
Categories []string `json:"categories,omitempty"`
DublinCoreExt *ext.DublinCoreExtension `json:"dcExt,omitempty"`
ITunesExt *ext.ITunesFeedExtension `json:"itunesExt,omitempty"`
Extensions ext.Extensions `json:"extensions,omitempty"`
Custom map[string]string `json:"custom,omitempty"`
Items []*Item `json:"items"`
FeedType string `json:"feedType"`
FeedVersion string `json:"feedVersion"`
// contains filtered or unexported fields
}
Feed is the universal Feed type that atom.Feed and rss.Feed gets translated to. It represents a web feed. Sorting with sort.Sort will order the Items by oldest to newest publish time.
func (Feed) Less ¶
Less compares PublishedParsed of Items[i], Items[k] and returns true if Items[i] is less than Items[k].
func (Feed) OriginalFeed ¶ added in v1.4.0
func (f Feed) OriginalFeed() interface{}
OriginalFeed returns the source feed object (*rss.Feed, *atom.Feed, or *json.Feed) this Feed was translated from, giving access to format-specific fields not present on the universal Feed. It is only populated when the parser has KeepOriginalFeed set, and returns nil otherwise. The original is held in memory, not serialized, so it does not survive a Feed that has been marshaled and unmarshaled.
type FeedType ¶
type FeedType int
FeedType represents one of the possible feed types that we can detect.
func DetectFeedType ¶
DetectFeedType attempts to determine the type of feed by looking for specific xml elements unique to the various feed types. It returns FeedTypeUnknown when the reader fails before the type can be determined.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/mmcdole/gofeed"
)
func main() {
feedData := `<rss version="2.0">
<channel>
<title>Sample Feed</title>
</channel>
</rss>`
feedType := gofeed.DetectFeedType(strings.NewReader(feedData))
if feedType == gofeed.FeedTypeRSS {
fmt.Println("Wow! This is an RSS feed!")
}
}
Output:
type Item ¶
type Item struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Content string `json:"content,omitempty"`
Link string `json:"link,omitempty"`
Links []string `json:"links,omitempty"`
Updated string `json:"updated,omitempty"`
UpdatedParsed *time.Time `json:"updatedParsed,omitempty"`
Published string `json:"published,omitempty"`
PublishedParsed *time.Time `json:"publishedParsed,omitempty"`
Author *Person `json:"author,omitempty"` // Deprecated: Use item.Authors instead
Authors []*Person `json:"authors,omitempty"`
GUID string `json:"guid,omitempty"`
Image *Image `json:"image,omitempty"`
Categories []string `json:"categories,omitempty"`
Enclosures []*Enclosure `json:"enclosures,omitempty"`
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"`
}
Item is the universal Item type that atom.Entry and rss.Item gets translated to. It represents a single entry in a given feed.
type Parser ¶
type Parser struct {
AtomTranslator Translator
RSSTranslator Translator
JSONTranslator Translator
UserAgent string
AuthConfig *Auth
Client *http.Client
// MaxByteSize limits how many bytes ParseURL/ParseURLWithContext will read
// from a response body. Zero means no limit. Exceeding it returns
// ErrResponseTooLarge rather than silently truncating.
MaxByteSize int64
// KeepOriginalFeed retains the source rss/atom/json feed on the result,
// accessible via Feed.OriginalFeed(). Off by default: keeping it holds a
// second copy of the feed in memory for the lifetime of the result.
KeepOriginalFeed bool
// contains filtered or unexported fields
}
Parser is a universal feed parser that detects a given feed type, parsers it, and translates it to the universal feed type.
func (*Parser) Parse ¶
Parse parses a RSS or Atom or JSON feed into the universal gofeed.Feed. It takes an io.Reader which should return the xml/json content.
Only the first few KB are buffered to detect the feed type; RSS and Atom content is then parsed incrementally from the reader. JSON feeds are read fully into memory, as JSON decoding needs the complete document.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/mmcdole/gofeed"
)
func main() {
feedData := `<rss version="2.0">
<channel>
<title>Sample Feed</title>
</channel>
</rss>`
fp := gofeed.NewParser()
feed, err := fp.Parse(strings.NewReader(feedData))
if err != nil {
panic(err)
}
fmt.Println(feed.Title)
}
Output:
func (*Parser) ParseString ¶
ParseString parses a feed XML string and into the universal feed type.
Example ¶
package main
import (
"fmt"
"github.com/mmcdole/gofeed"
)
func main() {
feedData := `<rss version="2.0">
<channel>
<title>Sample Feed</title>
</channel>
</rss>`
fp := gofeed.NewParser()
feed, err := fp.ParseString(feedData)
if err != nil {
panic(err)
}
fmt.Println(feed.Title)
}
Output:
func (*Parser) ParseURL ¶
ParseURL fetches the contents of a given url and attempts to parse the response into the universal feed type. It applies a default request timeout; use ParseURLWithContext to control cancellation.
Example ¶
package main
import (
"fmt"
"github.com/mmcdole/gofeed"
)
func main() {
fp := gofeed.NewParser()
feed, err := fp.ParseURL("http://feeds.twit.tv/twit.xml")
if err != nil {
panic(err)
}
fmt.Println(feed.Title)
}
Output:
Example (BasicAuth) ¶
package main
import (
"fmt"
"github.com/mmcdole/gofeed"
)
func main() {
fp := gofeed.NewParser()
fp.AuthConfig = &gofeed.Auth{
Username: "foo",
Password: "bar",
}
feed, err := fp.ParseURL("http://feeds.twit.tv/twit.xml")
if err != nil {
panic(err)
}
fmt.Println(feed.Title)
}
Output:
func (*Parser) ParseURLWithContext ¶
ParseURLWithContext fetches contents of a given url and attempts to parse the response into the universal feed type. You can instantiate the Auth structure with your Username and Password to use the BasicAuth during the HTTP call. It will be automatically added to the header of the request Request could be canceled or timeout via given context
type Translator ¶
Translator converts a particular feed (atom.Feed or rss.Feed of json.Feed) into the generic Feed struct