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.
|
|
|
|
|
|
|
|
package opml
|
|
|
|
|
|
|
|
import "encoding/xml"
|
|
|
|
|
2017-11-20 22:11:06 -05:00
|
|
|
type opml struct {
|
2017-11-20 00:10:04 -05:00
|
|
|
XMLName xml.Name `xml:"opml"`
|
|
|
|
Version string `xml:"version,attr"`
|
2017-11-20 22:11:06 -05:00
|
|
|
Outlines []outline `xml:"body>outline"`
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 22:11:06 -05:00
|
|
|
type outline struct {
|
2017-11-20 00:10:04 -05:00
|
|
|
Title string `xml:"title,attr,omitempty"`
|
|
|
|
Text string `xml:"text,attr"`
|
|
|
|
FeedURL string `xml:"xmlUrl,attr,omitempty"`
|
|
|
|
SiteURL string `xml:"htmlUrl,attr,omitempty"`
|
2017-11-20 22:11:06 -05:00
|
|
|
Outlines []outline `xml:"outline,omitempty"`
|
2017-11-20 00:10:04 -05:00
|
|
|
}
|
|
|
|
|
2017-11-20 22:11:06 -05:00
|
|
|
func (o *outline) GetTitle() string {
|
2017-11-20 00:10:04 -05:00
|
|
|
if o.Title != "" {
|
|
|
|
return o.Title
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.Text != "" {
|
|
|
|
return o.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.SiteURL != "" {
|
|
|
|
return o.SiteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if o.FeedURL != "" {
|
|
|
|
return o.FeedURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:11:06 -05:00
|
|
|
func (o *outline) GetSiteURL() string {
|
2017-11-20 00:10:04 -05:00
|
|
|
if o.SiteURL != "" {
|
|
|
|
return o.SiteURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.FeedURL
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:11:06 -05:00
|
|
|
func (o *outline) IsCategory() bool {
|
2017-11-20 00:10:04 -05:00
|
|
|
return o.Text != "" && o.SiteURL == "" && o.FeedURL == ""
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:11:06 -05:00
|
|
|
func (o *outline) Append(subscriptions SubcriptionList, category string) SubcriptionList {
|
2017-11-20 00:10:04 -05:00
|
|
|
if o.FeedURL != "" {
|
|
|
|
subscriptions = append(subscriptions, &Subcription{
|
|
|
|
Title: o.GetTitle(),
|
|
|
|
FeedURL: o.FeedURL,
|
|
|
|
SiteURL: o.GetSiteURL(),
|
|
|
|
CategoryName: category,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return subscriptions
|
|
|
|
}
|
|
|
|
|
2017-11-20 22:11:06 -05:00
|
|
|
func (o *opml) Transform() SubcriptionList {
|
2017-11-20 00:10:04 -05:00
|
|
|
var subscriptions SubcriptionList
|
|
|
|
|
|
|
|
for _, outline := range o.Outlines {
|
|
|
|
if outline.IsCategory() {
|
|
|
|
for _, element := range outline.Outlines {
|
|
|
|
subscriptions = element.Append(subscriptions, outline.Text)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
subscriptions = outline.Append(subscriptions, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return subscriptions
|
|
|
|
}
|