1 Commits
v1.0 ... v1.1

Author SHA1 Message Date
7b6d3ffe54 Fixed go routines syncing 2018-04-21 21:08:29 +02:00

View File

@@ -6,6 +6,7 @@ import (
"log" "log"
"os" "os"
"regexp" "regexp"
"sync"
//"path/filepath" //"path/filepath"
. "github.com/kkdai/youtube" . "github.com/kkdai/youtube"
@@ -21,7 +22,7 @@ func NewBatch(batchFile, destinationDir string) *Batch {
func (b *Batch) Start() { func (b *Batch) Start() {
//Start downloading all videos //Start downloading all videos
log.Println("Start downloading...")
//open the file containing yt links //open the file containing yt links
linkfile, err := os.Open(b.batchfile) linkfile, err := os.Open(b.batchfile)
if err != nil { if err != nil {
@@ -34,18 +35,27 @@ func (b *Batch) Start() {
//That should be enough for yt links. //That should be enough for yt links.
scanner := bufio.NewScanner(linkfile) scanner := bufio.NewScanner(linkfile)
var wg sync.WaitGroup
for scanner.Scan() { for scanner.Scan() {
//Call download //Call download
go downloadVideo(b.destDir, scanner.Text()) log.Println(scanner.Text())
//add go routine to wait for
wg.Add(1)
go downloadVideo(b.destDir, scanner.Text(), wg)
} }
//wait for all go routines to finish
wg.Wait()
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }
func downloadVideo(destDir, url string) { func downloadVideo(destDir, url string, wg sync.WaitGroup) {
// NewYoutube(debug) if debug parameter will set true we can log of messages // NewYoutube(debug) if debug parameter will set true we can log of messages
//defer Done() so that WaitGroup knows when the routine finishes
defer wg.Done()
y := NewYoutube(false) y := NewYoutube(false)
y.DecodeURL(url) y.DecodeURL(url)
re := regexp.MustCompile("(mp4|webm|3gpp)") re := regexp.MustCompile("(mp4|webm|3gpp)")