-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
244 lines (219 loc) · 6.14 KB
/
app.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package tinycast
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"html"
"html/template"
"log"
"net/http"
"net/url"
"strconv"
"time"
podgrabm "github.com/akhilrex/podgrab/model"
podgrabs "github.com/akhilrex/podgrab/service"
"github.com/patrickmn/go-cache"
"pipelined.dev/audio/mp3"
"github.com/beevik/etree"
"github.com/gin-gonic/gin"
"github.com/microcosm-cc/bluemonday"
)
// App is the core tinycast web app with handlers and state.
type App struct {
baseURL url.URL
cache *cache.Cache
apiKey string
}
func hashAPIKey(in string) string {
by := sha1.Sum([]byte(in))
return hex.EncodeToString(by[:])
}
// NewApp returns a new App based on configuration.
func NewApp(baseURL url.URL, apiKey string) *App {
return &App{
baseURL: baseURL,
cache: cache.New(5*time.Minute, 10*time.Minute),
apiKey: hashAPIKey(apiKey),
}
}
// Convert is a handler which re-encodes an audio stream to MP3 on the fly.
func (a *App) Convert(c *gin.Context) {
if !a.verifyAPIKey(c) {
c.Status(http.StatusForbidden)
return
}
var cfg ConversionConfig
var err error
if cfg, err = BindConversionConfig(c); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
//parsedUrl, err := url.Parse(cfg.Url)
//if err != nil {
// c.HTML(http.StatusInternalServerError, fmt.Sprintf("Failed to convert: %s", err), nil)
//}
c.Writer.Header().Add("Content-Type", "audio/mpeg")
//c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", path.Base(parsedUrl.Path)))
err = transform(c, cfg, c.Writer)
//if err != nil {
// c.HTML(http.StatusInternalServerError, fmt.Sprintf("Failed to convert: %s", err), nil)
//}
if err != nil {
log.Println("Err: ", err)
}
}
// ConversionConfig holds the desired conversion for an audio file.
type ConversionConfig struct {
URL string
BitRateMode BitRateMode
BitRate BitRate
ChannelMode mp3.ChannelMode
}
func (a *App) verifyAPIKey(c *gin.Context) bool {
return a.apiKey == c.Query("key")
}
// BindConversionConfig captures and validates a ConversionConfig passed in
// query parameters.
func BindConversionConfig(c *gin.Context) (ConversionConfig, error) {
var cfg ConversionConfig
var err error
if cfg.URL = c.Query("url"); cfg.URL == "" {
return cfg, fmt.Errorf("url required")
}
if cfg.BitRateMode, err = ParseBitRateMode(c.Query("bitRateMode")); err != nil {
return cfg, err
}
if cfg.BitRate, err = ParseBitRate(c.Query("bitRate")); err != nil {
return cfg, err
}
if cfg.ChannelMode, err = ParseChannelMode(c.Query("channelMode")); err != nil {
return cfg, err
}
return cfg, nil
}
// ToQueryValues translates a ConversionCongig to the same query parameters
// expected by BindConversionConfig.
func (cfg ConversionConfig) ToQueryValues() url.Values {
q := url.Values{}
q.Add("bitRateMode", string(cfg.BitRateMode))
q.Add("bitRate", cfg.BitRate.ToString())
q.Add("channelMode", cfg.ChannelMode.String())
return q
}
// Feed is a handler which replaces all download entries in a podcast feed with
// a URL handled by the Conversion handler.
func (a *App) Feed(c *gin.Context) {
if !a.verifyAPIKey(c) {
c.Status(http.StatusForbidden)
return
}
var cfg ConversionConfig
var err error
if cfg, err = BindConversionConfig(c); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
qr := cfg.ToQueryValues()
qr.Add("key", a.apiKey)
resp, err := http.Get(cfg.URL)
if err != nil {
log.Println(err)
c.Status(http.StatusInternalServerError)
return
}
defer resp.Body.Close()
doc := etree.NewDocument()
_, err = doc.ReadFrom(resp.Body)
if err != nil {
log.Println(err)
c.Status(http.StatusInternalServerError)
return
}
replace(doc, "//enclosure", a.baseURL, qr)
replace(doc, "//media:content", a.baseURL, qr)
for _, t := range doc.FindElements("//channel/title") {
t.SetText(fmt.Sprintf("(Tiny) %s", t.Text()))
}
for _, t := range doc.FindElements("//item/title") {
t.SetText(fmt.Sprintf("(Tiny) %s", t.Text()))
}
_, err = doc.WriteTo(c.Writer)
if err != nil {
log.Println("Failed to finish writing feed:", err)
c.Status(http.StatusInternalServerError)
return
}
}
func (a *App) search(query string) ([]*podgrabm.CommonSearchResultModel, error) {
cacheKey := fmt.Sprintf("s-%s", query)
cacheResult, found := a.cache.Get(cacheKey)
if found {
v, ok := cacheResult.([]*podgrabm.CommonSearchResultModel)
if ok {
return v, nil
}
}
searcher := new(podgrabs.PodcastIndexService)
results := searcher.Query(query)
// Cleanup the HTML in the podcast descriptions.
for _, r := range results {
r.Description = html.UnescapeString(sanitizeString(r.Description))
}
go func() {
a.cache.Set(cacheKey, results, cache.DefaultExpiration)
}()
return results, nil
}
var bmStrictPolicy = bluemonday.StrictPolicy()
func sanitizeString(input string) string {
return bmStrictPolicy.Sanitize(input)
}
// Home is a handler which allows users to search for podcasts to subscribe to.
func (a *App) Home(c *gin.Context) {
query := c.Query("q")
var results []*podgrabm.CommonSearchResultModel
var err error
var p Pagination
if query != "" {
results, err = a.search(query)
if err != nil {
log.Println("Failed search:", err)
}
var currentPage int64
if currentPage, err = strconv.ParseInt(c.Query("p"), 10, 32); err != nil {
currentPage = 0
}
if currentPage < 0 {
currentPage = 0
}
p = Pagination{
numItems: len(results),
itemsPerPage: 10,
curPage: int(currentPage),
}
if int(currentPage) > p.NumPages() {
p.curPage = 0
}
lastOffset := len(results)
if o := p.FirstItem() + p.itemsPerPage; o < lastOffset {
lastOffset = o
}
results = results[p.FirstItem():lastOffset]
}
podcastURL := a.baseURL
podcastURL.Scheme = "podcast"
podcastURL.Path = "/feed"
log.Println(podcastURL.String())
c.HTML(http.StatusOK, "main.tmpl", gin.H{
"title": "TinyCast",
"h1": "TinyCast",
"query": sanitizeString(query),
"searchResults": results,
"pagination": p,
"channelModes": ChannelModes,
"bitRateModes": BitRateModes,
"bitRates": BitRates,
"applePodcastUrl": template.URL(podcastURL.String()),
"apiKey": a.apiKey,
})
}