Link in, card, download, send, in private chats and groups. - cmd/core with an embedded download worker - Telegram front end on telego v1.13.0 (Bot API 10.4): link card with thumbnail and format buttons, one live status message, /dl /audio /thumb /info /queue /cancel, group support with requester-only buttons - Database backed queue: paid and free lanes, leases with heartbeats, one automatic retry, cancel, queue position and wait estimate - yt-dlp and ffprobe behind a Runner interface with classified, redacted errors mapped to friendly messages - SQLite via modernc with goose migrations, portable schema - Local file storage behind a Storage interface - go-i18n with an English catalog, no literal text in handlers - Free tier limits enforced before queueing and again on the real file - Bot token and webhook secret are stripped from all log output - Tests: URL parsing, job lifecycle, worker, yt-dlp wrapper, and end to end flows against a fake Bot API server; opt-in test with the real yt-dlp Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
159 lines
4.2 KiB
Go
159 lines
4.2 KiB
Go
// Package ytdlptest provides a scriptable ytdlp.Runner for tests.
|
|
package ytdlptest
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/noderunners/dlbot/internal/media"
|
|
"github.com/noderunners/dlbot/internal/ytdlp"
|
|
)
|
|
|
|
// Fake is an in-memory ytdlp.Runner. The zero value answers every URL with
|
|
// a small default video.
|
|
type Fake struct {
|
|
mu sync.Mutex
|
|
// Infos maps a URL to the metadata Probe returns.
|
|
Infos map[string]*ytdlp.Info
|
|
// ProbeErrs maps a URL to the error Probe returns.
|
|
ProbeErrs map[string]error
|
|
// DownloadErrs is consumed one error per Download call, per URL. A nil
|
|
// entry means success. This is how tests script "fail once, then work".
|
|
DownloadErrs map[string][]error
|
|
// FileSize is the size of produced files. Defaults to 1024 bytes.
|
|
FileSize int
|
|
// Delay is how long a download takes. It stops early on cancel.
|
|
Delay time.Duration
|
|
// Block, when set, makes downloads wait until the channel is closed.
|
|
Block chan struct{}
|
|
|
|
probes []string
|
|
downloads []ytdlp.DownloadRequest
|
|
}
|
|
|
|
var _ ytdlp.Runner = (*Fake)(nil)
|
|
|
|
// DefaultInfo is the metadata returned for URLs without an explicit entry.
|
|
func DefaultInfo(url string) *ytdlp.Info {
|
|
return &ytdlp.Info{
|
|
Type: "video",
|
|
ID: "vid123",
|
|
Title: "Test Video",
|
|
Extractor: "fake",
|
|
WebpageURL: url,
|
|
Uploader: "Tester",
|
|
Duration: 60,
|
|
Thumbnail: "https://img.example.com/t.jpg",
|
|
Formats: []ytdlp.Format{
|
|
{FormatID: "a", Ext: "m4a", VCodec: "none", ACodec: "mp4a", Filesize: 1 << 20},
|
|
{FormatID: "v480", Ext: "mp4", VCodec: "avc1", ACodec: "none", Height: 480, Filesize: 4 << 20},
|
|
{FormatID: "v720", Ext: "mp4", VCodec: "avc1", ACodec: "none", Height: 720, Filesize: 9 << 20},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Probe implements ytdlp.Runner.
|
|
func (f *Fake) Probe(_ context.Context, url string) (*ytdlp.Info, error) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.probes = append(f.probes, url)
|
|
if err := f.ProbeErrs[url]; err != nil {
|
|
return nil, err
|
|
}
|
|
if info := f.Infos[url]; info != nil {
|
|
cp := *info
|
|
return &cp, nil
|
|
}
|
|
return DefaultInfo(url), nil
|
|
}
|
|
|
|
// Download implements ytdlp.Runner.
|
|
func (f *Fake) Download(ctx context.Context, req ytdlp.DownloadRequest, onProgress func(ytdlp.Progress)) (*ytdlp.Result, error) {
|
|
f.mu.Lock()
|
|
f.downloads = append(f.downloads, req)
|
|
var scripted error
|
|
if q := f.DownloadErrs[req.URL]; len(q) > 0 {
|
|
scripted = q[0]
|
|
f.DownloadErrs[req.URL] = q[1:]
|
|
}
|
|
size := f.FileSize
|
|
if size == 0 {
|
|
size = 1024
|
|
}
|
|
delay, block := f.Delay, f.Block
|
|
info := f.Infos[req.URL]
|
|
f.mu.Unlock()
|
|
|
|
if onProgress != nil {
|
|
onProgress(ytdlp.Progress{Stage: ytdlp.StageDownloading, Downloaded: int64(size / 2), Total: int64(size)})
|
|
}
|
|
if block != nil {
|
|
select {
|
|
case <-block:
|
|
case <-ctx.Done():
|
|
return nil, &ytdlp.Error{Code: ytdlp.CodeCancelled, Raw: "cancelled"}
|
|
}
|
|
}
|
|
if delay > 0 {
|
|
select {
|
|
case <-time.After(delay):
|
|
case <-ctx.Done():
|
|
return nil, &ytdlp.Error{Code: ytdlp.CodeCancelled, Raw: "cancelled"}
|
|
}
|
|
}
|
|
if scripted != nil {
|
|
return nil, scripted
|
|
}
|
|
if req.MaxSize > 0 && int64(size) > req.MaxSize {
|
|
return nil, &ytdlp.Error{Code: ytdlp.CodeTooLarge, Raw: "too large"}
|
|
}
|
|
ext := "mp4"
|
|
switch req.Profile.Kind() {
|
|
case media.KindAudio:
|
|
ext = string(req.Profile)
|
|
case media.KindThumb:
|
|
ext = "jpg"
|
|
case media.KindSubs:
|
|
ext = "srt"
|
|
}
|
|
if err := os.MkdirAll(req.Dir, 0o750); err != nil {
|
|
return nil, err
|
|
}
|
|
path := filepath.Join(req.Dir, "media."+ext)
|
|
if err := os.WriteFile(path, make([]byte, size), 0o640); err != nil {
|
|
return nil, err
|
|
}
|
|
if info == nil {
|
|
info = DefaultInfo(req.URL)
|
|
}
|
|
res := &ytdlp.Result{
|
|
Path: path, Size: int64(size), Ext: ext,
|
|
ID: info.ID, Title: info.Title, Uploader: info.Uploader, Extractor: info.Extractor,
|
|
Duration: float64(info.Duration),
|
|
}
|
|
if req.Profile.Kind() == media.KindVideo {
|
|
res.Width, res.Height = 1280, 720
|
|
}
|
|
if onProgress != nil {
|
|
onProgress(ytdlp.Progress{Stage: ytdlp.StageProcessing})
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
// Probes returns the URLs probed so far.
|
|
func (f *Fake) Probes() []string {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return append([]string(nil), f.probes...)
|
|
}
|
|
|
|
// Downloads returns the download requests seen so far.
|
|
func (f *Fake) Downloads() []ytdlp.DownloadRequest {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
return append([]ytdlp.DownloadRequest(nil), f.downloads...)
|
|
}
|