tstor/src/sources/ytdlp/ytdlp.go

44 lines
961 B
Go
Raw Normal View History

2024-06-02 19:53:33 +00:00
package ytdlp
import (
"context"
"crypto/sha1"
"git.kmsign.ru/royalcat/tstor/pkg/ytdlp"
"github.com/royalcat/ctxprogress"
)
type ytdlpSource struct {
Url string `json:"url"`
}
var hasher = sha1.New()
func (s *ytdlpSource) Name() string {
return string(hasher.Sum([]byte(s.Url)))
}
func (s *ytdlpSource) Download(ctx context.Context, task TaskUpdater, dir string) error {
client, err := ytdlp.New()
if err != nil {
return err
}
ctxprogress.New(ctx)
ctxprogress.Set(ctx, ctxprogress.RangeProgress{Current: 0, Total: 2})
plst, err := client.Playlist(ctx, s.Url)
ctxprogress.Set(ctx, ctxprogress.RangeProgress{Current: 1, Total: 2})
ctxprogress.Range(ctx, plst, func(ctx context.Context, _ int, e ytdlp.PlaylistEntry) bool {
err = client.Download(ctx, e.Url(), dir)
if err != nil {
return false
}
return true
})
ctxprogress.Set(ctx, ctxprogress.RangeProgress{Current: 2, Total: 2})
if err != nil {
return err
}
return nil
}