From 0b87a1137f330a36419030a4e82363ed36563a23 Mon Sep 17 00:00:00 2001 From: davy Date: Fri, 28 Jun 2019 22:50:59 -0500 Subject: added feeds --- pkg/app/app.go | 43 +++++++++++++++++++++++++++++++++++++++++++ pkg/app/config.go | 16 ++++++++++++++++ 2 files changed, 59 insertions(+) (limited to 'pkg/app') diff --git a/pkg/app/app.go b/pkg/app/app.go index 665b602..d13a888 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -5,8 +5,12 @@ import ( "log" "net" "net/http" + "net/url" + "path" + "time" "github.com/fsnotify/fsnotify" + "github.com/gorilla/feeds" "github.com/gorilla/mux" "github.com/wybiral/tube/pkg/media" ) @@ -44,6 +48,7 @@ func NewApp(cfg *Config) (*App, error) { r.HandleFunc("/v/{id}.mp4", a.videoHandler).Methods("GET") r.HandleFunc("/t/{id}", a.thumbHandler).Methods("GET") r.HandleFunc("/v/{id}", a.pageHandler).Methods("GET") + r.HandleFunc("/feed.xml", a.rssHandler).Methods("GET") fsHandler := http.StripPrefix( "/static/", http.FileServer(http.Dir("./static/")), @@ -159,3 +164,41 @@ func (a *App) thumbHandler(w http.ResponseWriter, r *http.Request) { w.Write(m.Thumb) } } + +func (a *App) rssHandler(w http.ResponseWriter, r *http.Request) { + cfg := a.Config.Feed + now := time.Now() + f := &feeds.Feed{ + Title: cfg.Title, + Link: &feeds.Link{Href: cfg.Link}, + Description: cfg.Description, + Author: &feeds.Author{ + Name: cfg.Author.Name, + Email: cfg.Author.Email, + }, + Created: now, + Copyright: cfg.Copyright, + } + for _, v := range a.Library.Playlist() { + u, err := url.Parse(cfg.ExternalURL) + if err != nil { + return + } + u.Path = path.Join(u.Path, "v", v.ID) + id := u.String() + f.Items = append(f.Items, &feeds.Item{ + Id: id, + Title: v.Title, + Link: &feeds.Link{Href: id}, + Description: v.Description, + Author: &feeds.Author{ + Name: cfg.Author.Name, + Email: cfg.Author.Email, + }, + Created: v.Timestamp, + }) + } + w.Header().Set("Cache-Control", "public, max-age=7776000") + w.Header().Set("Content-Type", "text/xml") + f.WriteRss(w) +} diff --git a/pkg/app/config.go b/pkg/app/config.go index c4ea170..12cf21d 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -8,6 +8,7 @@ import ( type Config struct { LibraryPath string `json:"library"` Server *ServerConfig `json:"server"` + Feed *FeedConfig `json:"feed"` } type ServerConfig struct { @@ -15,6 +16,18 @@ type ServerConfig struct { Port int `json:"port"` } +type FeedConfig struct { + ExternalURL string `json:"external_url"` + Title string `json:"title"` + Link string `json:"link"` + Description string `json:"description"` + Author struct { + Name string `json:"name"` + Email string `json:"email"` + } `json:"author"` + Copyright string `json:"copyright"` +} + func DefaultConfig() *Config { return &Config{ LibraryPath: "videos", @@ -22,6 +35,9 @@ func DefaultConfig() *Config { Host: "127.0.0.1", Port: 0, }, + Feed: &FeedConfig{ + ExternalURL: "http://localhost", + }, } } -- cgit v1.2.3