-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.go
144 lines (117 loc) · 3.25 KB
/
views.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"html/template"
"io"
"io/fs"
"log"
"strconv"
"strings"
"time"
"github.com/andrewslotin/youcast/assets"
"github.com/eduncan911/podcast"
)
// DownloadablePodcastItem is a podcast item that is ready to be downloaded.
type DownloadablePodcastItem struct {
PodcastItem
MediaURL string
}
// Feed contains data for a podcast feed.
type Feed struct {
URL, IconURL string
Title, Description string
PubDate time.Time
Items []DownloadablePodcastItem
}
// Templates contains parsed templates.
var Templates = ParseTemplates(assets.Templates)
// ParseTemplates parses templates from the given file system.
func ParseTemplates(fs fs.FS) *template.Template {
return template.Must(template.New("").
Funcs(template.FuncMap{
"stripScheme": func(s string) string {
if ind := strings.Index(s, "://"); ind > -1 {
return s[ind+3:]
}
return s
},
"formatDuration": func(d time.Duration) string {
d = d.Round(time.Second)
var s string
if d >= 1*time.Hour {
s = strconv.Itoa(int(d/time.Hour)) + ":"
d -= (d / time.Hour) * time.Hour
}
return s + fmt.Sprintf("%02d:%02d", int(d/time.Minute), int(d%time.Minute/time.Second))
},
}).
ParseFS(fs, "*.html.tmpl"))
}
// HTMLRenderer renders a podcast feed as HTML.
type HTMLRenderer struct {
Template *template.Template
}
// ContentType returns the content type of the rendered feed.
func (HTMLRenderer) ContentType() string {
return "text/html; charset=utf-8"
}
// Render renders the feed to the given writer.
func (r HTMLRenderer) Render(w io.Writer, feed Feed) error {
return r.Template.Execute(w, feed)
}
// AtomRenderer renders a podcast feed as Atom.
type AtomRenderer struct{}
// ContentType returns the content type of the rendered feed.
func (AtomRenderer) ContentType() string {
return "application/xml"
}
// Render renders the feed to the given writer.
func (AtomRenderer) Render(w io.Writer, feed Feed) error {
var pubDate *time.Time
if !feed.PubDate.IsZero() {
pubDate = &feed.PubDate
}
p := podcast.New(feed.Title, feed.URL, feed.Description, pubDate, nil)
p.AddImage(feed.IconURL)
for _, it := range feed.Items {
if !it.Playable() {
continue // skip incomplete items
}
item := podcast.Item{
Title: it.Title,
Author: &podcast.Author{
Name: it.Author,
Email: "[email protected]",
},
Description: itemDescription(it),
Link: it.OriginalURL,
}
item.AddEnclosure(it.MediaURL, mimeTypeToEnclosureType(it.MIMEType), int64(it.ContentLength))
item.AddDuration(int64(it.Duration / time.Second))
item.AddPubDate(&it.AddedAt)
item.AddSummary(itemSummary(it))
if _, err := p.AddItem(item); err != nil {
log.Printf("failed to add %s: %s", it.OriginalURL, err)
}
}
return p.Encode(w)
}
func itemDescription(p DownloadablePodcastItem) string {
desc := p.Type.String()
if p.Author != "" {
desc += ": " + p.Author
}
return desc
}
func itemSummary(p DownloadablePodcastItem) string {
var buf strings.Builder
switch p.Type {
case YouTubeItem, TelegramItem:
buf.WriteString(`<a href="` + p.OriginalURL + `">` + p.Author + `</a>` + "\n\n")
case UploadedItem:
}
if p.Body != "" && p.Body != p.Title {
buf.WriteString(p.Body)
}
return strings.TrimSpace(buf.String())
}