From cd1509967d7dafbf2d1a051206e368bd3aa5e179 Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Sun, 8 Apr 2018 08:51:26 -0700 Subject: refactor exporter, add json/opml export formats --- exporter/exporter.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 32 +++++++------------------------- static/feeds.tmpl | 13 +++++++++++++ 3 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 exporter/exporter.go create mode 100644 static/feeds.tmpl diff --git a/exporter/exporter.go b/exporter/exporter.go new file mode 100644 index 0000000..8e7ff12 --- /dev/null +++ b/exporter/exporter.go @@ -0,0 +1,47 @@ +package exporter + +import ( + "adammathes.com/neko/config" + "adammathes.com/neko/models/feed" + "encoding/json" + "encoding/xml" + "fmt" + "html/template" + "os" + "path" +) + +func ExportFeeds(format string) { + feeds, err := feed.All() + if err != nil { + panic(err) + } + + switch format { + case "text": + for _, f := range feeds { + fmt.Printf("%s\n", f.Url) + } + + case "opml": + fmt.Printf(`neko feeds`) + fmt.Printf("\n") + for _, f := range feeds { + b, _ := xml.Marshal(f) + fmt.Printf("%s\n", string(b)) + } + fmt.Printf(``) + + case "json": + js, _ := json.Marshal(feeds) + fmt.Printf("%s\n", js) + + case "html": + tmplFile := path.Join(config.Config.StaticDir, "feeds.tmpl") + feedsTmpl := template.Must(template.ParseFiles(tmplFile)) + err := feedsTmpl.Execute(os.Stdout, feeds) + if err != nil { + panic(err) + } + } +} diff --git a/main.go b/main.go index b7aefb3..40f2647 100644 --- a/main.go +++ b/main.go @@ -3,30 +3,28 @@ package main import ( "adammathes.com/neko/config" "adammathes.com/neko/crawler" + "adammathes.com/neko/exporter" "adammathes.com/neko/models" "adammathes.com/neko/models/feed" "adammathes.com/neko/vlog" "adammathes.com/neko/web" - "encoding/xml" - "fmt" flag "github.com/ogier/pflag" ) func main() { - var serve, update, verbose, printFeeds, opml bool - var configFile, newFeed string + var serve, update, verbose bool + var configFile, newFeed, export string flag.StringVarP(&configFile, "config", "c", "config.json", "`configuration` file") flag.BoolVarP(&update, "update", "u", false, "fetch feeds and store them in the database") flag.BoolVarP(&serve, "serve", "s", false, "run http server") flag.BoolVarP(&verbose, "verbose", "v", false, "verbose output") - flag.BoolVarP(&printFeeds, "feeds", "f", false, "list all currently crawled feeds") - flag.BoolVarP(&opml, "opml", "o", false, "export feed list as opml") flag.StringVarP(&newFeed, "add", "a", "", "add the feed at URL `http://example.com/rss.xml`") + flag.StringVarP(&export, "export", "x", "", "export feeds as `text`, json or opml") flag.Parse() // no command - if !update && !serve && !printFeeds && !opml && newFeed == "" { + if !update && !serve && newFeed == "" && export == "" { flag.Usage() return } @@ -45,23 +43,7 @@ func main() { if newFeed != "" { feed.NewFeed(newFeed) } - if printFeeds { - feeds, err := feed.All() - if err != nil { - panic(err) - } - for _, f := range feeds { - fmt.Printf("%s\n", f.Url) - } - } - if opml { - feeds, _ := feed.All() - fmt.Printf(`neko feeds`) - fmt.Printf("\n") - for _, f := range feeds { - b, _ := xml.Marshal(f) - fmt.Printf("%s\n", string(b)) - } - fmt.Printf(``) + if export != "" { + exporter.ExportFeeds(export) } } diff --git a/static/feeds.tmpl b/static/feeds.tmpl new file mode 100644 index 0000000..fb089fd --- /dev/null +++ b/static/feeds.tmpl @@ -0,0 +1,13 @@ + + +neko exported feeds + + +

neko exported feeds

+ + + \ No newline at end of file -- cgit v1.2.3