Implement discord webhook notifications

This commit is contained in:
2025-04-13 16:24:40 +02:00
parent c859499676
commit 00ed4e9fc1
2 changed files with 73 additions and 8 deletions

View File

@@ -0,0 +1,44 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
func NotifyDiscord(message string) error {
webhookURL := os.Getenv("YTDL_DISCORD_WEBHOOK_URL")
if webhookURL == "" {
return fmt.Errorf("error notifying discord: webhook URL is not set in environment variables")
}
jsonData := map[string]string{"content": message}
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
return fmt.Errorf("error notifying discord: error marshalling JSON: %v", err)
}
req, err := http.NewRequest("POST", webhookURL, bytes.NewBuffer(jsonBytes))
if err != nil {
return fmt.Errorf("error notifying discord: error creating request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error notifying discord: error sending request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error notifying discord: error reading response body: %v", err)
}
log.Printf("Response from Discord: %s", string(body))
return nil
}

View File

@@ -6,11 +6,11 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"ytdl/downloaders"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"time" "time"
"ytdl/downloaders"
"github.com/nsqio/go-nsq" "github.com/nsqio/go-nsq"
) )
@@ -36,7 +36,7 @@ func init() {
log.Lmicroseconds|log.Lshortfile) log.Lmicroseconds|log.Lshortfile)
} }
//var downloader downloaders.Downloader = &downloaders.YTDLPRawDownloader{} // var downloader downloaders.Downloader = &downloaders.YTDLPRawDownloader{}
// var downloader downloaders.Downloader = &downloaders.KidaiDownloader{} // var downloader downloaders.Downloader = &downloaders.KidaiDownloader{}
var downloader downloaders.Downloader = &downloaders.YTDLPLibDownloader{} var downloader downloaders.Downloader = &downloaders.YTDLPLibDownloader{}
@@ -44,10 +44,21 @@ type DLHandler struct{}
func (*DLHandler) HandleMessage(message *nsq.Message) error { func (*DLHandler) HandleMessage(message *nsq.Message) error {
log.Printf("Received message '%s' with %d attempts", message.Body, message.Attempts) log.Printf("Received message '%s' with %d attempts", message.Body, message.Attempts)
err := NotifyDiscord(fmt.Sprintf("Received message '%s' with %d attempts", message.Body, message.Attempts))
if err != nil {
Error.Printf("Error notifying discord: %v", err)
return err
}
data := DownloadRequest{} data := DownloadRequest{}
err := json.Unmarshal(message.Body, &data) err = json.Unmarshal(message.Body, &data)
if err != nil { if err != nil {
Error.Printf("Error unmarshalling message: %v", err) Error.Printf("Error unmarshalling message: %v", err)
err = NotifyDiscord(fmt.Sprintf("Error unmarshalling message: %v", err))
if err != nil {
Error.Printf("Error notifying discord: %v", err)
return err
}
return err return err
} }
@@ -71,18 +82,28 @@ func (*DLHandler) HandleMessage(message *nsq.Message) error {
err = downloader.Download(data.Link) err = downloader.Download(data.Link)
if err != nil { if err != nil {
Error.Printf("Error downloading %s: %v", data.Link, err) Error.Printf("Error downloading %s: %v", data.Link, err)
err = NotifyDiscord(fmt.Sprintf("Error downloading %s: %v", data.Link, err))
if err != nil {
Error.Printf("Error notifying discord: %v", err)
return err
}
return err return err
} }
message.Finish() message.Finish()
err = NotifyDiscord(fmt.Sprintf("Downloaded %s", data.Link))
if err != nil {
Error.Printf("Error notifying discord: %v", err)
return err
}
return nil return nil
} }
func main() { func main() {
// err := downloader.Download("https://www.youtube.com/watch?v=SiKjprtiPaw") // err := downloader.Download("https://www.youtube.com/watch?v=SiKjprtiPaw")
// if err != nil { // if err != nil {
// Error.Printf("Error downloading: %v", err) // Error.Printf("Error downloading: %v", err)
// } // }
// return // return
config := nsq.NewConfig() config := nsq.NewConfig()
config.MaxAttempts = 5 config.MaxAttempts = 5