2017-11-20 00:10:04 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package atom // import "miniflux.app/reader/atom"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
2019-12-23 01:18:21 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
2017-11-20 00:10:04 -05:00
|
|
|
"io"
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/errors"
|
|
|
|
"miniflux.app/model"
|
2019-12-23 01:18:21 -05:00
|
|
|
xml_decoder "miniflux.app/reader/xml"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2019-12-23 01:18:21 -05:00
|
|
|
type atomFeed interface {
|
2020-12-02 23:47:11 -05:00
|
|
|
Transform(baseURL string) *model.Feed
|
2019-12-23 01:18:21 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:34:11 -05:00
|
|
|
// Parse returns a normalized feed struct from a Atom feed.
|
2020-12-02 23:47:11 -05:00
|
|
|
func Parse(baseURL string, r io.Reader) (*model.Feed, *errors.LocalizedError) {
|
2019-12-23 01:18:21 -05:00
|
|
|
var buf bytes.Buffer
|
|
|
|
tee := io.TeeReader(r, &buf)
|
|
|
|
|
|
|
|
var rawFeed atomFeed
|
|
|
|
if getAtomFeedVersion(tee) == "0.3" {
|
|
|
|
rawFeed = new(atom03Feed)
|
|
|
|
} else {
|
|
|
|
rawFeed = new(atom10Feed)
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := xml_decoder.NewDecoder(&buf)
|
|
|
|
err := decoder.Decode(rawFeed)
|
2017-11-20 00:10:04 -05:00
|
|
|
if err != nil {
|
2018-02-28 00:19:59 -05:00
|
|
|
return nil, errors.NewLocalizedError("Unable to parse Atom feed: %q", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2020-12-02 23:47:11 -05:00
|
|
|
return rawFeed.Transform(baseURL), nil
|
2019-12-23 01:18:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAtomFeedVersion(data io.Reader) string {
|
|
|
|
decoder := xml_decoder.NewDecoder(data)
|
|
|
|
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 00:10:04 -05:00
|
|
|
}
|