From 8ffb773f43c8dc54801ca1d111854e7e881c93c9 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 19 Nov 2017 21:10:04 -0800 Subject: First commit --- reader/opml/handler.go | 94 ++++++++++++++++++++++++++++ reader/opml/opml.go | 82 ++++++++++++++++++++++++ reader/opml/parser.go | 26 ++++++++ reader/opml/parser_test.go | 138 +++++++++++++++++++++++++++++++++++++++++ reader/opml/serializer.go | 58 +++++++++++++++++ reader/opml/serializer_test.go | 31 +++++++++ reader/opml/subscription.go | 18 ++++++ 7 files changed, 447 insertions(+) create mode 100644 reader/opml/handler.go create mode 100644 reader/opml/opml.go create mode 100644 reader/opml/parser.go create mode 100644 reader/opml/parser_test.go create mode 100644 reader/opml/serializer.go create mode 100644 reader/opml/serializer_test.go create mode 100644 reader/opml/subscription.go (limited to 'reader/opml') 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 := ` + + + mySubscriptions.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 := ` + + + mySubscriptions.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 := ` + + + mySubscriptions.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 := ` + + + + + + + ` + + _, 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 -- cgit v1.2.3