Skip to content

Instantly share code, notes, and snippets.

@UlisseMini
Created November 4, 2018 03:08
Show Gist options
  • Save UlisseMini/533a08a3029d1885fde34a73135c5182 to your computer and use it in GitHub Desktop.
Save UlisseMini/533a08a3029d1885fde34a73135c5182 to your computer and use it in GitHub Desktop.
downloads youtube links in new goroutines, has a strange bug where it tries to add a "" url
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"sync"
)
// Global so my functions can use it without passing it to them
var wg sync.WaitGroup
func main() {
// Make sure they used it correctly
if len(os.Args) != 2 {
fmt.Println("Usage: " + os.Args[0] + " <textfile>")
os.Exit(1)
}
// Goes ahead and reads the bytes into the bytes variable
bytes, err := ioutil.ReadFile(os.Args[1])
handle(err)
// Convert it into a string then a list
text := string(bytes)
urls := strings.Split(text, "\n")
// For every url download in a new goroutine
for index, item := range urls {
wg.Add(1)
go dl(item, index)
}
// Wait for all the goroutines to finish
wg.Wait()
}
func dl(url string, id int) {
fmt.Printf("[Goroutine %d] Starting to download %s\n", id, url)
cmd := exec.Command("youtube-dl", url)
// I wish i knew how to remap file descriptors to go into variables *sigh*
// If i could i would parse the shit out of the output and error
_, err := cmd.Output()
if err != nil {
fmt.Printf("[Goroutine %d] Failed to download %s\n", id, url)
} else {
fmt.Printf("[Goroutine %d] Downloaded %s\n", id, url)
}
wg.Done()
}
func handle(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment