diff options
| author | Adam Mathes <adam@trenchant.org> | 2018-04-08 08:51:26 -0700 | 
|---|---|---|
| committer | Adam Mathes <adam@trenchant.org> | 2018-04-08 08:51:26 -0700 | 
| commit | cd1509967d7dafbf2d1a051206e368bd3aa5e179 (patch) | |
| tree | dbcf7629eaaa3e04409c1c949072ebaf06d9436e | |
| parent | e0702a70437eea543aaa51264272dcbc3d652779 (diff) | |
| download | neko-cd1509967d7dafbf2d1a051206e368bd3aa5e179.tar.gz neko-cd1509967d7dafbf2d1a051206e368bd3aa5e179.tar.bz2 neko-cd1509967d7dafbf2d1a051206e368bd3aa5e179.zip  | |
refactor exporter, add json/opml export formats
| -rw-r--r-- | exporter/exporter.go | 47 | ||||
| -rw-r--r-- | main.go | 32 | ||||
| -rw-r--r-- | static/feeds.tmpl | 13 | 
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) +		} +	} +} @@ -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  | 
