From 8bad2d1a88874ee86b23697d804b6d4f908e971c Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Mon, 10 Sep 2018 20:19:32 +0200 Subject: [PATCH] Add export opml command --- cmd/ek/main.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/cmd/ek/main.go b/cmd/ek/main.go index 520db9d..52afae0 100644 --- a/cmd/ek/main.go +++ b/cmd/ek/main.go @@ -24,7 +24,9 @@ import ( "log" "net/url" "os" + "time" + "github.com/gilliek/go-opml/opml" "p83.nl/go/ekster/pkg/client" "p83.nl/go/ekster/pkg/indieauth" "p83.nl/go/ekster/pkg/microsub" @@ -138,6 +140,8 @@ Commands: unfollow UID URL unfollow URL on channel UID + export opml export feeds as OPML + Global arguments: `) @@ -326,6 +330,53 @@ func performCommands(sub microsub.Microsub, commands []string) { log.Fatalf("An error occurred: %s\n", err) } } + + if len(commands) == 2 && commands[0] == "export" { + filetype := commands[1] + if filetype == "opml" { + output := opml.OPML{} + output.Head.Title = "Microsub channels and feeds" + output.Head.DateCreated = time.Now().Format(time.RFC3339) + output.Version = "1.0" + + channels, err := sub.ChannelsGetList() + if err != nil { + log.Fatalf("An error occurred: %s\n", err) + } + + for _, c := range channels { + var feeds []opml.Outline + list, err := sub.FollowGetList(c.UID) + if err != nil { + log.Fatalf("An error occurred: %s\n", err) + } + for _, f := range list { + feeds = append(feeds, opml.Outline{ + Title: f.Name, + Text: f.Name, + Type: f.Type, + URL: f.URL, + HTMLURL: f.URL, + XMLURL: f.URL, + }) + } + + output.Body.Outlines = append(output.Body.Outlines, opml.Outline{ + Text: c.Name, + Title: c.Name, + Outlines: feeds, + }) + } + + xml, err := output.XML() + if err != nil { + log.Fatalf("An error occurred: %s\n", err) + } + os.Stdout.WriteString(xml) + } else { + log.Fatalf("unsupported filetype %q", filetype) + } + } } func showItem(item *microsub.Item) {