76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/gen2brain/beeep"
|
|
"github.com/kkdai/youtube/v2"
|
|
ytd "github.com/kkdai/youtube/v2/downloader"
|
|
)
|
|
|
|
func DownloadR(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)
|
|
return nil
|
|
}
|
|
ongoingDownloadsMutex.Lock()
|
|
ongoingDownloads[url] = struct{}{}
|
|
ongoingDownloadsMutex.Unlock()
|
|
|
|
log.Printf("Downloading %s", url)
|
|
|
|
err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration)
|
|
if err != nil {
|
|
Warning.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)
|
|
}
|
|
|
|
client := youtube.Client{}
|
|
video, err := client.GetVideo(url)
|
|
if err != nil {
|
|
return fmt.Errorf("failed downloading %s with %+v", url, err)
|
|
}
|
|
// $ go run . download -m mp4 -q hd https://www.youtube.com/watch?v=D9trBXaXCgA
|
|
// This works fine, now I have to figure out how to plug -m and -q into this shit below
|
|
downloader := ytd.Downloader{
|
|
OutputDir: OUTPUT_DIR,
|
|
Client: client,
|
|
}
|
|
|
|
videoTitle := video.Title
|
|
videoAuthor := video.Author
|
|
videoTitle = strings.ReplaceAll(videoTitle, " ", "-")
|
|
videoTitle = strings.ReplaceAll(videoTitle, "&", "and")
|
|
videoTitle = strings.ReplaceAll(videoTitle, "?", "")
|
|
videoAuthor = strings.ReplaceAll(videoAuthor, " ", "-")
|
|
videoAuthor = strings.ReplaceAll(videoAuthor, "&", "and")
|
|
videoAuthor = strings.ReplaceAll(videoAuthor, "?", "")
|
|
|
|
videoFileRoot := filepath.Join(OUTPUT_DIR, videoAuthor)
|
|
err = os.MkdirAll(videoFileRoot, 0755)
|
|
if err != nil {
|
|
return fmt.Errorf("failed creating directory %s with %+v", videoFileRoot, err)
|
|
}
|
|
videoFile := filepath.Join(videoAuthor, videoTitle+".mp4")
|
|
err = downloader.DownloadComposite(context.Background(), videoFile, video, "hd", "mp4", "")
|
|
if err != nil {
|
|
return fmt.Errorf("failed downloading %s with %+v", url, err)
|
|
}
|
|
|
|
log.Printf("Downloaded %s", url)
|
|
ongoingDownloadsMutex.Lock()
|
|
delete(ongoingDownloads, url)
|
|
ongoingDownloadsMutex.Unlock()
|
|
return nil
|
|
}
|