2020-07-27 21:42:45 +02:00
|
|
|
package downloader
|
|
|
|
|
|
|
|
import (
|
2020-07-27 23:14:06 +02:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2020-07-27 21:42:45 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2020-07-27 23:14:06 +02:00
|
|
|
"github.com/lbryio/ytsync/v5/downloader/ytdl"
|
|
|
|
|
2020-07-27 21:42:45 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-07-27 21:57:19 +02:00
|
|
|
func GetPlaylistVideoIDs(channelName string, maxVideos int) ([]string, error) {
|
2020-07-27 21:42:45 +02:00
|
|
|
args := []string{"--skip-download", "https://www.youtube.com/channel/" + channelName, "--get-id", "--flat-playlist"}
|
2020-07-27 23:14:06 +02:00
|
|
|
ids, err := run(args, true, true)
|
2020-07-27 21:57:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
videoIDs := make([]string, maxVideos)
|
|
|
|
for i, v := range ids {
|
|
|
|
if i >= maxVideos {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
videoIDs[i] = v
|
|
|
|
}
|
|
|
|
return videoIDs, nil
|
2020-07-27 21:42:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 23:14:06 +02:00
|
|
|
func GetVideoInformation(videoID string) (*ytdl.YtdlVideo, error) {
|
|
|
|
args := []string{"--skip-download", "--print-json", "https://www.youtube.com/watch?v=" + videoID}
|
|
|
|
results, err := run(args, false, true)
|
2020-07-27 21:42:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
2020-07-27 23:14:06 +02:00
|
|
|
var video *ytdl.YtdlVideo
|
|
|
|
err = json.Unmarshal([]byte(results[0]), &video)
|
2020-07-27 21:42:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
2020-07-27 23:14:06 +02:00
|
|
|
return video, nil
|
|
|
|
}
|
2020-07-27 21:42:45 +02:00
|
|
|
|
2020-07-27 23:14:06 +02:00
|
|
|
func run(args []string, withStdErr, withStdOut bool) ([]string, error) {
|
|
|
|
cmd := exec.Command("youtube-dl", args...)
|
|
|
|
logrus.Printf("Running command youtube-dl %s", strings.Join(args, " "))
|
|
|
|
|
|
|
|
var stderr io.ReadCloser
|
|
|
|
var errorLog []byte
|
|
|
|
if withStdErr {
|
|
|
|
var err error
|
|
|
|
stderr, err = cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
errorLog, err = ioutil.ReadAll(stderr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
2020-07-27 21:42:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 23:14:06 +02:00
|
|
|
var stdout io.ReadCloser
|
|
|
|
var outLog []byte
|
|
|
|
if withStdOut {
|
|
|
|
var err error
|
|
|
|
stdout, err = cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
outLog, err = ioutil.ReadAll(stdout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := cmd.Wait()
|
2020-07-27 21:42:45 +02:00
|
|
|
if len(errorLog) > 0 {
|
|
|
|
return nil, errors.Err(err)
|
|
|
|
}
|
|
|
|
if len(errorLog) > 0 {
|
|
|
|
return nil, errors.Err(string(errorLog))
|
|
|
|
}
|
|
|
|
return strings.Split(strings.Replace(string(outLog), "\r\n", "\n", -1), "\n"), nil
|
|
|
|
}
|