Phase 1: quick flow fixes
- One message per job: the status message is deleted once the file is
sent, or shortened to "Sent." when Telegram keeps it.
- No reply quotes in private chats; groups keep them.
- Queue status only mentions the position when the wait is above
QUEUE_NOTICE_THRESHOLD_SECONDS or somebody is ahead. Status edits are
throttled to one per 2.5 s per message; a state that arrives too soon
is shown later instead of dropped. The wait estimate is 0 with a free
slot.
- Caption with title (uploader and date when generic), uploader,
duration, a short source link without tracking parameters and an
optional "via @bot" line (CAPTION_ATTRIBUTION). Uploaded videos get a
320 px JPEG thumbnail; merged MP4s are written with faststart.
- Buttons under the result: Audio (MP3), Other quality, Thumbnail,
Delete. Requester only in groups.
- Language resolved once per update and passed in the context. A
fallback to English is logged as a bug; a test walks every screen in
Spanish and Dutch. Language picker on the first /start.
- Sizes without a zero decimal ("50 MB").
- First link of a new user shows the card once and sets the default to
"best that fits my limit".
- Errors offer the next step (Try again, Smaller quality, Upgrade, Open
app); passing lookup failures are retried once.
- Up to MULTI_LINK_MAX links per message; a link sent a moment ago gets
"already sent" with a Send again button.
- Mini App menu button, labelled per private chat in the user's
language.
- Personal /settings in private chats with an "Add me to a group"
button.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
3c153af97f
commit
6843053582
@@ -0,0 +1,48 @@
|
||||
package service
|
||||
|
||||
import "github.com/noderunners/dlbot/internal/media"
|
||||
|
||||
// fitOrder is the order "best that fits my limit" tries the qualities in.
|
||||
var fitOrder = []media.Profile{media.Best, media.P1080, media.P720, media.P480}
|
||||
|
||||
// BestFit picks the highest quality whose estimated size is within
|
||||
// maxSize (0 means no limit), from the sizes yt-dlp reported (filesize or
|
||||
// filesize_approx, else bitrate times duration).
|
||||
//
|
||||
// Only qualities the source has are considered. A quality with no size
|
||||
// estimate counts as fitting only when no quality has one, since then
|
||||
// there is nothing to judge by and the worker enforces the cap on the real
|
||||
// file. When even the smallest quality is too big, the smallest is
|
||||
// returned with fits false, so the caller can refuse it with the usual
|
||||
// over-limit flow.
|
||||
func BestFit(info *CardInfo, maxSize int64) (p media.Profile, fits bool) {
|
||||
var avail []media.Profile
|
||||
anySize := false
|
||||
for _, q := range fitOrder {
|
||||
if h := q.Height(); h > 0 && info.MaxHeight > 0 && info.MaxHeight < h {
|
||||
continue // the source is smaller than this quality
|
||||
}
|
||||
avail = append(avail, q)
|
||||
if info.Sizes[string(q)] > 0 {
|
||||
anySize = true
|
||||
}
|
||||
}
|
||||
if !anySize || maxSize <= 0 {
|
||||
return media.Best, true
|
||||
}
|
||||
smallest := avail[len(avail)-1]
|
||||
for _, q := range avail {
|
||||
size := info.Sizes[string(q)]
|
||||
if size > 0 && size <= maxSize {
|
||||
return q, true
|
||||
}
|
||||
}
|
||||
// Prefer the smallest quality that has a size, so the refusal can say
|
||||
// how big it is.
|
||||
for i := len(avail) - 1; i >= 0; i-- {
|
||||
if info.Sizes[string(avail[i])] > 0 {
|
||||
return avail[i], false
|
||||
}
|
||||
}
|
||||
return smallest, false
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/noderunners/dlbot/internal/media"
|
||||
)
|
||||
|
||||
const mb = 1 << 20
|
||||
|
||||
func TestBestFit(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
maxHeight int
|
||||
sizes map[string]int64
|
||||
cap int64
|
||||
want media.Profile
|
||||
wantFits bool
|
||||
}{
|
||||
{"everything fits", 1080, map[string]int64{"best": 40 * mb, "1080": 40 * mb, "720": 20 * mb, "480": 10 * mb}, 50 * mb, media.Best, true},
|
||||
{"best is too big", 2160, map[string]int64{"best": 300 * mb, "1080": 120 * mb, "720": 45 * mb, "480": 20 * mb}, 50 * mb, media.P720, true},
|
||||
{"only 480 fits", 1080, map[string]int64{"best": 90 * mb, "1080": 90 * mb, "720": 60 * mb, "480": 30 * mb}, 50 * mb, media.P480, true},
|
||||
{"nothing fits", 1080, map[string]int64{"best": 900 * mb, "1080": 900 * mb, "720": 400 * mb, "480": 200 * mb}, 50 * mb, media.P480, false},
|
||||
{"exactly the cap fits", 720, map[string]int64{"best": 50 * mb}, 50 * mb, media.Best, true},
|
||||
{"no limit takes the best", 1080, map[string]int64{"best": 900 * mb}, 0, media.Best, true},
|
||||
// Missing size data.
|
||||
{"no sizes at all", 1080, map[string]int64{}, 50 * mb, media.Best, true},
|
||||
{"nil sizes", 0, nil, 50 * mb, media.Best, true},
|
||||
{"unknown sizes are skipped when others are known", 1080, map[string]int64{"best": 200 * mb, "480": 20 * mb}, 50 * mb, media.P480, true},
|
||||
{"nothing known fits", 1080, map[string]int64{"best": 200 * mb, "720": 90 * mb}, 50 * mb, media.P720, false},
|
||||
// Qualities above the source are not considered.
|
||||
{"small source", 480, map[string]int64{"best": 30 * mb, "480": 30 * mb}, 10 * mb, media.P480, false},
|
||||
{"small source fits", 360, map[string]int64{"best": 8 * mb}, 10 * mb, media.Best, true},
|
||||
{"unknown height keeps every quality", 0, map[string]int64{"best": 80 * mb, "720": 40 * mb}, 50 * mb, media.P720, true},
|
||||
}
|
||||
for _, c := range cases {
|
||||
info := &CardInfo{MaxHeight: c.maxHeight, Sizes: c.sizes}
|
||||
got, fits := BestFit(info, c.cap)
|
||||
if got != c.want || fits != c.wantFits {
|
||||
t.Errorf("%s: BestFit = %s, %v; want %s, %v", c.name, got, fits, c.want, c.wantFits)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,8 @@ type CardInfo struct {
|
||||
// Availability is yt-dlp's access class ("public", "private", ...).
|
||||
// Anything that is not public or unlisted is never cached.
|
||||
Availability string `json:"availability,omitempty"`
|
||||
// UploadDate is YYYYMMDD, when the site says.
|
||||
UploadDate string `json:"upload_date,omitempty"`
|
||||
// SubLangs are the subtitle languages, written ones before automatic.
|
||||
SubLangs []string `json:"sub_langs,omitempty"`
|
||||
AutoSubLangs []string `json:"auto_sub_langs,omitempty"`
|
||||
@@ -116,6 +118,7 @@ func cardInfo(url string, info *ytdlp.Info) *CardInfo {
|
||||
Sizes: map[string]int64{},
|
||||
}
|
||||
c.Availability = info.Availability
|
||||
c.UploadDate = info.UploadDate
|
||||
c.SubLangs, c.AutoSubLangs = info.SubtitleLangs()
|
||||
if c.Title == "" {
|
||||
c.Title = info.ID
|
||||
@@ -282,6 +285,8 @@ type Service struct {
|
||||
log *slog.Logger
|
||||
probes chan struct{}
|
||||
now func() time.Time
|
||||
// probeRetryDelay is the pause before a failed lookup is tried again.
|
||||
probeRetryDelay time.Duration
|
||||
}
|
||||
|
||||
// Prober looks up the metadata of a link. It is yt-dlp on the core, or the
|
||||
@@ -298,7 +303,7 @@ func New(cfg config.Config, store *db.Store, sch *sched.Scheduler, bill *billing
|
||||
}
|
||||
return &Service{
|
||||
cfg: cfg, store: store, sched: sch, billing: bill, cache: ch, prober: prober, log: log,
|
||||
probes: make(chan struct{}, n), now: time.Now,
|
||||
probes: make(chan struct{}, n), now: time.Now, probeRetryDelay: time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,6 +341,16 @@ func (s *Service) ProbeAny(ctx context.Context, url string) (*Probed, error) {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
info, err := s.prober.Probe(ctx, url)
|
||||
if retryable(err) && ctx.Err() == nil {
|
||||
// A passing problem (network, rate limit, a lost worker) often goes
|
||||
// away a moment later. One more try before the user sees an error.
|
||||
s.log.Info("probe failed, trying once more", "url", links.Redact(url), "code", string(ytdlp.CodeOf(err)))
|
||||
select {
|
||||
case <-time.After(s.probeRetryDelay):
|
||||
info, err = s.prober.Probe(ctx, url)
|
||||
case <-ctx.Done():
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
var ye *ytdlp.Error
|
||||
if errors.As(err, &ye) {
|
||||
@@ -363,6 +378,20 @@ func (s *Service) ProbeAny(ctx context.Context, url string) (*Probed, error) {
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// retryable reports whether a lookup failure is worth one more try.
|
||||
func retryable(err error) bool {
|
||||
var ye *ytdlp.Error
|
||||
if !errors.As(err, &ye) || !ye.Retryable {
|
||||
return false
|
||||
}
|
||||
// A timeout already took the full probe time.
|
||||
return ye.Code != ytdlp.CodeTimeout && ye.Code != ytdlp.CodeCancelled
|
||||
}
|
||||
|
||||
// SetProbeRetryDelay changes the pause before a failed lookup is tried
|
||||
// again. Tests shorten it.
|
||||
func (s *Service) SetProbeRetryDelay(d time.Duration) { s.probeRetryDelay = d }
|
||||
|
||||
// tier converts a configured tier to resolver limits.
|
||||
func tier(t config.Tier) entitle.Limits {
|
||||
return entitle.Limits{
|
||||
|
||||
Reference in New Issue
Block a user