From 00ed4e9fc1d333c68709c17a0cce8fff95975454 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 13 Apr 2025 16:24:40 +0200 Subject: [PATCH] Implement discord webhook notifications --- downloader/discordNotifier.go | 44 +++++++++++++++++++++++++++++++++++ downloader/main.go | 37 ++++++++++++++++++++++------- 2 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 downloader/discordNotifier.go diff --git a/downloader/discordNotifier.go b/downloader/discordNotifier.go new file mode 100644 index 0000000..fa18ab7 --- /dev/null +++ b/downloader/discordNotifier.go @@ -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 +} diff --git a/downloader/main.go b/downloader/main.go index 8cac427..dd1e4b2 100644 --- a/downloader/main.go +++ b/downloader/main.go @@ -6,11 +6,11 @@ import ( "fmt" "io" "log" - "ytdl/downloaders" "os" "os/signal" "syscall" "time" + "ytdl/downloaders" "github.com/nsqio/go-nsq" ) @@ -36,7 +36,7 @@ func init() { 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.YTDLPLibDownloader{} @@ -44,10 +44,21 @@ type DLHandler struct{} func (*DLHandler) HandleMessage(message *nsq.Message) error { 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{} - err := json.Unmarshal(message.Body, &data) + err = json.Unmarshal(message.Body, &data) if err != nil { 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 } @@ -71,18 +82,28 @@ func (*DLHandler) HandleMessage(message *nsq.Message) error { err = downloader.Download(data.Link) if err != nil { 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 } message.Finish() + err = NotifyDiscord(fmt.Sprintf("Downloaded %s", data.Link)) + if err != nil { + Error.Printf("Error notifying discord: %v", err) + return err + } return nil } func main() { -// err := downloader.Download("https://www.youtube.com/watch?v=SiKjprtiPaw") -// if err != nil { -// Error.Printf("Error downloading: %v", err) -// } -// return + // err := downloader.Download("https://www.youtube.com/watch?v=SiKjprtiPaw") + // if err != nil { + // Error.Printf("Error downloading: %v", err) + // } + // return config := nsq.NewConfig() config.MaxAttempts = 5