The point of this is (hopefully) some sort of resiliency I do not want to lose any messages ever And I want to be able to kill this process whenever it is misbehaving Hopefully this achieves that goal
		
			
				
	
	
		
			61 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"fmt"
 | 
						|
	"log"
 | 
						|
	"sync"
 | 
						|
 | 
						|
	"github.com/gen2brain/beeep"
 | 
						|
	"github.com/lrstanley/go-ytdlp"
 | 
						|
)
 | 
						|
 | 
						|
const OUTPUT_DIR = "C:/Users/Administrator/ytdlpVideos"
 | 
						|
 | 
						|
var ongoingDownloads = make(map[string]struct{})
 | 
						|
var ongoingDownloadsMutex = &sync.Mutex{}
 | 
						|
 | 
						|
var dl = ytdlp.New().
 | 
						|
	// FormatSort("bestvideo[ext=mp4]+bestaudio[ext=m4a]").
 | 
						|
	FormatSort("res,ext:mp4:m4a").
 | 
						|
	Output("C:/Users/Administrator/ytdlpVideos/%(uploader)s/%(title)s.%(ext)s").
 | 
						|
	LimitRate(fmt.Sprintf("%dM", 100/DOWNLOAD_WORKERS)).
 | 
						|
	// HTTPChunkSize("20M").
 | 
						|
	MarkWatched().
 | 
						|
	SponsorblockMark("all").
 | 
						|
	RecodeVideo("mp4").
 | 
						|
	ConcurrentFragments(6)
 | 
						|
 | 
						|
func Download(url string) error {
 | 
						|
	_, ongoing := ongoingDownloads[url]
 | 
						|
	if ongoing {
 | 
						|
		// return fmt.Errorf("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)
 | 
						|
	}
 | 
						|
 | 
						|
	_, err = dl.Run(context.TODO(), url)
 | 
						|
	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
 | 
						|
}
 |