|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/goccy/go-yaml" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/tanq16/danzo/internal/scheduler" |
| 11 | + "github.com/tanq16/danzo/internal/utils" |
| 12 | +) |
| 13 | + |
| 14 | +type BatchEntry struct { |
| 15 | + OutputPath string `yaml:"op,omitempty"` |
| 16 | + Link string `yaml:"link"` |
| 17 | +} |
| 18 | + |
| 19 | +type BatchFile map[string][]BatchEntry |
| 20 | + |
| 21 | +func newBatchCmd() *cobra.Command { |
| 22 | + cmd := &cobra.Command{ |
| 23 | + Use: "batch [YAML_FILE] [OPTIONS]", |
| 24 | + Short: "Process multiple downloads from a YAML file", |
| 25 | + Args: cobra.ExactArgs(1), |
| 26 | + Run: func(cmd *cobra.Command, args []string) { |
| 27 | + yamlFile := args[0] |
| 28 | + data, err := os.ReadFile(yamlFile) |
| 29 | + if err != nil { |
| 30 | + fmt.Fprintf(os.Stderr, "Error reading YAML file: %v\n", err) |
| 31 | + os.Exit(1) |
| 32 | + } |
| 33 | + var batchFile BatchFile |
| 34 | + if err := yaml.Unmarshal(data, &batchFile); err != nil { |
| 35 | + fmt.Fprintf(os.Stderr, "Error parsing YAML file: %v\n", err) |
| 36 | + os.Exit(1) |
| 37 | + } |
| 38 | + jobs := buildJobsFromBatch(batchFile) |
| 39 | + if len(jobs) == 0 { |
| 40 | + fmt.Fprintf(os.Stderr, "No valid jobs found in the batch file\n") |
| 41 | + os.Exit(1) |
| 42 | + } |
| 43 | + scheduler.Run(jobs, workers, fileLog) |
| 44 | + }, |
| 45 | + } |
| 46 | + return cmd |
| 47 | +} |
| 48 | + |
| 49 | +func buildJobsFromBatch(batchFile BatchFile) []utils.DanzoJob { |
| 50 | + var jobs []utils.DanzoJob |
| 51 | + for jobType, entries := range batchFile { |
| 52 | + normalizedType := normalizeJobType(jobType) |
| 53 | + if normalizedType == "" { |
| 54 | + fmt.Fprintf(os.Stderr, "Warning: Unknown job type '%s', skipping...\n", jobType) |
| 55 | + continue |
| 56 | + } |
| 57 | + for _, entry := range entries { |
| 58 | + if entry.Link == "" { |
| 59 | + fmt.Fprintf(os.Stderr, "Warning: Empty link found in %s section, skipping...\n", jobType) |
| 60 | + continue |
| 61 | + } |
| 62 | + job := utils.DanzoJob{ |
| 63 | + JobType: normalizedType, |
| 64 | + URL: entry.Link, |
| 65 | + OutputPath: entry.OutputPath, |
| 66 | + HTTPClientConfig: globalHTTPConfig, |
| 67 | + Metadata: make(map[string]any), |
| 68 | + } |
| 69 | + switch normalizedType { |
| 70 | + case "http", "gdrive", "ghrelease", "m3u8": |
| 71 | + job.Connections = connections |
| 72 | + job.ProgressType = "progress" |
| 73 | + case "s3": |
| 74 | + job.Connections = connections |
| 75 | + job.ProgressType = "progress" |
| 76 | + job.Metadata["profile"] = "default" |
| 77 | + case "youtube", "ytmusic", "gitclone": |
| 78 | + job.ProgressType = "stream" |
| 79 | + default: |
| 80 | + job.ProgressType = "progress" |
| 81 | + } |
| 82 | + addJobTypeSpecificMetadata(&job, normalizedType) |
| 83 | + jobs = append(jobs, job) |
| 84 | + } |
| 85 | + } |
| 86 | + return jobs |
| 87 | +} |
| 88 | + |
| 89 | +func normalizeJobType(jobType string) string { |
| 90 | + typeMap := map[string]string{ |
| 91 | + "http": "http", |
| 92 | + "https": "http", |
| 93 | + "s3": "s3", |
| 94 | + "gdrive": "gdrive", |
| 95 | + "googledrive": "gdrive", |
| 96 | + "google-drive": "gdrive", |
| 97 | + "gitclone": "gitclone", |
| 98 | + "git-clone": "gitclone", |
| 99 | + "git": "gitclone", |
| 100 | + "ghrelease": "ghrelease", |
| 101 | + "gh-release": "ghrelease", |
| 102 | + "github": "ghrelease", |
| 103 | + "github-release": "ghrelease", |
| 104 | + "m3u8": "m3u8", |
| 105 | + "hls": "m3u8", |
| 106 | + "youtube": "youtube", |
| 107 | + "yt": "youtube", |
| 108 | + "ytmusic": "ytmusic", |
| 109 | + "youtube-music": "ytmusic", |
| 110 | + "yt-music": "ytmusic", |
| 111 | + } |
| 112 | + normalized := "" |
| 113 | + for key, value := range typeMap { |
| 114 | + if key == jobType || key == strings.ToLower(jobType) { |
| 115 | + normalized = value |
| 116 | + break |
| 117 | + } |
| 118 | + } |
| 119 | + return normalized |
| 120 | +} |
| 121 | + |
| 122 | +func addJobTypeSpecificMetadata(job *utils.DanzoJob, jobType string) { |
| 123 | + switch jobType { |
| 124 | + case "youtube": |
| 125 | + if _, ok := job.Metadata["format"]; !ok { |
| 126 | + job.Metadata["format"] = "decent" |
| 127 | + } |
| 128 | + case "ghrelease": |
| 129 | + job.Metadata["manual"] = false |
| 130 | + case "gitclone": |
| 131 | + if _, ok := job.Metadata["depth"]; !ok { |
| 132 | + job.Metadata["depth"] = 0 |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments