Compare commits

..

4 Commits

Author SHA1 Message Date
8ab4b4c280 Refactor sanitization 2024-11-05 15:59:29 +01:00
0ad8722bf9 Remove from ongoing downloads even if failed 2024-11-05 15:59:29 +01:00
d8c4c343f4 Skip downloading existing videos 2024-11-05 15:59:29 +01:00
52fdcc64ef Shorter timeout 2024-11-05 15:58:44 +01:00
2 changed files with 29 additions and 16 deletions

View File

@@ -23,6 +23,11 @@ func DownloadR(url string) error {
ongoingDownloadsMutex.Lock() ongoingDownloadsMutex.Lock()
ongoingDownloads[url] = struct{}{} ongoingDownloads[url] = struct{}{}
ongoingDownloadsMutex.Unlock() ongoingDownloadsMutex.Unlock()
defer func() {
ongoingDownloadsMutex.Lock()
delete(ongoingDownloads, url)
ongoingDownloadsMutex.Unlock()
}()
log.Printf("Downloading %s", url) log.Printf("Downloading %s", url)
@@ -44,23 +49,25 @@ func DownloadR(url string) error {
// This works fine, now I have to figure out how to plug -m and -q into this shit below // This works fine, now I have to figure out how to plug -m and -q into this shit below
downloader := ytd.Downloader{ downloader := ytd.Downloader{
OutputDir: OUTPUT_DIR, OutputDir: OUTPUT_DIR,
Client: client, Client: client,
} }
videoTitle := video.Title videoTitle := Sanitize(video.Title)
videoAuthor := video.Author videoAuthor := Sanitize(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) videoFileRoot := filepath.Join(OUTPUT_DIR, videoAuthor)
err = os.MkdirAll(videoFileRoot, 0755) err = os.MkdirAll(videoFileRoot, 0755)
if err != nil { if err != nil {
return fmt.Errorf("failed creating directory %s with %+v", videoFileRoot, err) return fmt.Errorf("failed creating directory %s with %+v", videoFileRoot, err)
} }
fullVideoPath := filepath.Join(OUTPUT_DIR, videoAuthor, videoTitle+".mp4")
_, err = os.Open(fullVideoPath)
if err == nil {
log.Printf("File %s already exists, skipping download", fullVideoPath)
return nil
}
videoFile := filepath.Join(videoAuthor, videoTitle+".mp4") videoFile := filepath.Join(videoAuthor, videoTitle+".mp4")
err = downloader.DownloadComposite(context.Background(), videoFile, video, "hd", "mp4", "") err = downloader.DownloadComposite(context.Background(), videoFile, video, "hd", "mp4", "")
if err != nil { if err != nil {
@@ -68,8 +75,14 @@ func DownloadR(url string) error {
} }
log.Printf("Downloaded %s", url) log.Printf("Downloaded %s", url)
ongoingDownloadsMutex.Lock()
delete(ongoingDownloads, url)
ongoingDownloadsMutex.Unlock()
return nil return nil
} }
func Sanitize(s string) string {
s = strings.ReplaceAll(s, "&", "and")
s = strings.ReplaceAll(s, "?", "")
s = strings.ReplaceAll(s, ":", "")
s = strings.ReplaceAll(s, ";", "")
s = strings.ReplaceAll(s, ",", "")
return s
}

View File

@@ -35,7 +35,7 @@ func init() {
log.Lmicroseconds|log.Lshortfile) log.Lmicroseconds|log.Lshortfile)
} }
const DOWNLOAD_WORKERS = 1 const DOWNLOAD_WORKERS = 2
type DLHandler struct{} type DLHandler struct{}
@@ -52,7 +52,7 @@ func (*DLHandler) HandleMessage(message *nsq.Message) error {
defer cancel() defer cancel()
go func() { go func() {
ticker := time.NewTicker(5 * time.Second) ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop() defer ticker.Stop()
for { for {