1package gofeed
2
3import (
4	"io"
5	"strings"
6
7	"github.com/mmcdole/gofeed/internal/shared"
8	xpp "github.com/mmcdole/goxpp"
9)
10
11// FeedType represents one of the possible feed
12// types that we can detect.
13type FeedType int
14
15const (
16	// FeedTypeUnknown represents a feed that could not have its
17	// type determiend.
18	FeedTypeUnknown FeedType = iota
19	// FeedTypeAtom repesents an Atom feed
20	FeedTypeAtom
21	// FeedTypeRSS represents an RSS feed
22	FeedTypeRSS
23)
24
25// DetectFeedType attempts to determine the type of feed
26// by looking for specific xml elements unique to the
27// various feed types.
28func DetectFeedType(feed io.Reader) FeedType {
29	p := xpp.NewXMLPullParser(feed, false, shared.NewReaderLabel)
30
31	xmlBase := shared.XMLBase{}
32	_, err := xmlBase.FindRoot(p)
33	if err != nil {
34		return FeedTypeUnknown
35	}
36
37	name := strings.ToLower(p.Name)
38	switch name {
39	case "rdf":
40		return FeedTypeRSS
41	case "rss":
42		return FeedTypeRSS
43	case "feed":
44		return FeedTypeAtom
45	default:
46		return FeedTypeUnknown
47	}
48}
49