Files
Media-download-telegram-bot/internal/ytdlp/classify.go
T
MichilisandClaude Fable 5.2 3b361c17a1 Phase 1: core bot
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>
2026-11-18 07:33:32 +00:00

75 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package ytdlp
import (
"strings"
"github.com/noderunners/dlbot/internal/links"
)
type rule struct {
code Code
retryable bool
needles []string
}
// rules are checked in order, so specific causes come before generic ones.
// Needles are matched against lowercased yt-dlp output.
var rules = []rule{
{CodeUnsupported, false, []string{"unsupported url", "is not a valid url", "no suitable extractor"}},
{CodeSiteBlocked, false, []string{"confirm you’re not a bot", "confirm you're not a bot", "captcha", "cloudflare"}},
{CodeDRM, false, []string{"drm protected", "drm-protected", "is drm"}},
{CodeGeoBlocked, false, []string{"not available in your country", "geo-restricted", "geo restricted", "blocked in your country", "not available from your location", "georestricted"}},
{CodeLive, false, []string{"live event will begin", "premieres in", "this live event", "is not currently live", "live stream"}},
{CodePrivate, false, []string{"private video", "video is private", "is private", "members-only", "members only", "join this channel", "requires payment", "subscriber-only", "only available to", "premium"}},
{CodeLoginRequired, false, []string{"login required", "log in", "logged-in", "logged in", "sign in", "--cookies", "account credentials", "authentication", "age-restricted", "confirm your age", "inappropriate for some users"}},
{CodeNotFound, false, []string{"video unavailable", "has been removed", "no longer available", "http error 404", "http error 410", "not found", "does not exist", "been deleted", "account has been terminated"}},
{CodeRateLimited, true, []string{"http error 429", "too many requests", "rate-limit", "rate limit"}},
{CodeNoSubtitles, false, []string{"no subtitles for the requested languages"}},
{CodeNoMedia, false, []string{"requested format is not available", "no video formats found", "unable to obtain file audio codec", "no formats found"}},
{CodeTooLarge, false, []string{"larger than max-filesize"}},
{CodeInternal, true, []string{"no space left on device", "permission denied", "read-only file system"}},
{CodeProcessing, false, []string{"postprocessing", "ffmpeg", "ffprobe", "conversion failed"}},
{CodeNetwork, true, []string{"timed out", "timeout", "connection reset", "connection refused", "connection aborted", "name resolution", "getaddrinfo", "http error 5", "incompleteread", "unable to download", "network is unreachable", "remote end closed", "ssl"}},
}
// Classify maps raw yt-dlp output to a failure class. The raw text is
// redacted so it is safe for owner logs.
func Classify(raw string) *Error {
e := &Error{Code: CodeUnknown, Retryable: true, Raw: sanitize(raw)}
low := strings.ToLower(raw)
for _, r := range rules {
for _, n := range r.needles {
if strings.Contains(low, n) {
e.Code = r.code
e.Retryable = r.retryable
return e
}
}
}
return e
}
// sanitize keeps the error lines of the output, redacts URLs and bounds the
// length.
func sanitize(raw string) string {
var keep []string
for _, line := range strings.Split(raw, "\n") {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "ERROR") {
keep = append(keep, line)
}
}
out := strings.Join(keep, " | ")
if out == "" {
out = strings.TrimSpace(raw)
if len(out) > 600 {
out = out[len(out)-600:]
}
}
out = links.RedactText(out)
if len(out) > 1000 {
out = out[:1000]
}
return out
}