summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Hurst <ark@mansus.space>2024-09-11 00:57:26 +0200
committerJulian Hurst <ark@mansus.space>2024-09-11 00:57:26 +0200
commitd71de8ab2490081c184dd8a30cc8c8d2cf718286 (patch)
tree7ac860bdaa6b717571dc3c86ee2c51d857389a80
parentcbd9317993b188ef142aad11b64a235206369aa0 (diff)
downloadgrimtube-d71de8ab2490081c184dd8a30cc8c8d2cf718286.tar.gz
Handle downloading with yt-dlp and serving videonative_stream
-rw-r--r--grimtube.go52
-rw-r--r--templates/search.html6
-rw-r--r--templates/vid.html6
3 files changed, 61 insertions, 3 deletions
diff --git a/grimtube.go b/grimtube.go
index 9f8061b..5ef91de 100644
--- a/grimtube.go
+++ b/grimtube.go
@@ -5,10 +5,11 @@ import (
"text/template"
"net/http"
"fmt"
- //"io"
+ "io"
"log"
"strconv"
"path"
+ "os/exec"
"git.sr.ht/~ark/ytparser"
)
@@ -165,6 +166,53 @@ func favicon(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "favicon.ico")
}
+func nativeStreamb(w http.ResponseWriter, r *http.Request) {
+ values := r.URL.Query()
+ if !values.Has("url") {
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ cmd := exec.Command("yt-dlp", "-o", "/tmp/nativestreamgrimtube.%(ext)s",values.Get("url"))
+ stderr, err := cmd.StderrPipe()
+ if err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ if err := cmd.Start(); err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ io.Copy(w, stderr)
+}
+
+func nativeStream(w http.ResponseWriter, r *http.Request) {
+ values := r.URL.Query()
+ if !values.Has("url") {
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ cmd := exec.Command("yt-dlp", "-o", "/tmp/nativestreamgrimtube", "--force-overwrites", values.Get("url"))
+ if err := cmd.Run(); err != nil {
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+ data := struct {
+ Name string
+ }{
+ "nativestreamgrimtube.webm",
+ }
+ serve(w, "templates/vid.html", data)
+}
+
+func stream(w http.ResponseWriter, r *http.Request) {
+ values := r.URL.Query()
+ if !values.Has("name") {
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+ http.ServeFile(w, r, "/tmp/" + values.Get("name"))
+}
+
func main() {
port := flag.Int("p", 8080, "The port to bind to.")
dir := flag.String("d", "", "Location of the templates and static resources.")
@@ -180,6 +228,8 @@ func main() {
http.HandleFunc("/", index)
http.HandleFunc("/search", search)
http.HandleFunc("/embed", embed)
+ http.HandleFunc("/native", nativeStream)
+ http.HandleFunc("/stream", stream)
log.Printf("Listening on port %d\n", *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil))
diff --git a/templates/search.html b/templates/search.html
index 6a9ce28..30e96b9 100644
--- a/templates/search.html
+++ b/templates/search.html
@@ -26,10 +26,12 @@
{{range .Items}}
<tr>
<td>
- <a href="/embed?id={{.Id}}&url={{.Url}}"><img width=240 src="{{.ThumbUrl}}"></a>
+ <!--<a href="/embed?id={{.Id}}&url={{.Url}}"><img width=240 src="{{.ThumbUrl}}"></a>-->
+ <a href="/native?url={{.Url}}"><img width=240 src="{{.ThumbUrl}}"></a>
</td>
<td>
- <a href="/embed?id={{.Id}}&url={{.Url}}">{{.Title}}</a>
+ <!--<a href="/embed?id={{.Id}}&url={{.Url}}">{{.Title}}</a>-->
+ <a href="/native?url={{.Url}}">{{.Title}}</a>
<br/>
<a href="{{.ChannelUrl}}">{{.ChannelTitle}}</a> | <a href="{{.AtomUrl}}">Atom feed</a>
<br/>
diff --git a/templates/vid.html b/templates/vid.html
new file mode 100644
index 0000000..6383e2b
--- /dev/null
+++ b/templates/vid.html
@@ -0,0 +1,6 @@
+{{define "title"}}Index{{end}}
+{{define "content"}}
+ <video width="100%" height="100%" controls>
+ <source src="/stream?name={{.Name}}">
+ </video>
+{{end}}