Exemplo n.º 1
0
Arquivo: ytdl.go Projeto: sqp/godock
// NewYTDLFile creates a video file downloader.
//
func NewYTDLFile(log cdtype.Logger, url string) (Filer, error) {
	info, e := ytdl.GetVideoInfo(url)
	if e != nil {
		return nil, fmt.Errorf("Unable to fetch video info: %s", e.Error())
	}

	// formats := info.Formats
	// // parse filter arguments, and filter through formats
	// for _, filter := range options.filters {
	// 	filter, e := parseFilter(filter)
	// 	if !log.Err(e) {
	// 		formats = filter(formats)
	// 	}
	// }
	// if len(formats) == 0 {
	// 	return nil, fmt.Errorf("No formats available that match criteria: %s", e.Error())
	// }

	log.Info("Author", info.Author)
	log.Info("Description", info.Description)
	log.Info("ID", info.ID)
	log.Info("vid", info)

	var list []*Format
	for _, v := range info.Formats {
		nf := &Format{
			Itag:          v.Itag,
			Extension:     v.Extension,
			Resolution:    v.Resolution,
			VideoEncoding: v.VideoEncoding,
			AudioEncoding: v.AudioEncoding,
			AudioBitrate:  v.AudioBitrate,
		}

		s := v.ValueForKey("clen")
		if s != nil {
			nf.Size, e = strconv.Atoi(s.(string))
			nf.Size /= 1000000
			log.Err(e, "convert size. format=", v.Itag)
			// } else {
			// log.Info("no clen", v.Itag)
		}

		list = append(list, nf)
	}

	// pretty.Println(info)

	return &YTDLFile{
		VideoInfo: *info,
		formats:   list,
		log:       log,
	}, nil
}
Exemplo n.º 2
0
Arquivo: ytdl.go Projeto: sqp/godock
func handler(identifier string, options options, log cdtype.Logger) error {
	info, err := ytdl.GetVideoInfo(identifier)
	if err != nil {
		return fmt.Errorf("Unable to fetch video info: %s", err.Error())
	}

	formats := info.Formats
	// parse filter arguments, and filter through formats
	for _, filter := range options.filters {
		filter, err := parseFilter(filter)
		if err == nil {
			formats = filter(formats)
		}
	}
	if len(formats) == 0 {
		return fmt.Errorf("No formats available that match criteria")

	}
	format := formats[0]
	downloadURL, err := info.GetDownloadURL(format)
	if err != nil {
		return fmt.Errorf("Unable to get download url: %s", err.Error())
	}
	if options.startOffset != "" {
		var offset time.Duration
		offset, err = time.ParseDuration(options.startOffset)
		query := downloadURL.Query()
		query.Set("begin", fmt.Sprint(int64(offset/time.Millisecond)))
		downloadURL.RawQuery = query.Encode()
	}

	// log.DEV("test", )

	// pretty.Println(info)

	// if options.downloadURL {
	// 	fmt.Print(downloadURL.String())
	// 	// print new line character if outputing to terminal
	// 	if isatty.IsTerminal(os.Stdout.Fd()) {
	// 		fmt.Println()
	// 	}
	// 	return
	// }

	// if out == nil {
	// 	var fileName string
	// 	fileName, err = createFileName(options.outputFile, outputFileName{
	// 		Title:         sanitizeFileNamePart(info.Title),
	// 		Ext:           sanitizeFileNamePart(format.Extension),
	// 		DatePublished: sanitizeFileNamePart(info.DatePublished.Format("2006-01-02")),
	// 		Resolution:    sanitizeFileNamePart(format.Resolution),
	// 		Author:        sanitizeFileNamePart(info.Author),
	// 		Duration:      sanitizeFileNamePart(info.Duration.String()),
	// 	})
	// 	if err != nil {
	// 		err = fmt.Errorf("Unable to parse output file file name: %s", err.Error())
	// 		return
	// 	}
	// 	// Create file truncate if append flag is not set
	// 	flags := os.O_CREATE | os.O_WRONLY
	// 	if options.append {
	// 		flags |= os.O_APPEND
	// 	} else {
	// 		flags |= os.O_TRUNC
	// 	}
	// 	var f *os.File
	// 	// open as write only
	// 	f, err = os.OpenFile(fileName, flags, 0666)
	// 	if err != nil {
	// 		err = fmt.Errorf("Unable to open output file: %s", err.Error())
	// 		return
	// 	}
	// 	defer f.Close()
	// 	out = f
	// }

	// log.Info("Downloading to ", out.(*os.File).Name())
	// var req *http.Request
	// req, err = http.NewRequest("GET", downloadURL.String(), nil)
	// // if byte range flag is set, use http range header option
	// if options.byteRange != "" || options.append {
	// 	if options.byteRange == "" && out != os.Stdout {
	// 		if stat, err := out.(*os.File).Stat(); err != nil {
	// 			options.byteRange = strconv.FormatInt(stat.Size(), 10) + "-"
	// 		}
	// 	}
	// 	if options.byteRange != "" {
	// 		req.Header.Set("Range", "bytes="+options.byteRange)
	// 	}
	// }
	// resp, err := http.DefaultClient.Do(req)
	// if err != nil || resp.StatusCode < 200 || resp.StatusCode >= 300 {
	// 	if err == nil {
	// 		err = fmt.Errorf("Received status code %d from download url", resp.StatusCode)
	// 	}
	// 	err = fmt.Errorf("Unable to start download: %s", err.Error())
	// 	return
	// }
	// defer resp.Body.Close()
	// // if we aren't in silent mode or the no progress flag wasn't set,
	// // initialize progress bar
	// // if !silent && !options.noProgress {
	// // 	progressBar := pb.New64(resp.ContentLength)
	// // 	progressBar.SetUnits(pb.U_BYTES)
	// // 	progressBar.ShowTimeLeft = true
	// // 	progressBar.ShowSpeed = true
	// // 	//	progressBar.RefreshRate = time.Millisecond * 1
	// // 	progressBar.Output = logOut
	// // 	progressBar.Start()
	// // 	defer progressBar.Finish()
	// // 	out = io.MultiWriter(out, progressBar)
	// // }
	// _, err = io.Copy(out, resp.Body)
	return err
}