diff --git a/main.go b/main.go index 9f19fa6..223b0c7 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ "github.com/PuerkitoBio/goquery" chi "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" - "github.com/google/uuid" "html" "io" "log" @@ -16,12 +15,11 @@ "os" "os/exec" "strings" - "sync" "time" ) func main() { - m := &sync.Map{} + requestChan := make(chan Request, 1000) r := chi.NewRouter() r.Use(middleware.Logger) @@ -47,32 +45,32 @@ w.Write(file) }) - r.Get("/session", func(w http.ResponseWriter, r *http.Request) { - m1 := make(map[string]Request) - - m.Range(func(key, value any) bool { - // Type assertion is required since Range returns 'any' types - k := key.(string) - v := value.(Request) - - m1[k] = v - - return true // Return true to continue iterating - }) - - data, err := json.Marshal(m1) - if err != nil { - http.Error(w, "Failed to submit request", http.StatusInternalServerError) - return - } - - _, err = w.Write(data) - if err != nil { - http.Error(w, "Failed to submit request", http.StatusInternalServerError) - return - } - w.WriteHeader(http.StatusOK) - }) + //r.Get("/session", func(w http.ResponseWriter, r *http.Request) { + // m1 := make(map[string]Request) + // + // m.Range(func(key, value any) bool { + // // Type assertion is required since Range returns 'any' types + // k := key.(string) + // v := value.(Request) + // + // m1[k] = v + // + // return true // Return true to continue iterating + // }) + // + // data, err := json.Marshal(m1) + // if err != nil { + // http.Error(w, "Failed to submit request", http.StatusInternalServerError) + // return + // } + // + // _, err = w.Write(data) + // if err != nil { + // http.Error(w, "Failed to submit request", http.StatusInternalServerError) + // return + // } + // w.WriteHeader(http.StatusOK) + //}) r.Post("/session", func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { @@ -85,23 +83,25 @@ name := r.FormValue("name") season := r.FormValue("season") - UUID, err := uuid.NewUUID() - if err != nil { - http.Error(w, "Failed to submit request", http.StatusInternalServerError) - return - } + //UUID, err := uuid.NewUUID() + //if err != nil { + // http.Error(w, "Failed to submit request", http.StatusInternalServerError) + // return + //} - m.Store(UUID.String(), Request{ + requestChan <- Request{ URL: u, Name: name, Season: season, - }) + } http.Redirect(w, r, "/success", http.StatusTemporaryRedirect) }) - go processRequest(m) + go processRequest(requestChan) http.ListenAndServe(":3000", r) + + close(requestChan) } type Request struct { @@ -110,19 +110,12 @@ Season string `json:"season"` } -func processRequest(m *sync.Map) { +func processRequest(requestChan chan Request) { for { - m.Range(func(key, value any) bool { - // Type assertion is required since Range returns 'any' types - //k := key.(int) - v := value.(Request) + v := <-requestChan - Download(v) + Download(v) - return true // Return true to continue iterating - }) - - m.Clear() time.Sleep(time.Second * 30) } } @@ -166,7 +159,7 @@ playEchoVideoSourceUrl = strings.Replace(playEchoVideoSourceUrl, "hlsx3cdn.burntburst45.store", "hlsx4cdn.burntburst45.store", 1) - name := "storage/Download/" + fmt.Sprintf("%s/%s/%s - %d", request.Name, request.Season, request.Name, i) + ".%(ext)s" + name := "storage/Download/" + fmt.Sprintf("%s/%s/%s - %02d", request.Name, request.Season, request.Name, i) + ".%(ext)s" cmd := exec.Command("./yt-dlp_linux", "-o", name, "--referer", "https://play.echovideo.ru/", "--add-header", "Origin: https://play.echovideo.ru", playEchoVideoSourceUrl)