2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-10-14 14:46:41 -04:00
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
package parser // import "miniflux.app/v2/internal/reader/parser"
|
2018-10-14 14:46:41 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-10 22:46:45 -04:00
|
|
|
rxml "miniflux.app/v2/internal/reader/xml"
|
2018-10-14 14:46:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// List of feed formats.
|
|
|
|
const (
|
|
|
|
FormatRDF = "rdf"
|
|
|
|
FormatRSS = "rss"
|
|
|
|
FormatAtom = "atom"
|
|
|
|
FormatJSON = "json"
|
|
|
|
FormatUnknown = "unknown"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DetectFeedFormat tries to guess the feed format from input data.
|
|
|
|
func DetectFeedFormat(data string) string {
|
|
|
|
if strings.HasPrefix(strings.TrimSpace(data), "{") {
|
|
|
|
return FormatJSON
|
|
|
|
}
|
|
|
|
|
2020-01-03 01:19:15 -05:00
|
|
|
decoder := rxml.NewDecoder(strings.NewReader(data))
|
2018-10-14 14:46:41 -04:00
|
|
|
|
|
|
|
for {
|
|
|
|
token, _ := decoder.Token()
|
|
|
|
if token == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if element, ok := token.(xml.StartElement); ok {
|
|
|
|
switch element.Name.Local {
|
|
|
|
case "rss":
|
|
|
|
return FormatRSS
|
|
|
|
case "feed":
|
|
|
|
return FormatAtom
|
|
|
|
case "RDF":
|
|
|
|
return FormatRDF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FormatUnknown
|
|
|
|
}
|