diff options
| author | davy wybiral <davy.wybiral@gmail.com> | 2019-08-08 06:04:39 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-08 06:04:39 -0500 |
| commit | 6b09ccd9d885a3230a197f7d42fa1fcc760a25ef (patch) | |
| tree | 39bbbb55e489989c0c89ed9af2e45d7eaf6d1fd1 /pkg/app/feed.go | |
| parent | 02762c812ccd692d61328bc76029b9cc01ed8bb4 (diff) | |
cache rss feed (#19)v0.2.0
Diffstat (limited to 'pkg/app/feed.go')
| -rw-r--r-- | pkg/app/feed.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/pkg/app/feed.go b/pkg/app/feed.go new file mode 100644 index 0000000..e4c1ce2 --- /dev/null +++ b/pkg/app/feed.go @@ -0,0 +1,77 @@ +package app + +import ( + "fmt" + "net/url" + "os" + "path" + "strconv" + "time" + + "github.com/wybiral/feeds" +) + +// buildFeed creates RSS feed attribute for App based on Library contents. +func buildFeed(a *App) { + 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, + } + var externalURL string + if len(cfg.ExternalURL) > 0 { + externalURL = cfg.ExternalURL + } else if a.Tor != nil { + onion, err := a.Tor.OnionKey.Onion() + if err != nil { + return + } + externalURL = fmt.Sprintf("http://%s.onion", onion.ServiceID) + } else { + hostname, err := os.Hostname() + if err != nil { + host := a.Config.Server.Host + port := a.Config.Server.Port + externalURL = fmt.Sprintf("http://%s:%d", host, port) + } else { + externalURL = fmt.Sprintf("http://%s", hostname) + } + } + for _, v := range a.Library.Playlist() { + u, err := url.Parse(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, + Enclosure: &feeds.Enclosure{ + Url: id + ".mp4", + Length: strconv.FormatInt(v.Size, 10), + Type: "video/mp4", + }, + Author: &feeds.Author{ + Name: cfg.Author.Name, + Email: cfg.Author.Email, + }, + Created: v.Timestamp, + }) + } + feed, err := f.ToRss() + if err != nil { + return + } + a.Feed = []byte(feed) +} |
