aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--exporter/exporter.go47
-rw-r--r--main.go32
-rw-r--r--static/feeds.tmpl13
3 files changed, 67 insertions, 25 deletions
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(`<opml version="2.0"><head><title>neko feeds</title></head><body>`)
+ fmt.Printf("\n")
+ for _, f := range feeds {
+ b, _ := xml.Marshal(f)
+ fmt.Printf("%s\n", string(b))
+ }
+ fmt.Printf(`</body></opml>`)
+
+ 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(`<opml version="2.0"><head><title>neko feeds</title></head><body>`)
- fmt.Printf("\n")
- for _, f := range feeds {
- b, _ := xml.Marshal(f)
- fmt.Printf("%s\n", string(b))
- }
- fmt.Printf(`</body></opml>`)
+ 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 @@
+<html>
+<head>
+<title>neko exported feeds</title>
+</head>
+<body>
+<h1>neko exported feeds</h1>
+<ul>
+{{ range . }}
+<li><a href="{{.WebUrl}}">{{.Title}}</a> | <a href="{{.Url}}">xml</a></li>
+{{ end }}
+</ul>
+</body>
+</html> \ No newline at end of file