-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_listen.go
68 lines (54 loc) 路 1.67 KB
/
get_listen.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
package rest
import (
"github.com/boggydigital/yet/rest/view_models"
"github.com/boggydigital/yet/yeti"
"net/http"
"strings"
)
func GetListen(w http.ResponseWriter, r *http.Request) {
// GET /watch?v&t
var err error
rdx, err = rdx.RefreshReader()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
v := r.URL.Query().Get("v")
t := r.URL.Query().Get("t")
if v == "" {
http.Redirect(w, r, "/list", http.StatusPermanentRedirect)
return
}
// resolve full YouTube URL to just video-id, as needed
if strings.Contains(v, "?") {
if videoIds, err := yeti.ParseVideoIds(v); err != nil {
// one more attempt - redirect to playlist page if we've got a valid playlist
if playlistIds, err := yeti.ParsePlaylistIds(v); err == nil && len(playlistIds) > 0 {
http.Redirect(w, r, "/playlist?list="+playlistIds[0], http.StatusPermanentRedirect)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if len(videoIds) > 0 {
http.Redirect(w, r, "/watch?v="+videoIds[0], http.StatusPermanentRedirect)
return
}
}
// iOS insists on inserting a space on paste
v = strings.TrimSpace(v)
videoId := ""
if videoIds, err := yeti.ParseVideoIds(v); err == nil && len(videoIds) > 0 {
videoId = videoIds[0]
} else {
videoId = v
}
w.Header().Set("Content-Type", "text/html")
wvm, err := view_models.GetWatchViewModel(videoId, t, rdx, true)
if err != nil {
http.Redirect(w, r, "/video_error?v="+videoId+"&err="+err.Error(), http.StatusTemporaryRedirect)
return
}
if err := tmpl.ExecuteTemplate(w, "watch", wvm); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}