Refactor downloaders to separate package to utilize an interface
This commit is contained in:
5
downloader/downloaders/downloader.go
Normal file
5
downloader/downloaders/downloader.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package downloaders
|
||||
|
||||
type Downloader interface {
|
||||
Download(url string) error
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package downloaders
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -14,11 +14,13 @@ import (
|
||||
ytd "github.com/kkdai/youtube/v2/downloader"
|
||||
)
|
||||
|
||||
func DownloadR(url string) error {
|
||||
type KidaiDownloader struct{}
|
||||
|
||||
func (d *KidaiDownloader) Download(url string) error {
|
||||
_, ongoing := ongoingDownloads[url]
|
||||
if ongoing {
|
||||
// return fmt.Errorf("Download %s is already ongoing", url)
|
||||
Warning.Printf("Download %s is already ongoing", url)
|
||||
log.Printf("Download %s is already ongoing", url)
|
||||
return nil
|
||||
}
|
||||
ongoingDownloadsMutex.Lock()
|
||||
@@ -32,14 +34,16 @@ func DownloadR(url string) error {
|
||||
|
||||
log.Printf("Downloading %s", url)
|
||||
|
||||
go func() {
|
||||
err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
|
||||
if err != nil {
|
||||
Warning.Printf("Failed beeping with %+v", err)
|
||||
log.Printf("Failed beeping with %+v", err)
|
||||
}
|
||||
err = beeep.Alert("Download Started", url, "assets/information.png")
|
||||
if err != nil {
|
||||
Warning.Printf("Failed alerting with %+v", err)
|
||||
log.Printf("Failed alerting with %+v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
client := youtube.Client{}
|
||||
video, err := client.GetVideo(url)
|
11
downloader/downloaders/utils.go
Normal file
11
downloader/downloaders/utils.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package downloaders
|
||||
|
||||
import "sync"
|
||||
|
||||
//const OUTPUT_DIR = "C:/Users/Administrator/ytdlpVideos"
|
||||
const OUTPUT_DIR = "."
|
||||
|
||||
var ongoingDownloads = make(map[string]struct{})
|
||||
var ongoingDownloadsMutex = &sync.Mutex{}
|
||||
|
||||
const DOWNLOAD_WORKERS = 1
|
@@ -1,4 +1,4 @@
|
||||
package main
|
||||
package downloaders
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/lrstanley/go-ytdlp"
|
||||
)
|
||||
|
||||
|
||||
var dl = ytdlp.New().
|
||||
// FormatSort("bestvideo[ext=mp4]+bestaudio[ext=m4a]").
|
||||
FormatSort("res,ext:mp4:m4a").
|
||||
@@ -21,11 +20,13 @@ var dl = ytdlp.New().
|
||||
RecodeVideo("mp4").
|
||||
ConcurrentFragments(6)
|
||||
|
||||
func Download(url string) error {
|
||||
type YTDLPLibDownloader struct{}
|
||||
|
||||
func (d *YTDLPLibDownloader) Download(url string) error {
|
||||
_, ongoing := ongoingDownloads[url]
|
||||
if ongoing {
|
||||
// return fmt.Errorf("Download %s is already ongoing", url)
|
||||
Warning.Printf("Download %s is already ongoing", url)
|
||||
log.Printf("Download %s is already ongoing", url)
|
||||
return nil
|
||||
}
|
||||
ongoingDownloadsMutex.Lock()
|
||||
@@ -36,11 +37,11 @@ func Download(url string) error {
|
||||
|
||||
err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
|
||||
if err != nil {
|
||||
Warning.Printf("Failed beeping with %+v", err)
|
||||
log.Printf("Failed beeping with %+v", err)
|
||||
}
|
||||
err = beeep.Alert("Download Started", url, "assets/information.png")
|
||||
if err != nil {
|
||||
Warning.Printf("Failed alerting with %+v", err)
|
||||
log.Printf("Failed alerting with %+v", err)
|
||||
}
|
||||
|
||||
_, err = dl.Run(context.TODO(), url)
|
1
downloader/downloaders/ytdlp-raw.go
Normal file
1
downloader/downloaders/ytdlp-raw.go
Normal file
@@ -0,0 +1 @@
|
||||
package downloaders
|
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"main/downloaders"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
@@ -35,7 +36,7 @@ func init() {
|
||||
log.Lmicroseconds|log.Lshortfile)
|
||||
}
|
||||
|
||||
const DOWNLOAD_WORKERS = 1
|
||||
var downloader downloaders.Downloader = &downloaders.YTDLPLibDownloader{}
|
||||
|
||||
type DLHandler struct{}
|
||||
|
||||
@@ -65,7 +66,7 @@ func (*DLHandler) HandleMessage(message *nsq.Message) error {
|
||||
}
|
||||
}()
|
||||
|
||||
err = DownloadR(data.Link)
|
||||
err = downloader.Download(data.Link)
|
||||
if err != nil {
|
||||
Error.Printf("Error downloading %s: %v", data.Link, err)
|
||||
return err
|
||||
@@ -75,9 +76,15 @@ func (*DLHandler) HandleMessage(message *nsq.Message) error {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// err := DownloadR("https://www.youtube.com/watch?v=QnvGX0C-LKE")
|
||||
// if err != nil {
|
||||
// Error.Printf("Error downloading: %v", err)
|
||||
// }
|
||||
// return
|
||||
|
||||
config := nsq.NewConfig()
|
||||
config.MaxAttempts = 5
|
||||
config.MaxInFlight = DOWNLOAD_WORKERS
|
||||
config.MaxInFlight = downloaders.DOWNLOAD_WORKERS
|
||||
config.MsgTimeout = 10 * time.Second
|
||||
|
||||
consumer, err := nsq.NewConsumer("ytdqueue", "dl", config)
|
||||
@@ -85,7 +92,7 @@ func main() {
|
||||
Error.Printf("Error creating consumer: %v", err)
|
||||
return
|
||||
}
|
||||
for i := 0; i < DOWNLOAD_WORKERS; i++ {
|
||||
for i := 0; i < downloaders.DOWNLOAD_WORKERS; i++ {
|
||||
consumer.AddHandler(&DLHandler{})
|
||||
}
|
||||
|
||||
|
@@ -1,8 +0,0 @@
|
||||
package main
|
||||
|
||||
import "sync"
|
||||
|
||||
const OUTPUT_DIR = "C:/Users/Administrator/ytdlpVideos"
|
||||
|
||||
var ongoingDownloads = make(map[string]struct{})
|
||||
var ongoingDownloadsMutex = &sync.Mutex{}
|
Reference in New Issue
Block a user