2023-06-19 17:42:47 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-11-20 00:10:04 -05:00
|
|
|
|
2023-08-10 00:15:55 -04:00
|
|
|
package opml // import "miniflux.app/v2/reader/opml"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
2019-07-05 12:05:42 -04:00
|
|
|
"sort"
|
2021-12-16 14:42:43 -05:00
|
|
|
"time"
|
2017-12-15 21:55:57 -05:00
|
|
|
|
2023-08-10 00:15:55 -04:00
|
|
|
"miniflux.app/v2/logger"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-20 17:35:11 -05:00
|
|
|
// Serialize returns a SubcriptionList in OPML format.
|
2017-11-20 00:10:04 -05:00
|
|
|
func Serialize(subscriptions SubcriptionList) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
writer := bufio.NewWriter(&b)
|
|
|
|
writer.WriteString(xml.Header)
|
|
|
|
|
2021-12-16 14:42:43 -05:00
|
|
|
opmlDocument := convertSubscriptionsToOPML(subscriptions)
|
2017-11-20 00:10:04 -05:00
|
|
|
encoder := xml.NewEncoder(writer)
|
2021-12-16 14:42:43 -05:00
|
|
|
encoder.Indent("", " ")
|
|
|
|
if err := encoder.Encode(opmlDocument); err != nil {
|
2017-12-15 21:55:57 -05:00
|
|
|
logger.Error("[OPML:Serialize] %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2021-12-16 14:42:43 -05:00
|
|
|
func convertSubscriptionsToOPML(subscriptions SubcriptionList) *opmlDocument {
|
|
|
|
opmlDocument := NewOPMLDocument()
|
|
|
|
opmlDocument.Version = "2.0"
|
|
|
|
opmlDocument.Header.Title = "Miniflux"
|
|
|
|
opmlDocument.Header.DateCreated = time.Now().Format("Mon, 02 Jan 2006 15:04:05 MST")
|
2019-07-05 12:05:42 -04:00
|
|
|
|
|
|
|
groupedSubs := groupSubscriptionsByFeed(subscriptions)
|
|
|
|
var categories []string
|
|
|
|
for k := range groupedSubs {
|
|
|
|
categories = append(categories, k)
|
|
|
|
}
|
|
|
|
sort.Strings(categories)
|
|
|
|
|
|
|
|
for _, categoryName := range categories {
|
2021-12-16 14:42:43 -05:00
|
|
|
category := opmlOutline{Text: categoryName}
|
2019-07-05 12:05:42 -04:00
|
|
|
for _, subscription := range groupedSubs[categoryName] {
|
2021-12-16 14:42:43 -05:00
|
|
|
category.Outlines = append(category.Outlines, opmlOutline{
|
2019-07-05 12:05:42 -04:00
|
|
|
Title: subscription.Title,
|
|
|
|
Text: subscription.Title,
|
|
|
|
FeedURL: subscription.FeedURL,
|
|
|
|
SiteURL: subscription.SiteURL,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-16 14:42:43 -05:00
|
|
|
opmlDocument.Outlines = append(opmlDocument.Outlines, category)
|
2019-07-05 12:05:42 -04:00
|
|
|
}
|
|
|
|
|
2021-12-16 14:42:43 -05:00
|
|
|
return opmlDocument
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupSubscriptionsByFeed(subscriptions SubcriptionList) map[string]SubcriptionList {
|
|
|
|
groups := make(map[string]SubcriptionList)
|
|
|
|
|
|
|
|
for _, subscription := range subscriptions {
|
|
|
|
groups[subscription.CategoryName] = append(groups[subscription.CategoryName], subscription)
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups
|
2019-07-05 12:05:42 -04:00
|
|
|
}
|