Skip to content
This repository has been archived by the owner on Oct 6, 2024. It is now read-only.

Commit

Permalink
Refactor decodeVideoStream method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyah authored and Nyah committed Apr 9, 2020
1 parent fe14ba2 commit 2b90226
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"io"
"os"
"os/user"
"path/filepath"
"strings"
"unicode"

Expand Down Expand Up @@ -48,33 +46,19 @@ func decodeVideoStream(videoUrl, path, format string) error {
}

file := removeWhiteSpace(res.Info.Title) + fixExtension(format)
videoStream, err := res.Download(context.TODO(), format)
videoStream, err := res.Download(context.Background(), "best")
if err != nil {
logrus.Errorf("Unable to download %s stream: %v", format, err)
}
defer videoStream.close()

// Create output file
currentDirectory, err := user.Current()
if err != nil {
logrus.Errorf("Error getting current user directory: %v", err)
return err
}

outputDirectory := currentDirectory.HomeDir + "/Downloads/" + path
outputFile := filepath.Join(outputDirectory, file)
if err := os.MkdirAll(filepath.Dir(outputFile), 0775); err != nil {
logrus.Errorf("Unable to create output directory: %v", err)
}

fp, err := os.OpenFile(outputFile, os.O_CREATE, 0755)
fp, err := os.OpenFile(file, os.O_CREATE, 0755)
if err != nil {
logrus.Errorf("Unable to create output file: %v", err)
return err
}
defer fp.Close()

io.Copy(fp, videoStream)
videoStream.Close()

return nil
}

0 comments on commit 2b90226

Please sign in to comment.