Cleanup
This commit is contained in:
48
download.go
48
download.go
@@ -2,13 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/kkdai/youtube/v2"
|
||||
"github.com/lrstanley/go-ytdlp"
|
||||
)
|
||||
|
||||
@@ -22,49 +17,6 @@ var dl = ytdlp.New().
|
||||
HTTPChunkSize("20M").
|
||||
MarkWatched()
|
||||
|
||||
func DownloadNative(event PBEvent, status chan error) {
|
||||
log.Printf("%s: Downloading %s", event.Record.Id, event.Record.Link)
|
||||
videoId := strings.Split(event.Record.Link, "?v=")[1]
|
||||
client := youtube.Client{}
|
||||
|
||||
video, err := client.GetVideo(videoId)
|
||||
if err != nil {
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
|
||||
formats := video.Formats.WithAudioChannels()
|
||||
stream, _, err := client.GetStream(video, &formats[0])
|
||||
if err != nil {
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
defer stream.Close()
|
||||
|
||||
log.Printf("%s: Video title: %s", event.Record.Id, video.Title)
|
||||
outFolder := fmt.Sprintf("%s/%s", OUTPUT_DIR, video.Author)
|
||||
out := fmt.Sprintf("%s/%s.mp4", outFolder, video.Title)
|
||||
err = os.MkdirAll(outFolder, os.ModePerm)
|
||||
if err != nil {
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
log.Printf("%s: Video save location: %s", event.Record.Id, out)
|
||||
|
||||
file, err := os.Create(out)
|
||||
if err != nil {
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = io.Copy(file, stream)
|
||||
if err != nil {
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func Download(event PBEvent, status chan error) {
|
||||
_, err := dl.Run(context.TODO(), event.Record.Link)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user