tstor/src/sources/ytdlp/ytdlp.go

45 lines
1,007 B
Go
Raw Normal View History

2024-06-02 19:53:33 +00:00
package ytdlp
import (
"context"
"crypto/sha1"
2024-06-02 21:20:43 +00:00
"encoding/base64"
2024-06-02 19:53:33 +00:00
"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 {
2024-06-02 21:20:43 +00:00
return base64.URLEncoding.EncodeToString(hasher.Sum([]byte(s.Url)))
2024-06-02 19:53:33 +00:00
}
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
}