From e0702a70437eea543aaa51264272dcbc3d652779 Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Sat, 7 Apr 2018 09:24:06 -0700 Subject: opml export --- crawler/crawler.go | 1 + main.go | 16 ++++++++++++++-- models/feed/feed.go | 21 +++++++++++++-------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/crawler/crawler.go b/crawler/crawler.go index 4871366..a29f814 100644 --- a/crawler/crawler.go +++ b/crawler/crawler.go @@ -93,6 +93,7 @@ func CrawlFeed(f *feed.Feed, ch chan<- string) { } f.Title = feed.Title + f.WebUrl = feed.Link f.Update() for _, i := range feed.Items { diff --git a/main.go b/main.go index 34bb869..b7aefb3 100644 --- a/main.go +++ b/main.go @@ -7,12 +7,13 @@ import ( "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 bool + var serve, update, verbose, printFeeds, opml bool var configFile, newFeed string flag.StringVarP(&configFile, "config", "c", "config.json", "`configuration` file") @@ -20,11 +21,12 @@ func main() { 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.Parse() // no command - if !update && !serve && !printFeeds && newFeed == "" { + if !update && !serve && !printFeeds && !opml && newFeed == "" { flag.Usage() return } @@ -52,4 +54,14 @@ func main() { 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(``) + } } diff --git a/models/feed/feed.go b/models/feed/feed.go index 70b939a..3d2473b 100644 --- a/models/feed/feed.go +++ b/models/feed/feed.go @@ -8,10 +8,14 @@ import ( ) type Feed struct { - Id int64 `json:"_id"` - Url string `json:"url"` - Title string `json:"title"` - // TODO: last_updated scan + Id int64 `json:"_id" xml:"-"` + Url string `json:"url" xml:"xmlUrl,attr"` + WebUrl string `json:"web_url" xml:"htmlUrl,attr"` + Title string `json:"title" xml:"text,attr"` + + // for OPML output purposes + XMLName string `json:"-" xml:"outline"` + Type string `json:"-" xml:"type,attr"` } func NewFeed(url string) error { @@ -33,7 +37,7 @@ func All() ([]*Feed, error) { func filter(where string) ([]*Feed, error) { // todo: add back in title - rows, err := models.DB.Query(`SELECT id, url, title + rows, err := models.DB.Query(`SELECT id, url, web_url, title FROM feed ` + where) if err != nil { return nil, err @@ -43,7 +47,8 @@ func filter(where string) ([]*Feed, error) { feeds := make([]*Feed, 0) for rows.Next() { f := new(Feed) - err := rows.Scan(&f.Id, &f.Url, &f.Title) + err := rows.Scan(&f.Id, &f.Url, &f.WebUrl, &f.Title) + f.Type = "rss" if err != nil { return nil, err } @@ -69,8 +74,8 @@ func (f *Feed) Update() { } models.DB.Query(`UPDATE feed - SET title=?, url=? - WHERE id=?`, f.Title, f.Url, f.Id) + SET title=?, url=?, web_url=? + WHERE id=?`, f.Title, f.Url, f.WebUrl, f.Id) } func (f *Feed) Delete() { -- cgit v1.2.3