Compare commits
2 Commits
47edcc41be
...
8b5b1ddf70
Author | SHA1 | Date | |
---|---|---|---|
8b5b1ddf70 | |||
b2a4410d6c |
122
cmd/ek/main.go
122
cmd/ek/main.go
|
@ -32,10 +32,30 @@ import (
|
|||
"p83.nl/go/ekster/pkg/microsub"
|
||||
)
|
||||
|
||||
const (
|
||||
// Version is the version of the command
|
||||
Version = "0.8.2"
|
||||
)
|
||||
|
||||
var (
|
||||
verbose = flag.Bool("verbose", false, "show verbose logging")
|
||||
)
|
||||
|
||||
// Export is the JSON export format
|
||||
type Export struct {
|
||||
Version string `json:"version"`
|
||||
Generator string `json:"generator"`
|
||||
Channels []ExportChannel `json:"channels,omitempty"`
|
||||
Feeds map[string][]ExportFeed `json:"feeds,omitempty"`
|
||||
}
|
||||
|
||||
type ExportFeed string
|
||||
|
||||
type ExportChannel struct {
|
||||
UID string `json:"uid,omitempty"`
|
||||
Name string `json:"channel,omitempty"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
|
||||
}
|
||||
|
@ -143,6 +163,9 @@ Commands:
|
|||
export opml export feeds as OPML
|
||||
import opml FILENAME import OPML feeds
|
||||
|
||||
export json export feeds as json
|
||||
import json FILENAME import json feeds
|
||||
|
||||
Global arguments:
|
||||
|
||||
`)
|
||||
|
@ -334,6 +357,7 @@ func performCommands(sub microsub.Microsub, commands []string) {
|
|||
|
||||
if len(commands) == 2 && commands[0] == "export" {
|
||||
filetype := commands[1]
|
||||
|
||||
if filetype == "opml" {
|
||||
output := opml.OPML{}
|
||||
output.Head.Title = "Microsub channels and feeds"
|
||||
|
@ -374,6 +398,33 @@ func performCommands(sub microsub.Microsub, commands []string) {
|
|||
log.Fatalf("An error occurred: %s\n", err)
|
||||
}
|
||||
os.Stdout.WriteString(xml)
|
||||
} else if filetype == "json" {
|
||||
contents := Export{Version: "1.0", Generator: "ek version " + Version}
|
||||
|
||||
channels, err := sub.ChannelsGetList()
|
||||
if err != nil {
|
||||
log.Fatalf("An error occurred: %s\n", err)
|
||||
}
|
||||
|
||||
for _, c := range channels {
|
||||
contents.Channels = append(contents.Channels, ExportChannel{UID: c.UID, Name: c.Name})
|
||||
}
|
||||
|
||||
contents.Feeds = make(map[string][]ExportFeed)
|
||||
|
||||
for _, c := range channels {
|
||||
list, err := sub.FollowGetList(c.UID)
|
||||
if err != nil {
|
||||
log.Fatalf("An error occurred: %s\n", err)
|
||||
}
|
||||
for _, f := range list {
|
||||
contents.Feeds[c.UID] = append(contents.Feeds[c.UID], ExportFeed(f.URL))
|
||||
}
|
||||
}
|
||||
err = json.NewEncoder(os.Stdout).Encode(&contents)
|
||||
if err != nil {
|
||||
log.Fatalf("An error occurred: %s\n", err)
|
||||
}
|
||||
} else {
|
||||
log.Fatalf("unsupported filetype %q", filetype)
|
||||
}
|
||||
|
@ -451,8 +502,79 @@ func performCommands(sub microsub.Microsub, commands []string) {
|
|||
}
|
||||
}
|
||||
}
|
||||
} else if filetype == "json" {
|
||||
var export Export
|
||||
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.Fatalf("can't open file %s: %s", filename, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
err = json.NewDecoder(f).Decode(&export)
|
||||
if err != nil {
|
||||
log.Fatalf("error while reading %s: %s", filename, err)
|
||||
}
|
||||
|
||||
channelMap := make(map[string]microsub.Channel)
|
||||
|
||||
channels, err := sub.ChannelsGetList()
|
||||
if err != nil {
|
||||
log.Fatalf("an error occurred: %s\n", err)
|
||||
}
|
||||
|
||||
for _, c := range channels {
|
||||
channelMap[c.Name] = c
|
||||
}
|
||||
|
||||
for _, c := range export.Channels {
|
||||
uid := ""
|
||||
|
||||
if ch, e := channelMap[c.Name]; !e {
|
||||
channelCreated, err := sub.ChannelsCreate(c.Name)
|
||||
if err != nil {
|
||||
log.Printf("An error occurred: %q\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
uid = channelCreated.UID
|
||||
log.Printf("Channel created: %s\n", c.Name)
|
||||
} else {
|
||||
uid = ch.UID
|
||||
}
|
||||
|
||||
feedMap := make(map[string]bool)
|
||||
|
||||
feeds, err := sub.FollowGetList(uid)
|
||||
if err != nil {
|
||||
log.Fatalf("An error occurred: %q\n", err)
|
||||
}
|
||||
|
||||
for _, f := range feeds {
|
||||
feedMap[f.URL] = true
|
||||
}
|
||||
|
||||
for _, feed := range export.Feeds[uid] {
|
||||
|
||||
if _, e := feedMap[string(feed)]; !e {
|
||||
_, err := sub.FollowURL(uid, string(feed))
|
||||
if err != nil {
|
||||
log.Printf("An error occurred: %q\n", err)
|
||||
continue
|
||||
}
|
||||
log.Printf("Feed followed: %s\n", string(feed))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
log.Fatalf("unsupported filetype %q", filetype)
|
||||
}
|
||||
}
|
||||
|
||||
if len(commands) == 1 && commands[0] == "version" {
|
||||
fmt.Printf("ek %s\n", Version)
|
||||
}
|
||||
}
|
||||
|
||||
func showItem(item *microsub.Item) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user