summaryrefslogtreecommitdiff
path: root/app/feed.go
blob: 4dd7813a97a05454a4799444186b232a6f90e3f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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 {
		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)
}