1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-07-22 17:18:37 +00:00

When detecting the format, detect its version as well

There is no need to detect the format and then the version when both can be
done at the same time.

Add a benchmark as well, on large and small atom and rss files.
This commit is contained in:
jvoisin 2024-03-12 13:11:56 +01:00 committed by Frédéric Guillot
parent 688b73b7ae
commit 45d486b919
12 changed files with 3608 additions and 160 deletions

View file

@ -21,12 +21,12 @@ const (
)
// DetectFeedFormat tries to guess the feed format from input data.
func DetectFeedFormat(r io.ReadSeeker) string {
func DetectFeedFormat(r io.ReadSeeker) (string, string) {
data := make([]byte, 512)
r.Read(data)
if bytes.HasPrefix(bytes.TrimSpace(data), []byte("{")) {
return FormatJSON
return FormatJSON, ""
}
r.Seek(0, io.SeekStart)
@ -41,14 +41,19 @@ func DetectFeedFormat(r io.ReadSeeker) string {
if element, ok := token.(xml.StartElement); ok {
switch element.Name.Local {
case "rss":
return FormatRSS
return FormatRSS, ""
case "feed":
return FormatAtom
for _, attr := range element.Attr {
if attr.Name.Local == "version" && attr.Value == "0.3" {
return FormatAtom, "0.3"
}
}
return FormatAtom, "1.0"
case "RDF":
return FormatRDF
return FormatRDF, ""
}
}
}
return FormatUnknown
return FormatUnknown, ""
}