Implement discord webhook notifications
This commit is contained in:
44
downloader/discordNotifier.go
Normal file
44
downloader/discordNotifier.go
Normal 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
|
||||
}
|
@@ -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
|
||||
|
Reference in New Issue
Block a user