Compare commits
No commits in common. "8b5b1ddf700d3e8ac10a8477f1598fc87b3ff2e0" and "47edcc41beb6e4c6f6bbcbec16e4e73963574fed" have entirely different histories.
8b5b1ddf70
...
47edcc41be
122
cmd/ek/main.go
122
cmd/ek/main.go
|
|
@ -32,30 +32,10 @@ import (
|
||||||
"p83.nl/go/ekster/pkg/microsub"
|
"p83.nl/go/ekster/pkg/microsub"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
// Version is the version of the command
|
|
||||||
Version = "0.8.2"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
verbose = flag.Bool("verbose", false, "show verbose logging")
|
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() {
|
func init() {
|
||||||
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
|
log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime)
|
||||||
}
|
}
|
||||||
|
|
@ -163,9 +143,6 @@ Commands:
|
||||||
export opml export feeds as OPML
|
export opml export feeds as OPML
|
||||||
import opml FILENAME import OPML feeds
|
import opml FILENAME import OPML feeds
|
||||||
|
|
||||||
export json export feeds as json
|
|
||||||
import json FILENAME import json feeds
|
|
||||||
|
|
||||||
Global arguments:
|
Global arguments:
|
||||||
|
|
||||||
`)
|
`)
|
||||||
|
|
@ -357,7 +334,6 @@ func performCommands(sub microsub.Microsub, commands []string) {
|
||||||
|
|
||||||
if len(commands) == 2 && commands[0] == "export" {
|
if len(commands) == 2 && commands[0] == "export" {
|
||||||
filetype := commands[1]
|
filetype := commands[1]
|
||||||
|
|
||||||
if filetype == "opml" {
|
if filetype == "opml" {
|
||||||
output := opml.OPML{}
|
output := opml.OPML{}
|
||||||
output.Head.Title = "Microsub channels and feeds"
|
output.Head.Title = "Microsub channels and feeds"
|
||||||
|
|
@ -398,33 +374,6 @@ func performCommands(sub microsub.Microsub, commands []string) {
|
||||||
log.Fatalf("An error occurred: %s\n", err)
|
log.Fatalf("An error occurred: %s\n", err)
|
||||||
}
|
}
|
||||||
os.Stdout.WriteString(xml)
|
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 {
|
} else {
|
||||||
log.Fatalf("unsupported filetype %q", filetype)
|
log.Fatalf("unsupported filetype %q", filetype)
|
||||||
}
|
}
|
||||||
|
|
@ -502,79 +451,8 @@ 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) {
|
func showItem(item *microsub.Item) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user