2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 06:10:04 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package atom // import "miniflux.app/v2/internal/reader/atom"
|
2017-11-20 06:10:04 +01:00
|
|
|
|
|
|
|
import (
|
2019-12-23 07:18:21 +01:00
|
|
|
"encoding/xml"
|
2023-10-22 04:50:29 +02:00
|
|
|
"fmt"
|
2017-11-20 06:10:04 +01:00
|
|
|
"io"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/model"
|
|
|
|
xml_decoder "miniflux.app/v2/internal/reader/xml"
|
2017-11-20 06:10:04 +01:00
|
|
|
)
|
|
|
|
|
2019-12-23 07:18:21 +01:00
|
|
|
type atomFeed interface {
|
2020-12-03 05:47:11 +01:00
|
|
|
Transform(baseURL string) *model.Feed
|
2019-12-23 07:18:21 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:34:11 +01:00
|
|
|
// Parse returns a normalized feed struct from a Atom feed.
|
2024-03-04 17:07:43 +01:00
|
|
|
func Parse(baseURL string, r io.ReadSeeker) (*model.Feed, error) {
|
2019-12-23 07:18:21 +01:00
|
|
|
var rawFeed atomFeed
|
2024-03-04 17:07:43 +01:00
|
|
|
if getAtomFeedVersion(r) == "0.3" {
|
2019-12-23 07:18:21 +01:00
|
|
|
rawFeed = new(atom03Feed)
|
|
|
|
} else {
|
|
|
|
rawFeed = new(atom10Feed)
|
|
|
|
}
|
2024-03-04 17:07:43 +01:00
|
|
|
r.Seek(0, io.SeekStart)
|
2019-12-23 07:18:21 +01:00
|
|
|
|
2024-03-04 17:07:43 +01:00
|
|
|
if err := xml_decoder.NewXMLDecoder(r).Decode(rawFeed); err != nil {
|
2023-10-22 04:50:29 +02:00
|
|
|
return nil, fmt.Errorf("atom: unable to parse feed: %w", err)
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:47:11 +01:00
|
|
|
return rawFeed.Transform(baseURL), nil
|
2019-12-23 07:18:21 +01:00
|
|
|
}
|
|
|
|
|
2024-03-04 17:07:43 +01:00
|
|
|
func getAtomFeedVersion(data io.ReadSeeker) string {
|
2023-12-02 01:27:18 +01:00
|
|
|
decoder := xml_decoder.NewXMLDecoder(data)
|
2019-12-23 07:18:21 +01:00
|
|
|
for {
|
|
|
|
token, _ := decoder.Token()
|
|
|
|
if token == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if element, ok := token.(xml.StartElement); ok {
|
|
|
|
if element.Name.Local == "feed" {
|
|
|
|
for _, attr := range element.Attr {
|
|
|
|
if attr.Name.Local == "version" && attr.Value == "0.3" {
|
|
|
|
return "0.3"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "1.0"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "1.0"
|
2017-11-20 06:10:04 +01:00
|
|
|
}
|