1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00

56 lines
1.3 KiB
Go
Raw Normal View History

package feed
import (
"context"
"fmt"
2020-04-15 13:59:30 -07:00
"github.com/gilliek/go-opml/opml"
"github.com/pkg/errors"
2020-04-16 15:14:46 -07:00
log "github.com/sirupsen/logrus"
"github.com/mxpv/podsync/pkg/config"
2020-04-16 15:14:46 -07:00
"github.com/mxpv/podsync/pkg/model"
)
2020-04-15 13:59:30 -07:00
func BuildOPML(ctx context.Context, config *config.Config, db feedProvider, provider urlProvider) (string, error) {
doc := opml.OPML{Version: "1.0"}
doc.Head = opml.Head{Title: "Podsync feeds"}
doc.Body = opml.Body{}
for _, feed := range config.Feeds {
f, err := db.GetFeed(ctx, feed.ID)
2020-04-16 15:14:46 -07:00
if err == model.ErrNotFound {
// As we update OPML on per-feed basis, some feeds may not yet be populated in database.
log.Debugf("can't find configuration for feed %q, ignoring opml", feed.ID)
continue
} else if err != nil {
return "", errors.Wrapf(err, "failed to query feed %q", feed.ID)
}
2020-04-16 15:14:46 -07:00
if !feed.OPML {
continue
}
2020-04-16 15:14:46 -07:00
downloadURL, err := provider.URL(ctx, "", fmt.Sprintf("%s.xml", feed.ID))
if err != nil {
return "", errors.Wrapf(err, "failed to get feed URL for %q", feed.ID)
}
2020-04-16 15:14:46 -07:00
outline := opml.Outline{
Title: f.Title,
Text: f.Description,
Type: "rss",
XMLURL: downloadURL,
2020-04-15 13:59:30 -07:00
}
2020-04-16 15:14:46 -07:00
doc.Body.Outlines = append(doc.Body.Outlines, outline)
2020-04-15 13:59:30 -07:00
}
2020-04-15 13:59:30 -07:00
out, err := doc.XML()
if err != nil {
return "", errors.Wrap(err, "failed to marshal OPML")
}
2020-04-15 13:59:30 -07:00
return out, nil
}