format.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package parser // import "miniflux.app/reader/parser"
  5. import (
  6. "encoding/xml"
  7. "strings"
  8. "miniflux.app/reader/encoding"
  9. )
  10. // List of feed formats.
  11. const (
  12. FormatRDF = "rdf"
  13. FormatRSS = "rss"
  14. FormatAtom = "atom"
  15. FormatJSON = "json"
  16. FormatUnknown = "unknown"
  17. )
  18. // DetectFeedFormat tries to guess the feed format from input data.
  19. func DetectFeedFormat(data string) string {
  20. if strings.HasPrefix(strings.TrimSpace(data), "{") {
  21. return FormatJSON
  22. }
  23. decoder := xml.NewDecoder(strings.NewReader(data))
  24. decoder.Entity = xml.HTMLEntity
  25. decoder.CharsetReader = encoding.CharsetReader
  26. for {
  27. token, _ := decoder.Token()
  28. if token == nil {
  29. break
  30. }
  31. if element, ok := token.(xml.StartElement); ok {
  32. switch element.Name.Local {
  33. case "rss":
  34. return FormatRSS
  35. case "feed":
  36. return FormatAtom
  37. case "RDF":
  38. return FormatRDF
  39. }
  40. }
  41. }
  42. return FormatUnknown
  43. }