mirror of
https://github.com/miniflux/v2.git
synced 2025-06-27 16:36:00 +00:00
Improve OPML package to be more idiomatic
This commit is contained in:
parent
e91a9b4f13
commit
c26787f476
7 changed files with 50 additions and 40 deletions
|
@ -7,21 +7,24 @@ package opml
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/miniflux/miniflux2/model"
|
||||
"github.com/miniflux/miniflux2/storage"
|
||||
"io"
|
||||
"log"
|
||||
|
||||
"github.com/miniflux/miniflux2/model"
|
||||
"github.com/miniflux/miniflux2/storage"
|
||||
)
|
||||
|
||||
type OpmlHandler struct {
|
||||
// Handler handles the logic for OPML import/export.
|
||||
type Handler struct {
|
||||
store *storage.Storage
|
||||
}
|
||||
|
||||
func (o *OpmlHandler) Export(userID int64) (string, error) {
|
||||
feeds, err := o.store.GetFeeds(userID)
|
||||
// Export exports user feeds to OPML.
|
||||
func (h *Handler) Export(userID int64) (string, error) {
|
||||
feeds, err := h.store.GetFeeds(userID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return "", errors.New("Unable to fetch feeds.")
|
||||
return "", errors.New("unable to fetch feeds")
|
||||
}
|
||||
|
||||
var subscriptions SubcriptionList
|
||||
|
@ -37,27 +40,28 @@ func (o *OpmlHandler) Export(userID int64) (string, error) {
|
|||
return Serialize(subscriptions), nil
|
||||
}
|
||||
|
||||
func (o *OpmlHandler) Import(userID int64, data io.Reader) (err error) {
|
||||
// Import parses and create feeds from an OPML import.
|
||||
func (h *Handler) 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) {
|
||||
if !h.store.FeedURLExists(userID, subscription.FeedURL) {
|
||||
var category *model.Category
|
||||
|
||||
if subscription.CategoryName == "" {
|
||||
category, err = o.store.GetFirstCategory(userID)
|
||||
category, err = h.store.GetFirstCategory(userID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return errors.New("Unable to find first category.")
|
||||
return errors.New("unable to find first category")
|
||||
}
|
||||
} else {
|
||||
category, err = o.store.GetCategoryByTitle(userID, subscription.CategoryName)
|
||||
category, err = h.store.GetCategoryByTitle(userID, subscription.CategoryName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return errors.New("Unable to search category by title.")
|
||||
return errors.New("unable to search category by title")
|
||||
}
|
||||
|
||||
if category == nil {
|
||||
|
@ -66,10 +70,10 @@ func (o *OpmlHandler) Import(userID int64, data io.Reader) (err error) {
|
|||
Title: subscription.CategoryName,
|
||||
}
|
||||
|
||||
err := o.store.CreateCategory(category)
|
||||
err := h.store.CreateCategory(category)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return fmt.Errorf(`Unable to create this category: "%s".`, subscription.CategoryName)
|
||||
return fmt.Errorf(`unable to create this category: "%s"`, subscription.CategoryName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -82,13 +86,14 @@ func (o *OpmlHandler) Import(userID int64, data io.Reader) (err error) {
|
|||
Category: category,
|
||||
}
|
||||
|
||||
o.store.CreateFeed(feed)
|
||||
h.store.CreateFeed(feed)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewOpmlHandler(store *storage.Storage) *OpmlHandler {
|
||||
return &OpmlHandler{store: store}
|
||||
// NewHandler creates a new handler for OPML files.
|
||||
func NewHandler(store *storage.Storage) *Handler {
|
||||
return &Handler{store: store}
|
||||
}
|
||||
|
|
|
@ -6,21 +6,21 @@ package opml
|
|||
|
||||
import "encoding/xml"
|
||||
|
||||
type Opml struct {
|
||||
type opml struct {
|
||||
XMLName xml.Name `xml:"opml"`
|
||||
Version string `xml:"version,attr"`
|
||||
Outlines []Outline `xml:"body>outline"`
|
||||
Outlines []outline `xml:"body>outline"`
|
||||
}
|
||||
|
||||
type Outline struct {
|
||||
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"`
|
||||
Outlines []outline `xml:"outline,omitempty"`
|
||||
}
|
||||
|
||||
func (o *Outline) GetTitle() string {
|
||||
func (o *outline) GetTitle() string {
|
||||
if o.Title != "" {
|
||||
return o.Title
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ func (o *Outline) GetTitle() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (o *Outline) GetSiteURL() string {
|
||||
func (o *outline) GetSiteURL() string {
|
||||
if o.SiteURL != "" {
|
||||
return o.SiteURL
|
||||
}
|
||||
|
@ -48,11 +48,11 @@ func (o *Outline) GetSiteURL() string {
|
|||
return o.FeedURL
|
||||
}
|
||||
|
||||
func (o *Outline) IsCategory() bool {
|
||||
func (o *outline) IsCategory() bool {
|
||||
return o.Text != "" && o.SiteURL == "" && o.FeedURL == ""
|
||||
}
|
||||
|
||||
func (o *Outline) Append(subscriptions SubcriptionList, category string) SubcriptionList {
|
||||
func (o *outline) Append(subscriptions SubcriptionList, category string) SubcriptionList {
|
||||
if o.FeedURL != "" {
|
||||
subscriptions = append(subscriptions, &Subcription{
|
||||
Title: o.GetTitle(),
|
||||
|
@ -65,7 +65,7 @@ func (o *Outline) Append(subscriptions SubcriptionList, category string) Subcrip
|
|||
return subscriptions
|
||||
}
|
||||
|
||||
func (o *Opml) Transform() SubcriptionList {
|
||||
func (o *opml) Transform() SubcriptionList {
|
||||
var subscriptions SubcriptionList
|
||||
|
||||
for _, outline := range o.Outlines {
|
||||
|
|
|
@ -14,14 +14,14 @@ import (
|
|||
|
||||
// Parse reads an OPML file and returns a SubcriptionList.
|
||||
func Parse(data io.Reader) (SubcriptionList, error) {
|
||||
opml := new(Opml)
|
||||
feeds := new(opml)
|
||||
decoder := xml.NewDecoder(data)
|
||||
decoder.CharsetReader = charset.NewReaderLabel
|
||||
|
||||
err := decoder.Decode(opml)
|
||||
err := decoder.Decode(feeds)
|
||||
if err != nil {
|
||||
return nil, errors.NewLocalizedError("Unable to parse OPML file: %v.", err)
|
||||
}
|
||||
|
||||
return opml.Transform(), nil
|
||||
return feeds.Transform(), nil
|
||||
}
|
||||
|
|
|
@ -17,13 +17,13 @@ func Serialize(subscriptions SubcriptionList) string {
|
|||
writer := bufio.NewWriter(&b)
|
||||
writer.WriteString(xml.Header)
|
||||
|
||||
opml := new(Opml)
|
||||
opml.Version = "2.0"
|
||||
feeds := new(opml)
|
||||
feeds.Version = "2.0"
|
||||
for categoryName, subs := range groupSubscriptionsByFeed(subscriptions) {
|
||||
outline := Outline{Text: categoryName}
|
||||
category := outline{Text: categoryName}
|
||||
|
||||
for _, subscription := range subs {
|
||||
outline.Outlines = append(outline.Outlines, Outline{
|
||||
category.Outlines = append(category.Outlines, outline{
|
||||
Title: subscription.Title,
|
||||
Text: subscription.Title,
|
||||
FeedURL: subscription.FeedURL,
|
||||
|
@ -31,12 +31,12 @@ func Serialize(subscriptions SubcriptionList) string {
|
|||
})
|
||||
}
|
||||
|
||||
opml.Outlines = append(opml.Outlines, outline)
|
||||
feeds.Outlines = append(feeds.Outlines, category)
|
||||
}
|
||||
|
||||
encoder := xml.NewEncoder(writer)
|
||||
encoder.Indent(" ", " ")
|
||||
if err := encoder.Encode(opml); err != nil {
|
||||
if err := encoder.Encode(feeds); err != nil {
|
||||
log.Println(err)
|
||||
return ""
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package opml
|
||||
|
||||
// Subcription represents a feed that will be imported or exported.
|
||||
type Subcription struct {
|
||||
Title string
|
||||
SiteURL string
|
||||
|
@ -11,8 +12,11 @@ type Subcription struct {
|
|||
CategoryName string
|
||||
}
|
||||
|
||||
// Equals compare two subscriptions.
|
||||
func (s Subcription) Equals(subscription *Subcription) bool {
|
||||
return s.Title == subscription.Title && s.SiteURL == subscription.SiteURL && s.FeedURL == subscription.FeedURL && s.CategoryName == subscription.CategoryName
|
||||
return s.Title == subscription.Title && s.SiteURL == subscription.SiteURL &&
|
||||
s.FeedURL == subscription.FeedURL && s.CategoryName == subscription.CategoryName
|
||||
}
|
||||
|
||||
// SubcriptionList is a list of subscriptions.
|
||||
type SubcriptionList []*Subcription
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue