feat: Working download with mp3-convx

This commit is contained in:
HeshamTB 2023-09-11 02:46:14 +03:00
parent fb91a431a5
commit 598fd51412
5 changed files with 98 additions and 7 deletions

70
main.go
View File

@ -3,6 +3,8 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"html/template"
"io"
"log" "log"
"net/http" "net/http"
"time" "time"
@ -13,6 +15,12 @@ const (
DEFAULT_HTTPS_PORT = "4433" DEFAULT_HTTPS_PORT = "4433"
) )
type DownloadFormats struct {
VideoRes string
videoOnly bool
audioOnly bool
}
type apiMessageResponse struct { type apiMessageResponse struct {
Message string Message string
} }
@ -53,6 +61,7 @@ func NewLogger(handler http.Handler) *Logger {
func writeJSONResponse(w http.ResponseWriter, s string) http.ResponseWriter { func writeJSONResponse(w http.ResponseWriter, s string) http.ResponseWriter {
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
w.Header().Set("Content-Type", "application/json")
jsonResp, err := json.Marshal(apiMessageResponse{Message: s}) jsonResp, err := json.Marshal(apiMessageResponse{Message: s})
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
@ -75,18 +84,71 @@ func main() {
handler.HandleFunc( handler.HandleFunc(
"/download", "/download",
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
userURL := r.URL.Query().Get("url") if r.Method != "POST" {
w.Header().Set("Content-Type", "application/json") w.WriteHeader(400)
return
}
err := r.ParseForm()
if err != nil {
w.WriteHeader(400)
return
}
userURL := r.FormValue("URL")
if userURL == "" { if userURL == "" {
w = writeJSONResponse(w, "Provide URL as query") w = writeJSONResponse(w, "Provide URL as query")
return return
} }
w = writeJSONResponse(w, fmt.Sprintf("You sent %s ", userURL))}, // Get URL from convx
req, err := http.Get(fmt.Sprintf("http://localhost:80?url=%s", userURL))
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
return
}
if req.StatusCode != 200 {
log.Printf("Got %v from convx\n", req)
w.WriteHeader(500)
return
}
body, err := io.ReadAll(req.Body)
if err != nil {
w.WriteHeader(500)
log.Printf("Error while reading convx response body. \n%v", err.Error())
return
}
downloadURL := string(body)
log.Println("URL from convx", downloadURL)
// Serve Button Template
tmpl := template.Must(template.ParseFiles("templates/download-result.html"))
err = tmpl.Execute(w, downloadURL)
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
return
}
},
) )
handler.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.ParseFiles("templates/download.html"))
formats := []DownloadFormats{}
formats = append(formats, DownloadFormats{
VideoRes: "720p",
audioOnly: false,
videoOnly: false,
})
err := tmpl.Execute(w, formats)
if err != nil {
log.Println(err.Error())
w.WriteHeader(500)
return
}
})
wrappedHandler := NewLogger(handler) wrappedHandler := NewLogger(handler)
srv := http.Server{ srv := http.Server{
ReadTimeout: 3 * time.Second, ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,
Addr: ":" + DEFAULT_HTTP_PORT, Addr: ":" + DEFAULT_HTTP_PORT,
Handler: wrappedHandler, Handler: wrappedHandler,

View File

@ -0,0 +1 @@
<a href="{{ . }}" rel="nofollow" download>Download</a>

30
templates/download.html Normal file
View File

@ -0,0 +1,30 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
<script src="https://unpkg.com/htmx.org@1.9.5"></script>
<title>YT Download</title>
</head>
<main class="container">
<form hx-post="/download" hx-target="#get-btn" hx-swap="outerHTML" hx-indicator="#progress">
<label for="URL">Link</label>
<div class="grid">
<input type="url" id="URL" name="URL" placeholder="Video Link" required>
<!-- Select -->
<select required>
<option value="" disabled selected>Format</option>
{{ range . }} <option>{{ .VideoRes }}</option>{{ end }}
</select>
</div>
<button id="get-btn">Get
<!-- <a class="htmx-indicator" href="#" aria-busy="true"></a> -->
</button>
</form>
<progress class="htmx-indicator" id="progress"></progress>
</main>
<footer class="container">
<small>By cronos</small>
</footer>
</html>

View File

@ -12,7 +12,7 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css" /> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css" />
<!-- Custom styles for this example --> <!-- Custom styles for this example -->
<link rel="stylesheet" href="custom.css" /> <!-- <link rel="stylesheet" href="custom.css" />-->
</head> </head>
<body> <body>
@ -85,8 +85,6 @@
</footer> </footer>
<!-- ./ Footer --> <!-- ./ Footer -->
<!-- Minimal theme switcher -->
<script src="js/minimal-theme-switcher.js"></script>
</body> </body>
</html> </html>