diff options
author | Frédéric Guillot <fred@miniflux.net> | 2017-11-19 21:10:04 -0800 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2017-11-19 22:01:46 -0800 |
commit | 8ffb773f43c8dc54801ca1d111854e7e881c93c9 (patch) | |
tree | 38133a2fc612597a75fed1d13e5b4042f58a2b7e /reader/opml/opml.go |
First commit
Diffstat (limited to 'reader/opml/opml.go')
-rw-r--r-- | reader/opml/opml.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/reader/opml/opml.go b/reader/opml/opml.go new file mode 100644 index 0000000..d5278a7 --- /dev/null +++ b/reader/opml/opml.go @@ -0,0 +1,82 @@ +// 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" + +type Opml struct { + XMLName xml.Name `xml:"opml"` + Version string `xml:"version,attr"` + Outlines []Outline `xml:"body>outline"` +} + +type Outline struct { + Title string `xml:"title,attr,omitempty"` + Text string `xml:"text,attr"` + FeedURL string `xml:"xmlUrl,attr,omitempty"` + SiteURL string `xml:"htmlUrl,attr,omitempty"` + Outlines []Outline `xml:"outline,omitempty"` +} + +func (o *Outline) GetTitle() string { + 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 "" +} + +func (o *Outline) GetSiteURL() string { + if o.SiteURL != "" { + return o.SiteURL + } + + return o.FeedURL +} + +func (o *Outline) IsCategory() bool { + return o.Text != "" && o.SiteURL == "" && o.FeedURL == "" +} + +func (o *Outline) Append(subscriptions SubcriptionList, category string) SubcriptionList { + if o.FeedURL != "" { + subscriptions = append(subscriptions, &Subcription{ + Title: o.GetTitle(), + FeedURL: o.FeedURL, + SiteURL: o.GetSiteURL(), + CategoryName: category, + }) + } + + return subscriptions +} + +func (o *Opml) Transform() SubcriptionList { + 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 +} |