Compare commits
4 Commits
0c27822871
...
f8f9d99e69
| Author | SHA1 | Date | |
|---|---|---|---|
|
f8f9d99e69
|
|||
|
e8120b9529
|
|||
|
cd7aaeebc2
|
|||
|
8408b17ca0
|
@@ -3,6 +3,7 @@ package dl
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -25,8 +26,6 @@ func parseProgressLine(line string) *progressUpdate {
|
|||||||
if line == "" || !strings.HasPrefix(line, "[download]") {
|
if line == "" || !strings.HasPrefix(line, "[download]") {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Try to extract the simple percentage from the default format:
|
|
||||||
// [download] 45.2% of 123.45MiB at 2.34MiB/s ETA 00:45
|
|
||||||
m := progressRE.FindStringSubmatch(line)
|
m := progressRE.FindStringSubmatch(line)
|
||||||
if m == nil {
|
if m == nil {
|
||||||
return nil
|
return nil
|
||||||
@@ -38,12 +37,10 @@ func parseProgressLine(line string) *progressUpdate {
|
|||||||
|
|
||||||
u := &progressUpdate{pct: pct}
|
u := &progressUpdate{pct: pct}
|
||||||
|
|
||||||
// Extract speed and ETA from remaining parts
|
|
||||||
rest := line
|
rest := line
|
||||||
if idx := strings.Index(rest, "%"); idx >= 0 {
|
if idx := strings.Index(rest, "%"); idx >= 0 {
|
||||||
rest = rest[idx+1:]
|
rest = rest[idx+1:]
|
||||||
}
|
}
|
||||||
// Look for "at <speed>"
|
|
||||||
if atIdx := strings.Index(rest, " at "); atIdx >= 0 {
|
if atIdx := strings.Index(rest, " at "); atIdx >= 0 {
|
||||||
afterAt := rest[atIdx+4:]
|
afterAt := rest[atIdx+4:]
|
||||||
if spaceIdx := strings.Index(afterAt, " "); spaceIdx >= 0 {
|
if spaceIdx := strings.Index(afterAt, " "); spaceIdx >= 0 {
|
||||||
@@ -51,7 +48,6 @@ func parseProgressLine(line string) *progressUpdate {
|
|||||||
rest = afterAt[spaceIdx:]
|
rest = afterAt[spaceIdx:]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Look for "ETA <duration>"
|
|
||||||
if etaIdx := strings.Index(rest, "ETA "); etaIdx >= 0 {
|
if etaIdx := strings.Index(rest, "ETA "); etaIdx >= 0 {
|
||||||
etaStr := strings.TrimSpace(rest[etaIdx+4:])
|
etaStr := strings.TrimSpace(rest[etaIdx+4:])
|
||||||
u.eta = etaStr
|
u.eta = etaStr
|
||||||
@@ -63,16 +59,29 @@ func parseProgressLine(line string) *progressUpdate {
|
|||||||
// readProgress reads yt-dlp stderr and sends progress updates to the channel.
|
// readProgress reads yt-dlp stderr and sends progress updates to the channel.
|
||||||
// The caller is responsible for closing the updates channel.
|
// The caller is responsible for closing the updates channel.
|
||||||
func readProgress(stderr io.ReadCloser, updates chan<- *domain.DownloadJob, job *domain.DownloadJob) {
|
func readProgress(stderr io.ReadCloser, updates chan<- *domain.DownloadJob, job *domain.DownloadJob) {
|
||||||
|
var stderrBuf strings.Builder
|
||||||
scanner := bufio.NewScanner(stderr)
|
scanner := bufio.NewScanner(stderr)
|
||||||
|
lineCount := 0
|
||||||
|
matchCount := 0
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
|
lineCount++
|
||||||
|
|
||||||
|
if stderrBuf.Len() < 4096 {
|
||||||
|
stderrBuf.WriteString(line)
|
||||||
|
stderrBuf.WriteByte('\n')
|
||||||
|
}
|
||||||
|
|
||||||
parsed := parseProgressLine(line)
|
parsed := parseProgressLine(line)
|
||||||
if parsed == nil {
|
if parsed == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
matchCount++
|
||||||
job.Progress = parsed.pct
|
job.Progress = parsed.pct
|
||||||
job.Speed = parsed.speed
|
job.Speed = parsed.speed
|
||||||
job.ETA = parsed.eta
|
job.ETA = parsed.eta
|
||||||
updates <- job
|
updates <- job
|
||||||
}
|
}
|
||||||
|
job.StderrLog = stderrBuf.String()
|
||||||
|
slog.Info("readProgress finished", "url", job.URL, "lines", lineCount, "matches", matchCount, "stderr_len", len(job.StderrLog))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -265,6 +265,7 @@ func (c *Client) StartDownload(job *domain.DownloadJob, downloadDir string) (<-c
|
|||||||
slog.Info("starting download", "url", job.URL, "format", formatID)
|
slog.Info("starting download", "url", job.URL, "format", formatID)
|
||||||
|
|
||||||
cmd := exec.Command(c.binaryPath, args...)
|
cmd := exec.Command(c.binaryPath, args...)
|
||||||
|
cmd.Env = append(os.Environ(), "PYTHONUNBUFFERED=1")
|
||||||
|
|
||||||
stderr, err := cmd.StderrPipe()
|
stderr, err := cmd.StderrPipe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -315,7 +316,7 @@ func (c *Client) StartDownload(job *domain.DownloadJob, downloadDir string) (<-c
|
|||||||
<-progressDone
|
<-progressDone
|
||||||
if err != nil {
|
if err != nil {
|
||||||
job.Status = domain.StatusFailed
|
job.Status = domain.StatusFailed
|
||||||
slog.Error("download failed", "url", job.URL, "error", err)
|
slog.Error("download failed", "url", job.URL, "error", err, "stderr", job.StderrLog)
|
||||||
} else {
|
} else {
|
||||||
job.Status = domain.StatusCompleted
|
job.Status = domain.StatusCompleted
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,6 +128,7 @@ type DownloadJob struct {
|
|||||||
FilePath string
|
FilePath string
|
||||||
Title string
|
Title string
|
||||||
QuotaApplied bool
|
QuotaApplied bool
|
||||||
|
StderrLog string
|
||||||
CancelCh chan struct{}
|
CancelCh chan struct{}
|
||||||
Cmd *exec.Cmd
|
Cmd *exec.Cmd
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,7 +70,15 @@ func (h *Handler) handleURLInput(ctx context.Context, chatID int64, text string,
|
|||||||
if strings.Contains(errMsg, "DRM") {
|
if strings.Contains(errMsg, "DRM") {
|
||||||
youtubeURL, ytTitle, searchErr := h.YtDlp.SearchYoutube(text)
|
youtubeURL, ytTitle, searchErr := h.YtDlp.SearchYoutube(text)
|
||||||
if searchErr != nil {
|
if searchErr != nil {
|
||||||
h.sendText(ctx, chatID, fmt.Sprintf("This content is DRM protected and no YouTube alternative was found: %s", errMsg))
|
kb := models.InlineKeyboardMarkup{
|
||||||
|
InlineKeyboard: [][]models.InlineKeyboardButton{
|
||||||
|
{{Text: "Search on YouTube", CallbackData: "drm_search"}},
|
||||||
|
{{Text: "Cancel", CallbackData: "pending_cancel"}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
h.Bot.SendMessage(ctx, &tgbot.SendMessageParams{
|
||||||
|
ChatID: chatID, Text: "This content is DRM protected and no YouTube alternative was found automatically. Try searching by name:", ReplyMarkup: kb,
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
h.sendText(ctx, chatID, fmt.Sprintf("This content is DRM protected. Found on YouTube: %s", ytTitle))
|
h.sendText(ctx, chatID, fmt.Sprintf("This content is DRM protected. Found on YouTube: %s", ytTitle))
|
||||||
@@ -388,6 +396,8 @@ func (h *Handler) runDownload(ctx context.Context, job *domain.DownloadJob) {
|
|||||||
|
|
||||||
lastProgress := 0.0
|
lastProgress := 0.0
|
||||||
for upd := range updates {
|
for upd := range updates {
|
||||||
|
slog.Info("progress update", "pct", upd.Progress, "speed", upd.Speed, "status", upd.Status)
|
||||||
|
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
currentJob := h.activeJobs[job.UserID]
|
currentJob := h.activeJobs[job.UserID]
|
||||||
h.mu.Unlock()
|
h.mu.Unlock()
|
||||||
@@ -411,18 +421,23 @@ func (h *Handler) runDownload(ctx context.Context, job *domain.DownloadJob) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if upd.Progress-lastProgress >= domain.ProgressThreshold || upd.Status == domain.StatusCompleted {
|
// Always show the first real progress update, then throttle at 5% intervals
|
||||||
|
if upd.Progress > 0 && lastProgress == 0 {
|
||||||
|
lastProgress = upd.Progress
|
||||||
|
} else if upd.Progress-lastProgress < domain.ProgressThreshold && upd.Status != domain.StatusCompleted {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
lastProgress = upd.Progress
|
lastProgress = upd.Progress
|
||||||
|
|
||||||
if upd.Progress > domain.QuotaProgressThreshold && !job.QuotaApplied && job.FileSize > 0 {
|
|
||||||
job.QuotaApplied = true
|
|
||||||
h.applyQuota(job.UserID, job.FileSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
kb := progressKeyboard(upd.Progress, upd.Speed, upd.ETA)
|
|
||||||
text := fmt.Sprintf("Downloading: %s", job.Title)
|
|
||||||
h.editText(ctx, job.ChatID, job.MessageID, text, &kb)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if upd.Progress > domain.QuotaProgressThreshold && !job.QuotaApplied && job.FileSize > 0 {
|
||||||
|
job.QuotaApplied = true
|
||||||
|
h.applyQuota(job.UserID, job.FileSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
kb := progressKeyboard(upd.Progress, upd.Speed, upd.ETA)
|
||||||
|
text := fmt.Sprintf("Downloading: %s", job.Title)
|
||||||
|
h.editText(ctx, job.ChatID, job.MessageID, text, &kb)
|
||||||
}
|
}
|
||||||
|
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ type PendingKind string
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
PendingURL PendingKind = "url"
|
PendingURL PendingKind = "url"
|
||||||
|
PendingDRM PendingKind = "drm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PendingInput struct {
|
type PendingInput struct {
|
||||||
@@ -235,6 +236,9 @@ func (h *Handler) HandleCallback(ctx context.Context, cb *models.CallbackQuery)
|
|||||||
case data == "deleteaccount":
|
case data == "deleteaccount":
|
||||||
h.deleteAccountPrompt(ctx, chatID, msgID, userID)
|
h.deleteAccountPrompt(ctx, chatID, msgID, userID)
|
||||||
h.answerCb(ctx, cb.ID)
|
h.answerCb(ctx, cb.ID)
|
||||||
|
case data == "drm_search":
|
||||||
|
h.promptForInput(ctx, chatID, msgID, userID, PendingDRM, "Enter a search query to find this content on YouTube:", nil)
|
||||||
|
h.answerCb(ctx, cb.ID)
|
||||||
case data == "pending_cancel":
|
case data == "pending_cancel":
|
||||||
h.backToMenu(ctx, chatID, msgID, cb.ID, userID)
|
h.backToMenu(ctx, chatID, msgID, cb.ID, userID)
|
||||||
default:
|
default:
|
||||||
@@ -296,11 +300,23 @@ func (h *Handler) processPendingInput(ctx context.Context, msg *models.Message,
|
|||||||
switch pi.Kind {
|
switch pi.Kind {
|
||||||
case PendingURL:
|
case PendingURL:
|
||||||
h.handleURLInput(ctx, msg.Chat.ID, text, userID)
|
h.handleURLInput(ctx, msg.Chat.ID, text, userID)
|
||||||
|
case PendingDRM:
|
||||||
|
h.handleDRMSearch(ctx, msg.Chat.ID, text, userID)
|
||||||
default:
|
default:
|
||||||
h.sendText(ctx, msg.Chat.ID, "Unknown input type")
|
h.sendText(ctx, msg.Chat.ID, "Unknown input type")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) handleDRMSearch(ctx context.Context, chatID int64, text string, userID int64) {
|
||||||
|
youtubeURL, ytTitle, err := h.YtDlp.SearchYoutube(text)
|
||||||
|
if err != nil {
|
||||||
|
h.sendText(ctx, chatID, fmt.Sprintf("No results found for '%s'. Try a different search query or send a YouTube URL directly.", text))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.sendText(ctx, chatID, fmt.Sprintf("Found on YouTube: %s", ytTitle))
|
||||||
|
h.handleURLInput(ctx, chatID, youtubeURL, userID)
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) promptForInput(ctx context.Context, chatID int64, msgID int, userID int64, kind PendingKind, prompt string, data map[string]string) {
|
func (h *Handler) promptForInput(ctx context.Context, chatID int64, msgID int, userID int64, kind PendingKind, prompt string, data map[string]string) {
|
||||||
kb := models.InlineKeyboardMarkup{
|
kb := models.InlineKeyboardMarkup{
|
||||||
InlineKeyboard: [][]models.InlineKeyboardButton{
|
InlineKeyboard: [][]models.InlineKeyboardButton{
|
||||||
|
|||||||
Reference in New Issue
Block a user