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

@@ -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