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 }