aboutsummaryrefslogtreecommitdiffhomepage
path: root/reader/opml
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2017-11-19 21:10:04 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net>2017-11-19 22:01:46 -0800
commit8ffb773f43c8dc54801ca1d111854e7e881c93c9 (patch)
tree38133a2fc612597a75fed1d13e5b4042f58a2b7e /reader/opml
First commit
Diffstat (limited to 'reader/opml')
-rw-r--r--reader/opml/handler.go94
-rw-r--r--reader/opml/opml.go82
-rw-r--r--reader/opml/parser.go26
-rw-r--r--reader/opml/parser_test.go138
-rw-r--r--reader/opml/serializer.go58
-rw-r--r--reader/opml/serializer_test.go31
-rw-r--r--reader/opml/subscription.go18
7 files changed, 447 insertions, 0 deletions
diff --git a/reader/opml/handler.go b/reader/opml/handler.go
new file mode 100644
index 0000000..6150d91
--- /dev/null
+++ b/reader/opml/handler.go
@@ -0,0 +1,94 @@
+// 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 (
+ "errors"
+ "fmt"
+ "github.com/miniflux/miniflux2/model"
+ "github.com/miniflux/miniflux2/storage"
+ "io"
+ "log"
+)
+
+type OpmlHandler struct {
+ store *storage.Storage
+}
+
+func (o *OpmlHandler) Export(userID int64) (string, error) {
+ feeds, err := o.store.GetFeeds(userID)
+ if err != nil {
+ log.Println(err)
+ return "", errors.New("Unable to fetch feeds.")
+ }
+
+ var subscriptions SubcriptionList
+ for _, feed := range feeds {
+ subscriptions = append(subscriptions, &Subcription{
+ Title: feed.Title,
+ FeedURL: feed.FeedURL,
+ SiteURL: feed.SiteURL,
+ CategoryName: feed.Category.Title,
+ })
+ }
+
+ return Serialize(subscriptions), nil
+}
+
+func (o *OpmlHandler) Import(userID int64, data io.Reader) (err error) {
+ subscriptions, err := Parse(data)
+ if err != nil {
+ return err
+ }
+
+ for _, subscription := range subscriptions {
+ if !o.store.FeedURLExists(userID, subscription.FeedURL) {
+ var category *model.Category
+
+ if subscription.CategoryName == "" {
+ category, err = o.store.GetFirstCategory(userID)
+ if err != nil {
+ log.Println(err)
+ return errors.New("Unable to find first category.")
+ }
+ } else {
+ category, err = o.store.GetCategoryByTitle(userID, subscription.CategoryName)
+ if err != nil {
+ log.Println(err)
+ return errors.New("Unable to search category by title.")
+ }
+
+ if category == nil {
+ category = &model.Category{
+ UserID: userID,
+ Title: subscription.CategoryName,
+ }
+
+ err := o.store.CreateCategory(category)
+ if err != nil {
+ log.Println(err)
+ return fmt.Errorf(`Unable to create this category: "%s".`, subscription.CategoryName)
+ }
+ }
+ }
+
+ feed := &model.Feed{
+ UserID: userID,
+ Title: subscription.Title,
+ FeedURL: subscription.FeedURL,
+ SiteURL: subscription.SiteURL,
+ Category: category,
+ }
+
+ o.store.CreateFeed(feed)
+ }
+ }
+
+ return nil
+}
+
+func NewOpmlHandler(store *storage.Storage) *OpmlHandler {
+ return &OpmlHandler{store: store}
+}
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
+}
diff --git a/reader/opml/parser.go b/reader/opml/parser.go
new file mode 100644
index 0000000..5d8babd
--- /dev/null
+++ b/reader/opml/parser.go
@@ -0,0 +1,26 @@
+// 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"
+ "fmt"
+ "io"
+
+ "golang.org/x/net/html/charset"
+)
+
+func Parse(data io.Reader) (SubcriptionList, error) {
+ opml := new(Opml)
+ decoder := xml.NewDecoder(data)
+ decoder.CharsetReader = charset.NewReaderLabel
+
+ err := decoder.Decode(opml)
+ if err != nil {
+ return nil, fmt.Errorf("Unable to parse OPML file: %v\n", err)
+ }
+
+ return opml.Transform(), nil
+}
diff --git a/reader/opml/parser_test.go b/reader/opml/parser_test.go
new file mode 100644
index 0000000..02543df
--- /dev/null
+++ b/reader/opml/parser_test.go
@@ -0,0 +1,138 @@
+// 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 "testing"
+import "bytes"
+
+func TestParseOpmlWithoutCategories(t *testing.T) {
+ data := `<?xml version="1.0" encoding="ISO-8859-1"?>
+ <opml version="2.0">
+ <head>
+ <title>mySubscriptions.opml</title>
+ </head>
+ <body>
+ <outline text="CNET News.com" description="Tech news and business reports by CNET News.com. Focused on information technology, core topics include computers, hardware, software, networking, and Internet media." htmlUrl="http://news.com.com/" language="unknown" title="CNET News.com" type="rss" version="RSS2" xmlUrl="http://news.com.com/2547-1_3-0-5.xml"/>
+ <outline text="washingtonpost.com - Politics" description="Politics" htmlUrl="http://www.washingtonpost.com/wp-dyn/politics?nav=rss_politics" language="unknown" title="washingtonpost.com - Politics" type="rss" version="RSS2" xmlUrl="http://www.washingtonpost.com/wp-srv/politics/rssheadlines.xml"/>
+ <outline text="Scobleizer: Microsoft Geek Blogger" description="Robert Scoble's look at geek and Microsoft life." htmlUrl="http://radio.weblogs.com/0001011/" language="unknown" title="Scobleizer: Microsoft Geek Blogger" type="rss" version="RSS2" xmlUrl="http://radio.weblogs.com/0001011/rss.xml"/>
+ <outline text="Yahoo! News: Technology" description="Technology" htmlUrl="http://news.yahoo.com/news?tmpl=index&amp;cid=738" language="unknown" title="Yahoo! News: Technology" type="rss" version="RSS2" xmlUrl="http://rss.news.yahoo.com/rss/tech"/>
+ <outline text="Workbench" description="Programming and publishing news and comment" htmlUrl="http://www.cadenhead.org/workbench/" language="unknown" title="Workbench" type="rss" version="RSS2" xmlUrl="http://www.cadenhead.org/workbench/rss.xml"/>
+ <outline text="Christian Science Monitor | Top Stories" description="Read the front page stories of csmonitor.com." htmlUrl="http://csmonitor.com" language="unknown" title="Christian Science Monitor | Top Stories" type="rss" version="RSS" xmlUrl="http://www.csmonitor.com/rss/top.rss"/>
+ <outline text="Dictionary.com Word of the Day" description="A new word is presented every day with its definition and example sentences from actual published works." htmlUrl="http://dictionary.reference.com/wordoftheday/" language="unknown" title="Dictionary.com Word of the Day" type="rss" version="RSS" xmlUrl="http://www.dictionary.com/wordoftheday/wotd.rss"/>
+ <outline text="The Motley Fool" description="To Educate, Amuse, and Enrich" htmlUrl="http://www.fool.com" language="unknown" title="The Motley Fool" type="rss" version="RSS" xmlUrl="http://www.fool.com/xml/foolnews_rss091.xml"/>
+ <outline text="InfoWorld: Top News" description="The latest on Top News from InfoWorld" htmlUrl="http://www.infoworld.com/news/index.html" language="unknown" title="InfoWorld: Top News" type="rss" version="RSS2" xmlUrl="http://www.infoworld.com/rss/news.xml"/>
+ <outline text="NYT &gt; Business" description="Find breaking news &amp; business news on Wall Street, media &amp; advertising, international business, banking, interest rates, the stock market, currencies &amp; funds." htmlUrl="http://www.nytimes.com/pages/business/index.html?partner=rssnyt" language="unknown" title="NYT &gt; Business" type="rss" version="RSS2" xmlUrl="http://www.nytimes.com/services/xml/rss/nyt/Business.xml"/>
+ <outline text="NYT &gt; Technology" description="" htmlUrl="http://www.nytimes.com/pages/technology/index.html?partner=rssnyt" language="unknown" title="NYT &gt; Technology" type="rss" version="RSS2" xmlUrl="http://www.nytimes.com/services/xml/rss/nyt/Technology.xml"/>
+ <outline text="Scripting News" description="It's even worse than it appears." htmlUrl="http://www.scripting.com/" language="unknown" title="Scripting News" type="rss" version="RSS2" xmlUrl="http://www.scripting.com/rss.xml"/>
+ <outline text="Wired News" description="Technology, and the way we do business, is changing the world we know. Wired News is a technology - and business-oriented news service feeding an intelligent, discerning audience. What role does technology play in the day-to-day living of your life? Wired News tells you. How has evolving technology changed the face of the international business world? Wired News puts you in the picture." htmlUrl="http://www.wired.com/" language="unknown" title="Wired News" type="rss" version="RSS" xmlUrl="http://www.wired.com/news_drop/netcenter/netcenter.rdf"/>
+ </body>
+ </opml>
+ `
+
+ var expected SubcriptionList
+ expected = append(expected, &Subcription{Title: "CNET News.com", FeedURL: "http://news.com.com/2547-1_3-0-5.xml", SiteURL: "http://news.com.com/"})
+
+ subscriptions, err := Parse(bytes.NewBufferString(data))
+ if err != nil {
+ t.Error(err)
+ }
+
+ if len(subscriptions) != 13 {
+ t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 13)
+ }
+
+ if !subscriptions[0].Equals(expected[0]) {
+ t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[0], expected[0])
+ }
+}
+
+func TestParseOpmlWithCategories(t *testing.T) {
+ data := `<?xml version="1.0" encoding="utf-8"?>
+ <opml version="2.0">
+ <head>
+ <title>mySubscriptions.opml</title>
+ </head>
+ <body>
+ <outline text="My Category 1">
+ <outline text="Feed 1" xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
+ <outline text="Feed 2" xmlUrl="http://example.org/feed2/" htmlUrl="http://example.org/2"/>
+ </outline>
+ <outline text="My Category 2">
+ <outline text="Feed 3" xmlUrl="http://example.org/feed3/" htmlUrl="http://example.org/3"/>
+ </outline>
+ </body>
+ </opml>
+ `
+
+ var expected SubcriptionList
+ expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category 1"})
+ expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: "My Category 1"})
+ expected = append(expected, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed3/", SiteURL: "http://example.org/3", CategoryName: "My Category 2"})
+
+ subscriptions, err := Parse(bytes.NewBufferString(data))
+ if err != nil {
+ t.Error(err)
+ }
+
+ if len(subscriptions) != 3 {
+ t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 3)
+ }
+
+ for i := 0; i < len(subscriptions); i++ {
+ if !subscriptions[i].Equals(expected[i]) {
+ t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i])
+ }
+ }
+}
+
+func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) {
+ data := `<?xml version="1.0" encoding="ISO-8859-1"?>
+ <opml version="2.0">
+ <head>
+ <title>mySubscriptions.opml</title>
+ </head>
+ <body>
+ <outline xmlUrl="http://example.org/feed1/" htmlUrl="http://example.org/1"/>
+ <outline xmlUrl="http://example.org/feed2/"/>
+ </body>
+ </opml>
+ `
+
+ var expected SubcriptionList
+ expected = append(expected, &Subcription{Title: "http://example.org/1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""})
+ expected = append(expected, &Subcription{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: ""})
+
+ subscriptions, err := Parse(bytes.NewBufferString(data))
+ if err != nil {
+ t.Error(err)
+ }
+
+ if len(subscriptions) != 2 {
+ t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2)
+ }
+
+ for i := 0; i < len(subscriptions); i++ {
+ if !subscriptions[i].Equals(expected[i]) {
+ t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i])
+ }
+ }
+}
+
+func TestParseInvalidXML(t *testing.T) {
+ data := `<?xml version="1.0" encoding="ISO-8859-1"?>
+ <opml version="2.0">
+ <head>
+ </head>
+ <body>
+ <outline
+ </body>
+ </opml>
+ `
+
+ _, err := Parse(bytes.NewBufferString(data))
+ if err == nil {
+ t.Error(err)
+ }
+}
diff --git a/reader/opml/serializer.go b/reader/opml/serializer.go
new file mode 100644
index 0000000..20c7046
--- /dev/null
+++ b/reader/opml/serializer.go
@@ -0,0 +1,58 @@
+// 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 (
+ "bufio"
+ "bytes"
+ "encoding/xml"
+ "log"
+)
+
+func Serialize(subscriptions SubcriptionList) string {
+ var b bytes.Buffer
+ writer := bufio.NewWriter(&b)
+ writer.WriteString(xml.Header)
+
+ opml := new(Opml)
+ opml.Version = "2.0"
+ for categoryName, subs := range groupSubscriptionsByFeed(subscriptions) {
+ outline := Outline{Text: categoryName}
+
+ for _, subscription := range subs {
+ outline.Outlines = append(outline.Outlines, Outline{
+ Title: subscription.Title,
+ Text: subscription.Title,
+ FeedURL: subscription.FeedURL,
+ SiteURL: subscription.SiteURL,
+ })
+ }
+
+ opml.Outlines = append(opml.Outlines, outline)
+ }
+
+ encoder := xml.NewEncoder(writer)
+ encoder.Indent(" ", " ")
+ if err := encoder.Encode(opml); err != nil {
+ log.Println(err)
+ return ""
+ }
+
+ return b.String()
+}
+
+func groupSubscriptionsByFeed(subscriptions SubcriptionList) map[string]SubcriptionList {
+ groups := make(map[string]SubcriptionList)
+
+ for _, subscription := range subscriptions {
+ // if subs, ok := groups[subscription.CategoryName]; !ok {
+ // groups[subscription.CategoryName] = SubcriptionList{}
+ // }
+
+ groups[subscription.CategoryName] = append(groups[subscription.CategoryName], subscription)
+ }
+
+ return groups
+}
diff --git a/reader/opml/serializer_test.go b/reader/opml/serializer_test.go
new file mode 100644
index 0000000..b1ef2a6
--- /dev/null
+++ b/reader/opml/serializer_test.go
@@ -0,0 +1,31 @@
+// 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 "testing"
+import "bytes"
+
+func TestSerialize(t *testing.T) {
+ var subscriptions SubcriptionList
+ subscriptions = append(subscriptions, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed/1", SiteURL: "http://example.org/1", CategoryName: "Category 1"})
+ subscriptions = append(subscriptions, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed/2", SiteURL: "http://example.org/2", CategoryName: "Category 1"})
+ subscriptions = append(subscriptions, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: "Category 2"})
+
+ output := Serialize(subscriptions)
+ feeds, err := Parse(bytes.NewBufferString(output))
+ if err != nil {
+ t.Error(err)
+ }
+
+ if len(feeds) != 3 {
+ t.Errorf("Wrong number of subscriptions: %d instead of %d", len(feeds), 3)
+ }
+
+ for i := 0; i < len(feeds); i++ {
+ if !feeds[i].Equals(subscriptions[i]) {
+ t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], feeds[i])
+ }
+ }
+}
diff --git a/reader/opml/subscription.go b/reader/opml/subscription.go
new file mode 100644
index 0000000..b968bb0
--- /dev/null
+++ b/reader/opml/subscription.go
@@ -0,0 +1,18 @@
+// 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
+
+type Subcription struct {
+ Title string
+ SiteURL string
+ FeedURL string
+ CategoryName string
+}
+
+func (s Subcription) Equals(subscription *Subcription) bool {
+ return s.Title == subscription.Title && s.SiteURL == subscription.SiteURL && s.FeedURL == subscription.FeedURL && s.CategoryName == subscription.CategoryName
+}
+
+type SubcriptionList []*Subcription