detector.go 961 B

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