Sanitize video titles and author names

This commit is contained in:
2024-11-05 15:43:06 +01:00
parent 63899ac4bd
commit fa0f1cb48c

View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/gen2brain/beeep" "github.com/gen2brain/beeep"
"github.com/kkdai/youtube/v2" "github.com/kkdai/youtube/v2"
@@ -46,12 +47,21 @@ func DownloadR(url string) error {
Client: client, Client: client,
} }
videoFileRoot := filepath.Join(OUTPUT_DIR, video.Author) 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) 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)
} }
videoFile := filepath.Join(video.Author, video.Title+".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 {
return fmt.Errorf("failed downloading %s with %+v", url, err) return fmt.Errorf("failed downloading %s with %+v", url, err)