Restrucuture

This commit is contained in:
janic
2022-04-29 01:00:04 +02:00
parent 2142461d36
commit e3a64aab16
17 changed files with 282 additions and 112 deletions

View File

@@ -0,0 +1,24 @@
package ytdl
import "database/sql"
var DB *sql.DB
var Cache_Path string = "./cache/"
var Filepath string
type Media struct {
ID string `json: "id"`
YTID string `json: "yt-id"`
Size int `json: "size"`
Sha256sum string `json: "sha256sum"`
Duration int `json: "duration"`
Title string `json: "title"`
Author string `json: "author"`
}
// check error
func check(err error) {
if err != nil {
panic(err.Error())
}
}

View File

@@ -0,0 +1,40 @@
package ytdl
import (
"context"
"crypto/sha256"
"fmt"
"io"
"log"
"os"
"time"
"gitea.voser.cloud/Golang/GoDown/handlers/sqldb"
"gopkg.in/vansante/go-ffprobe.v2"
)
func AddMedia(ytid string) {
DB = sqldb.Opendb()
defer DB.Close()
fileinfo, err := os.Stat(Filepath)
check(err)
file, err := os.Open(Filepath)
check(err)
defer file.Close()
hash := sha256.New()
if _, err := io.Copy(hash, file); err != nil {
log.Fatal(err)
}
ctx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
ffprobe, err := ffprobe.ProbeURL(ctx, Filepath)
if err != nil {
log.Panicf("Error getting data: %v", err)
}
sqlinsert := fmt.Sprintf("INSERT INTO testdb.media (ytid,size,sha256sum,duration) VALUES ('%s','%d','%x','%f')", ytid, fileinfo.Size(), hash.Sum(nil), ffprobe.Format.DurationSeconds)
fmt.Println(sqlinsert)
dbinsert, err := DB.Query(sqlinsert)
check(err)
defer dbinsert.Close()
}

View File

@@ -0,0 +1,27 @@
package ytdl
import (
"fmt"
"io"
"os"
"github.com/kkdai/youtube/v2"
)
func DlAudio(ytid string) {
Filepath = fmt.Sprintf(Cache_Path + ytid + ".mp4")
ytdl := youtube.Client{}
video, err := ytdl.GetVideo(ytid)
check(err)
formats := video.Formats.WithAudioChannels()
stream, _, err := ytdl.GetStream(video, &formats[0])
check(err)
_ = os.Mkdir(Cache_Path, 0755)
file, err := os.Create(Filepath)
check(err)
fmt.Println(Filepath)
defer file.Close()
_, err = io.Copy(file, stream)
check(err)
}