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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -14,11 +14,13 @@ import (
|
|||||||
ytd "github.com/kkdai/youtube/v2/downloader"
|
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]
|
_, ongoing := ongoingDownloads[url]
|
||||||
if ongoing {
|
if ongoing {
|
||||||
// return fmt.Errorf("Download %s is already ongoing", url)
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
ongoingDownloadsMutex.Lock()
|
ongoingDownloadsMutex.Lock()
|
||||||
@@ -32,14 +34,16 @@ func DownloadR(url string) error {
|
|||||||
|
|
||||||
log.Printf("Downloading %s", url)
|
log.Printf("Downloading %s", url)
|
||||||
|
|
||||||
err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
|
go func() {
|
||||||
if err != nil {
|
err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
|
||||||
Warning.Printf("Failed beeping with %+v", err)
|
if err != nil {
|
||||||
}
|
log.Printf("Failed beeping with %+v", err)
|
||||||
err = beeep.Alert("Download Started", url, "assets/information.png")
|
}
|
||||||
if err != nil {
|
err = beeep.Alert("Download Started", url, "assets/information.png")
|
||||||
Warning.Printf("Failed alerting with %+v", err)
|
if err != nil {
|
||||||
}
|
log.Printf("Failed alerting with %+v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
client := youtube.Client{}
|
client := youtube.Client{}
|
||||||
video, err := client.GetVideo(url)
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"github.com/lrstanley/go-ytdlp"
|
"github.com/lrstanley/go-ytdlp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
var dl = ytdlp.New().
|
var dl = ytdlp.New().
|
||||||
// FormatSort("bestvideo[ext=mp4]+bestaudio[ext=m4a]").
|
// FormatSort("bestvideo[ext=mp4]+bestaudio[ext=m4a]").
|
||||||
FormatSort("res,ext:mp4:m4a").
|
FormatSort("res,ext:mp4:m4a").
|
||||||
@@ -21,11 +20,13 @@ var dl = ytdlp.New().
|
|||||||
RecodeVideo("mp4").
|
RecodeVideo("mp4").
|
||||||
ConcurrentFragments(6)
|
ConcurrentFragments(6)
|
||||||
|
|
||||||
func Download(url string) error {
|
type YTDLPLibDownloader struct{}
|
||||||
|
|
||||||
|
func (d *YTDLPLibDownloader) Download(url string) error {
|
||||||
_, ongoing := ongoingDownloads[url]
|
_, ongoing := ongoingDownloads[url]
|
||||||
if ongoing {
|
if ongoing {
|
||||||
// return fmt.Errorf("Download %s is already ongoing", url)
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
ongoingDownloadsMutex.Lock()
|
ongoingDownloadsMutex.Lock()
|
||||||
@@ -36,11 +37,11 @@ func Download(url string) error {
|
|||||||
|
|
||||||
err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
|
err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
|
||||||
if err != nil {
|
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")
|
err = beeep.Alert("Download Started", url, "assets/information.png")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Warning.Printf("Failed alerting with %+v", err)
|
log.Printf("Failed alerting with %+v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = dl.Run(context.TODO(), url)
|
_, 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"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"main/downloaders"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -35,7 +36,7 @@ func init() {
|
|||||||
log.Lmicroseconds|log.Lshortfile)
|
log.Lmicroseconds|log.Lshortfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
const DOWNLOAD_WORKERS = 1
|
var downloader downloaders.Downloader = &downloaders.YTDLPLibDownloader{}
|
||||||
|
|
||||||
type DLHandler struct{}
|
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 {
|
if err != nil {
|
||||||
Error.Printf("Error downloading %s: %v", data.Link, err)
|
Error.Printf("Error downloading %s: %v", data.Link, err)
|
||||||
return err
|
return err
|
||||||
@@ -75,9 +76,15 @@ func (*DLHandler) HandleMessage(message *nsq.Message) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
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 := nsq.NewConfig()
|
||||||
config.MaxAttempts = 5
|
config.MaxAttempts = 5
|
||||||
config.MaxInFlight = DOWNLOAD_WORKERS
|
config.MaxInFlight = downloaders.DOWNLOAD_WORKERS
|
||||||
config.MsgTimeout = 10 * time.Second
|
config.MsgTimeout = 10 * time.Second
|
||||||
|
|
||||||
consumer, err := nsq.NewConsumer("ytdqueue", "dl", config)
|
consumer, err := nsq.NewConsumer("ytdqueue", "dl", config)
|
||||||
@@ -85,7 +92,7 @@ func main() {
|
|||||||
Error.Printf("Error creating consumer: %v", err)
|
Error.Printf("Error creating consumer: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for i := 0; i < DOWNLOAD_WORKERS; i++ {
|
for i := 0; i < downloaders.DOWNLOAD_WORKERS; i++ {
|
||||||
consumer.AddHandler(&DLHandler{})
|
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