1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00
miniflux-v2/internal/reader/parser/parser.go
jvoisin c4a2afb7b7 perf: cache the format of feeds
Detecting the format of a feed accounts for up to 30% of the time spent in
`parser.ParseFeed`. Cache the value once detected, as it'll never change.
2025-03-25 21:23:30 +01:00

44 lines
1.3 KiB
Go

// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package parser // import "miniflux.app/v2/internal/reader/parser"
import (
"errors"
"io"
"miniflux.app/v2/internal/model"
"miniflux.app/v2/internal/reader/atom"
"miniflux.app/v2/internal/reader/json"
"miniflux.app/v2/internal/reader/rdf"
"miniflux.app/v2/internal/reader/rss"
)
var ErrFeedFormatNotDetected = errors.New("parser: unable to detect feed format")
// ParseFeedWithFormat returns a normalized feed object.
func ParseFeedWithFormat(baseURL string, r io.ReadSeeker, format, version string) (*model.Feed, error) {
switch format {
case FormatAtom:
r.Seek(0, io.SeekStart)
return atom.Parse(baseURL, r, version)
case FormatRSS:
r.Seek(0, io.SeekStart)
return rss.Parse(baseURL, r)
case FormatJSON:
r.Seek(0, io.SeekStart)
return json.Parse(baseURL, r)
case FormatRDF:
r.Seek(0, io.SeekStart)
return rdf.Parse(baseURL, r)
default:
return nil, ErrFeedFormatNotDetected
}
}
// ParseFeed analyzes the input data and returns a normalized feed object.
func ParseFeed(baseURL string, r io.ReadSeeker) (*model.Feed, error) {
r.Seek(0, io.SeekStart)
format, version := DetectFeedFormat(r)
return ParseFeedWithFormat(baseURL, r, format, version)
}