Compare commits
2 Commits
66a8cd1128
...
dfe946a41b
| Author | SHA1 | Date | |
|---|---|---|---|
|
dfe946a41b
|
|||
|
3290bb55c0
|
@@ -3,7 +3,7 @@ HTTP_PROXY=
|
||||
HTTPS_PROXY=
|
||||
DB_PATH=data/uptodown.db
|
||||
DOWNLOAD_DIR=/tmp/uptodown
|
||||
MAX_FILE_SIZE_MB=2000
|
||||
MAX_FILE_SIZE_MB=50
|
||||
COOKIES_FILE=
|
||||
TZ=UTC
|
||||
BOT_ALLOWED_USERS=
|
||||
|
||||
@@ -41,7 +41,7 @@ Copy `.env.example` to `.env` and configure:
|
||||
| `HTTPS_PROXY` | empty | Outbound HTTPS proxy |
|
||||
| `DB_PATH` | `data/uptodown.db` | SQLite database path |
|
||||
| `DOWNLOAD_DIR` | `/tmp/uptodown` | Temporary download directory |
|
||||
| `MAX_FILE_SIZE_MB` | `2000` | Maximum upload file size in MB |
|
||||
| `MAX_FILE_SIZE_MB` | `50` | Maximum upload file size in MB |
|
||||
| `COOKIES_FILE` | empty | Path to cookies.txt for auth |
|
||||
| `TZ` | `UTC` | Timezone |
|
||||
| `BOT_ALLOWED_USERS` | empty | Comma-separated allowed user IDs |
|
||||
|
||||
@@ -29,7 +29,7 @@ func main() {
|
||||
token := os.Getenv("BOT_TOKEN")
|
||||
dbPath := getEnvDefault("DB_PATH", "data/uptodown.db")
|
||||
downloadDir := getEnvDefault("DOWNLOAD_DIR", "/tmp/uptodown")
|
||||
maxFileSizeMB := getEnvIntDefault("MAX_FILE_SIZE_MB", 2000)
|
||||
maxFileSizeMB := getEnvIntDefault("MAX_FILE_SIZE_MB", 50)
|
||||
cookiesFile := os.Getenv("COOKIES_FILE")
|
||||
|
||||
if token == "" {
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -20,6 +21,36 @@ import (
|
||||
type Client struct {
|
||||
binaryPath string
|
||||
cookiesFile string
|
||||
cache map[string]*cachedMediaInfo
|
||||
cacheMu sync.Mutex
|
||||
}
|
||||
|
||||
type cachedMediaInfo struct {
|
||||
info *domain.MediaInfo
|
||||
ts time.Time
|
||||
}
|
||||
|
||||
const cacheTTL = 30 * time.Minute
|
||||
const cacheMaxEntries = 200
|
||||
|
||||
func (c *Client) getCached(url string) *domain.MediaInfo {
|
||||
c.cacheMu.Lock()
|
||||
defer c.cacheMu.Unlock()
|
||||
entry, ok := c.cache[url]
|
||||
if !ok || time.Since(entry.ts) > cacheTTL {
|
||||
delete(c.cache, url)
|
||||
return nil
|
||||
}
|
||||
return entry.info
|
||||
}
|
||||
|
||||
func (c *Client) setCache(url string, info *domain.MediaInfo) {
|
||||
c.cacheMu.Lock()
|
||||
defer c.cacheMu.Unlock()
|
||||
if len(c.cache) >= cacheMaxEntries {
|
||||
c.cache = make(map[string]*cachedMediaInfo)
|
||||
}
|
||||
c.cache[url] = &cachedMediaInfo{info: info, ts: time.Now()}
|
||||
}
|
||||
|
||||
// NewClient creates a new yt-dlp client. It checks for the binary.
|
||||
@@ -29,7 +60,11 @@ func NewClient(cookiesFile string) (*Client, error) {
|
||||
return nil, fmt.Errorf("yt-dlp not found in PATH: %w", err)
|
||||
}
|
||||
slog.Info("yt-dlp found", "path", path)
|
||||
return &Client{binaryPath: path, cookiesFile: cookiesFile}, nil
|
||||
return &Client{
|
||||
binaryPath: path,
|
||||
cookiesFile: cookiesFile,
|
||||
cache: make(map[string]*cachedMediaInfo),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type ytdlpFormat struct {
|
||||
@@ -70,6 +105,11 @@ type ytdlpPlaylistEntry struct {
|
||||
|
||||
// GetMediaInfo fetches format information for a given URL.
|
||||
func (c *Client) GetMediaInfo(url string) (*domain.MediaInfo, error) {
|
||||
if cached := c.getCached(url); cached != nil {
|
||||
slog.Debug("cache hit", "url", url)
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-J", "--no-download",
|
||||
"--no-warnings",
|
||||
@@ -106,6 +146,7 @@ func (c *Client) GetMediaInfo(url string) (*domain.MediaInfo, error) {
|
||||
if mi.IsPlaylist {
|
||||
mi.PlaylistCount = info.PlaylistCount
|
||||
mi.PlaylistEntries = c.parsePlaylistEntries(info.Entries)
|
||||
c.setCache(url, mi)
|
||||
return mi, nil
|
||||
}
|
||||
|
||||
@@ -124,11 +165,17 @@ func (c *Client) GetMediaInfo(url string) (*domain.MediaInfo, error) {
|
||||
}
|
||||
mi.EstimatedSize = maxSize
|
||||
|
||||
c.setCache(url, mi)
|
||||
return mi, nil
|
||||
}
|
||||
|
||||
// GetPlaylistInfo fetches a flat playlist listing (fast, no format detail).
|
||||
func (c *Client) GetPlaylistInfo(url string) (*domain.MediaInfo, error) {
|
||||
if cached := c.getCached(url); cached != nil {
|
||||
slog.Debug("cache hit", "url", url)
|
||||
return cached, nil
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"-J", "--flat-playlist", "--no-download",
|
||||
"--no-warnings",
|
||||
@@ -159,6 +206,7 @@ func (c *Client) GetPlaylistInfo(url string) (*domain.MediaInfo, error) {
|
||||
PlaylistEntries: c.parsePlaylistEntries(info.Entries),
|
||||
}
|
||||
|
||||
c.setCache(url, mi)
|
||||
return mi, nil
|
||||
}
|
||||
|
||||
@@ -376,6 +424,36 @@ func (c *Client) SearchYoutube(query string) (url, title string, err error) {
|
||||
return info.Entries[0].URL, info.Entries[0].Title, nil
|
||||
}
|
||||
|
||||
// Search does a yt-dlp search and returns entries.
|
||||
func (c *Client) Search(query, source string, limit int) ([]domain.PlaylistEntry, error) {
|
||||
searchPrefix := "ytsearch"
|
||||
if source == "ytmusic" {
|
||||
searchPrefix = "ytmusicsearch"
|
||||
}
|
||||
|
||||
args := []string{
|
||||
"--flat-playlist", "-J", "--no-download",
|
||||
"--no-warnings",
|
||||
fmt.Sprintf("%s%d:%s", searchPrefix, limit, query),
|
||||
}
|
||||
if c.cookiesFile != "" {
|
||||
args = append(args, "--cookies", c.cookiesFile)
|
||||
}
|
||||
|
||||
slog.Info("searching", "query", query, "source", source)
|
||||
out, err := c.run(args)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("search failed: %w", err)
|
||||
}
|
||||
|
||||
var info ytdlpInfo
|
||||
if err := json.Unmarshal(out, &info); err != nil {
|
||||
return nil, fmt.Errorf("parse search result: %w", err)
|
||||
}
|
||||
|
||||
return c.parsePlaylistEntries(info.Entries), nil
|
||||
}
|
||||
|
||||
func (c *Client) run(args []string) ([]byte, error) {
|
||||
cmd := exec.Command(c.binaryPath, args...)
|
||||
var stderr bytes.Buffer
|
||||
|
||||
@@ -6,7 +6,7 @@ const (
|
||||
RateLimitInterval = 1 * time.Second
|
||||
ProgressThreshold = 5.0
|
||||
MaxFileSizeEnvKey = "MAX_FILE_SIZE_MB"
|
||||
DefaultMaxFileSize = 2000
|
||||
DefaultMaxFileSize = 50
|
||||
DefaultDailyQuotaMB = 100
|
||||
DefaultWeeklyQuotaMB = 500
|
||||
DefaultMonthlyQuotaMB = 2000
|
||||
|
||||
@@ -129,6 +129,7 @@ type DownloadJob struct {
|
||||
Title string
|
||||
QuotaApplied bool
|
||||
StderrLog string
|
||||
Done chan struct{}
|
||||
CancelCh chan struct{}
|
||||
Cmd *exec.Cmd
|
||||
}
|
||||
@@ -177,3 +178,12 @@ type PlaylistState struct {
|
||||
PerPage int
|
||||
Selected map[string]bool
|
||||
}
|
||||
|
||||
// SearchState tracks the user's current search results flow.
|
||||
type SearchState struct {
|
||||
Query string
|
||||
Entries []PlaylistEntry
|
||||
Page int
|
||||
PerPage int
|
||||
Source string // "youtube" or "ytmusic"
|
||||
}
|
||||
|
||||
@@ -3,8 +3,10 @@ package handler
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -29,23 +31,24 @@ type stepState struct {
|
||||
Container string
|
||||
FormatIDs []string
|
||||
PlaylistState *domain.PlaylistState
|
||||
SearchState *domain.SearchState
|
||||
}
|
||||
|
||||
func (h *Handler) getUserStepState(chatID int64) *stepState {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.stepStatesMu.Lock()
|
||||
defer h.stepStatesMu.Unlock()
|
||||
return h.stepStates[chatID]
|
||||
}
|
||||
|
||||
func (h *Handler) setUserStepState(chatID int64, ss *stepState) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.stepStatesMu.Lock()
|
||||
defer h.stepStatesMu.Unlock()
|
||||
h.stepStates[chatID] = ss
|
||||
}
|
||||
|
||||
func (h *Handler) clearStepState(chatID int64) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.stepStatesMu.Lock()
|
||||
defer h.stepStatesMu.Unlock()
|
||||
delete(h.stepStates, chatID)
|
||||
}
|
||||
|
||||
@@ -354,6 +357,8 @@ func (h *Handler) handleContainerSelection(ctx context.Context, chatID int64, ms
|
||||
return
|
||||
}
|
||||
|
||||
dlCtx, dlCancel := context.WithCancel(context.Background())
|
||||
|
||||
job := &domain.DownloadJob{
|
||||
ID: h.generateJobID(),
|
||||
UserID: userID,
|
||||
@@ -368,17 +373,33 @@ func (h *Handler) handleContainerSelection(ctx context.Context, chatID int64, ms
|
||||
Status: domain.StatusDownloading,
|
||||
Title: ss.MediaInfo.Title,
|
||||
FileSize: fileSize,
|
||||
Done: make(chan struct{}),
|
||||
}
|
||||
|
||||
h.editText(ctx, chatID, msgID, "Processing...", nil)
|
||||
h.setActiveJob(userID, job)
|
||||
h.setCancelFunc(userID, dlCancel)
|
||||
h.clearStepState(chatID)
|
||||
|
||||
go h.runDownload(context.Background(), job)
|
||||
go h.runDownload(dlCtx, job)
|
||||
}
|
||||
|
||||
// runDownload manages the download lifecycle, progress, and completion.
|
||||
func (h *Handler) runDownload(ctx context.Context, job *domain.DownloadJob) {
|
||||
defer func() {
|
||||
if job.Done != nil {
|
||||
close(job.Done)
|
||||
}
|
||||
}()
|
||||
|
||||
// Acquire semaphore slot or wait for cancellation
|
||||
select {
|
||||
case h.downloadSem <- struct{}{}:
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
defer func() { <-h.downloadSem }()
|
||||
|
||||
kb := progressKeyboard(0, "", "")
|
||||
msg, err := h.Bot.SendMessage(ctx, &tgbot.SendMessageParams{
|
||||
ChatID: job.ChatID,
|
||||
@@ -395,61 +416,77 @@ func (h *Handler) runDownload(ctx context.Context, job *domain.DownloadJob) {
|
||||
if err != nil {
|
||||
h.editText(ctx, job.ChatID, job.MessageID, fmt.Sprintf("Download failed: %s", err.Error()), nil)
|
||||
job.Status = domain.StatusFailed
|
||||
h.mu.Lock()
|
||||
h.activeJobsMu.Lock()
|
||||
delete(h.activeJobs, job.UserID)
|
||||
h.mu.Unlock()
|
||||
h.activeJobsMu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
lastProgress := 0.0
|
||||
for upd := range updates {
|
||||
slog.Info("progress update", "pct", upd.Progress, "speed", upd.Speed, "status", upd.Status)
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
case upd, ok := <-updates:
|
||||
if !ok {
|
||||
break loop
|
||||
}
|
||||
slog.Info("progress update", "pct", upd.Progress, "speed", upd.Speed, "status", upd.Status)
|
||||
|
||||
h.mu.Lock()
|
||||
currentJob := h.activeJobs[job.UserID]
|
||||
h.mu.Unlock()
|
||||
if currentJob == nil {
|
||||
return
|
||||
h.activeJobsMu.Lock()
|
||||
currentJob := h.activeJobs[job.UserID]
|
||||
h.activeJobsMu.Unlock()
|
||||
if currentJob == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if upd.Status == domain.StatusCancelled {
|
||||
h.editText(ctx, job.ChatID, job.MessageID, "Download cancelled.", nil)
|
||||
h.activeJobsMu.Lock()
|
||||
delete(h.activeJobs, job.UserID)
|
||||
h.activeJobsMu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
if upd.Status == domain.StatusFailed {
|
||||
h.editText(ctx, job.ChatID, job.MessageID, "Download failed.", nil)
|
||||
h.activeJobsMu.Lock()
|
||||
delete(h.activeJobs, job.UserID)
|
||||
h.activeJobsMu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
case <-ctx.Done():
|
||||
dl.CancelDownload(job)
|
||||
for range updates {
|
||||
}
|
||||
break loop
|
||||
}
|
||||
|
||||
if upd.Status == domain.StatusCancelled {
|
||||
h.editText(ctx, job.ChatID, job.MessageID, "Download cancelled.", nil)
|
||||
h.mu.Lock()
|
||||
delete(h.activeJobs, job.UserID)
|
||||
h.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
if upd.Status == domain.StatusFailed {
|
||||
h.editText(ctx, job.ChatID, job.MessageID, "Download failed.", nil)
|
||||
h.mu.Lock()
|
||||
delete(h.activeJobs, job.UserID)
|
||||
h.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
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.activeJobsMu.Lock()
|
||||
delete(h.activeJobs, job.UserID)
|
||||
h.mu.Unlock()
|
||||
h.activeJobsMu.Unlock()
|
||||
|
||||
h.cancelFuncsMu.Lock()
|
||||
delete(h.cancelFuncs, job.UserID)
|
||||
h.cancelFuncsMu.Unlock()
|
||||
|
||||
if job.Status == domain.StatusCompleted {
|
||||
pattern := filepath.Join(h.downloadDir, job.ID+"_*")
|
||||
@@ -517,12 +554,19 @@ func (h *Handler) sendDocument(ctx context.Context, chatID int64, filePath, titl
|
||||
func (h *Handler) handleCancelDownload(ctx context.Context, chatID int64, msgID int, cbID string, userID int64) {
|
||||
defer h.answerCb(ctx, cbID)
|
||||
|
||||
h.cancelFuncsMu.Lock()
|
||||
if cancel, ok := h.cancelFuncs[userID]; ok {
|
||||
cancel()
|
||||
delete(h.cancelFuncs, userID)
|
||||
}
|
||||
h.cancelFuncsMu.Unlock()
|
||||
|
||||
job := h.getActiveJob(userID)
|
||||
if job != nil {
|
||||
dl.CancelDownload(job)
|
||||
h.mu.Lock()
|
||||
h.activeJobsMu.Lock()
|
||||
delete(h.activeJobs, userID)
|
||||
h.mu.Unlock()
|
||||
h.activeJobsMu.Unlock()
|
||||
h.editText(ctx, chatID, msgID, "Download cancelled.", nil)
|
||||
return
|
||||
}
|
||||
@@ -646,21 +690,71 @@ func (h *Handler) sendMediaPreview(ctx context.Context, chatID int64, info *doma
|
||||
}
|
||||
|
||||
if info.Thumbnail != "" {
|
||||
photo := &models.InputFileString{Data: info.Thumbnail}
|
||||
_, err := h.Bot.SendPhoto(ctx, &tgbot.SendPhotoParams{
|
||||
ChatID: chatID,
|
||||
Photo: photo,
|
||||
Caption: caption,
|
||||
thumbPath, err := downloadFile(info.Thumbnail, h.downloadDir)
|
||||
if err != nil {
|
||||
slog.Warn("download thumbnail failed, falling back to text", "url", info.Thumbnail, "error", err)
|
||||
h.sendText(ctx, chatID, caption)
|
||||
return
|
||||
}
|
||||
defer os.Remove(thumbPath)
|
||||
|
||||
f, err := os.Open(thumbPath)
|
||||
if err != nil {
|
||||
slog.Warn("open thumbnail failed, falling back to text", "error", err)
|
||||
h.sendText(ctx, chatID, caption)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
photo := &models.InputFileUpload{Filename: "thumb.jpg", Data: f}
|
||||
_, err = h.Bot.SendPhoto(ctx, &tgbot.SendPhotoParams{
|
||||
ChatID: chatID, Photo: photo, Caption: caption,
|
||||
})
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
slog.Warn("send thumbnail failed, falling back to text", "error", err)
|
||||
slog.Warn("send thumbnail photo failed, falling back to text", "error", err)
|
||||
}
|
||||
|
||||
h.sendText(ctx, chatID, caption)
|
||||
}
|
||||
|
||||
// downloadFile downloads a URL to a temp file in the given directory and returns the path.
|
||||
func downloadFile(url, dir string) (string, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("http get: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("unexpected status: %s", resp.Status)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||
return "", fmt.Errorf("create download dir: %w", err)
|
||||
}
|
||||
|
||||
f, err := os.CreateTemp(dir, "thumb_*")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create temp file: %w", err)
|
||||
}
|
||||
|
||||
_, err = io.Copy(f, resp.Body)
|
||||
if err != nil {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
return "", fmt.Errorf("write temp file: %w", err)
|
||||
}
|
||||
|
||||
if err := f.Close(); err != nil {
|
||||
os.Remove(f.Name())
|
||||
return "", fmt.Errorf("close temp file: %w", err)
|
||||
}
|
||||
|
||||
return f.Name(), nil
|
||||
}
|
||||
|
||||
func formatDuration(seconds float64) string {
|
||||
if seconds <= 0 {
|
||||
return "0:00"
|
||||
|
||||
@@ -23,8 +23,9 @@ import (
|
||||
type PendingKind string
|
||||
|
||||
const (
|
||||
PendingURL PendingKind = "url"
|
||||
PendingDRM PendingKind = "drm"
|
||||
PendingURL PendingKind = "url"
|
||||
PendingDRM PendingKind = "drm"
|
||||
PendingSearch PendingKind = "search"
|
||||
)
|
||||
|
||||
type PendingInput struct {
|
||||
@@ -35,18 +36,29 @@ type PendingInput struct {
|
||||
Data map[string]string
|
||||
}
|
||||
|
||||
const maxConcurrentDownloads = 3
|
||||
|
||||
type Handler struct {
|
||||
Bot *tgbot.Bot
|
||||
Repo repo.Repository
|
||||
YtDlp *dl.Client
|
||||
AllowedUsers map[int64]bool
|
||||
|
||||
activeJobs map[int64]*domain.DownloadJob
|
||||
stepStates map[int64]*stepState
|
||||
lastMsgTime map[int64]time.Time
|
||||
pendingInput map[int64]*PendingInput
|
||||
mu sync.Mutex
|
||||
downloadDir string
|
||||
maxFileSize int64
|
||||
cancelFuncs map[int64]context.CancelFunc
|
||||
|
||||
activeJobsMu sync.Mutex
|
||||
stepStatesMu sync.Mutex
|
||||
rateLimiterMu sync.Mutex
|
||||
pendingInputMu sync.Mutex
|
||||
cancelFuncsMu sync.Mutex
|
||||
|
||||
downloadDir string
|
||||
maxFileSize int64
|
||||
downloadSem chan struct{}
|
||||
}
|
||||
|
||||
func NewHandler(bot *tgbot.Bot, store repo.Repository, ytdlp *dl.Client, allowed map[int64]bool, downloadDir string, maxFileSize int64) *Handler {
|
||||
@@ -59,8 +71,10 @@ func NewHandler(bot *tgbot.Bot, store repo.Repository, ytdlp *dl.Client, allowed
|
||||
stepStates: make(map[int64]*stepState),
|
||||
lastMsgTime: make(map[int64]time.Time),
|
||||
pendingInput: make(map[int64]*PendingInput),
|
||||
cancelFuncs: make(map[int64]context.CancelFunc),
|
||||
downloadDir: downloadDir,
|
||||
maxFileSize: maxFileSize,
|
||||
downloadSem: make(chan struct{}, maxConcurrentDownloads),
|
||||
}
|
||||
go h.periodicCleanup()
|
||||
return h
|
||||
@@ -69,27 +83,29 @@ func NewHandler(bot *tgbot.Bot, store repo.Repository, ytdlp *dl.Client, allowed
|
||||
func (h *Handler) periodicCleanup() {
|
||||
for {
|
||||
time.Sleep(domain.CleanupInterval)
|
||||
h.mu.Lock()
|
||||
|
||||
// Clean rate limit entries
|
||||
cutoff := time.Now().Add(-domain.RateLimitInterval * 2)
|
||||
for uid, t := range h.lastMsgTime {
|
||||
if t.Before(cutoff) {
|
||||
delete(h.lastMsgTime, uid)
|
||||
func() {
|
||||
h.rateLimiterMu.Lock()
|
||||
defer h.rateLimiterMu.Unlock()
|
||||
cutoff := time.Now().Add(-domain.RateLimitInterval * 2)
|
||||
for uid, t := range h.lastMsgTime {
|
||||
if t.Before(cutoff) {
|
||||
delete(h.lastMsgTime, uid)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Clean stale pending inputs
|
||||
pendingCutoff := time.Now().Add(-5 * time.Minute)
|
||||
for chatID, pi := range h.pendingInput {
|
||||
if pi.CreatedAt.Before(pendingCutoff) {
|
||||
delete(h.pendingInput, chatID)
|
||||
func() {
|
||||
h.pendingInputMu.Lock()
|
||||
defer h.pendingInputMu.Unlock()
|
||||
pendingCutoff := time.Now().Add(-5 * time.Minute)
|
||||
for chatID, pi := range h.pendingInput {
|
||||
if pi.CreatedAt.Before(pendingCutoff) {
|
||||
delete(h.pendingInput, chatID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
h.mu.Unlock()
|
||||
|
||||
// Clean old temp files (>1h)
|
||||
h.cleanOldTempFiles()
|
||||
}
|
||||
}
|
||||
@@ -115,8 +131,8 @@ func (h *Handler) cleanOldTempFiles() {
|
||||
}
|
||||
|
||||
func (h *Handler) isRateLimited(userID int64) bool {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.rateLimiterMu.Lock()
|
||||
defer h.rateLimiterMu.Unlock()
|
||||
now := time.Now()
|
||||
if last, ok := h.lastMsgTime[userID]; ok && now.Sub(last) < domain.RateLimitInterval {
|
||||
return true
|
||||
@@ -146,19 +162,19 @@ func (h *Handler) HandleMessage(ctx context.Context, msg *models.Message) {
|
||||
slog.Info("message", "chat_id", msg.Chat.ID, "text", msg.Text)
|
||||
|
||||
if msg.Text[0] == '/' {
|
||||
h.mu.Lock()
|
||||
h.pendingInputMu.Lock()
|
||||
delete(h.pendingInput, msg.Chat.ID)
|
||||
h.mu.Unlock()
|
||||
h.pendingInputMu.Unlock()
|
||||
h.routeCommand(ctx, msg, userID)
|
||||
return
|
||||
}
|
||||
|
||||
h.mu.Lock()
|
||||
h.pendingInputMu.Lock()
|
||||
pi, hasPI := h.pendingInput[msg.Chat.ID]
|
||||
if hasPI {
|
||||
delete(h.pendingInput, msg.Chat.ID)
|
||||
}
|
||||
h.mu.Unlock()
|
||||
h.pendingInputMu.Unlock()
|
||||
|
||||
if hasPI {
|
||||
h.processPendingInput(ctx, msg, userID, pi)
|
||||
@@ -185,6 +201,10 @@ func (h *Handler) routeCommand(ctx context.Context, msg *models.Message, userID
|
||||
h.handleHistory(ctx, msg, userID)
|
||||
case "/download":
|
||||
h.handleDownloadCommand(ctx, msg, userID)
|
||||
case "/search":
|
||||
h.handleSearchCommand(ctx, msg, userID)
|
||||
case "/music":
|
||||
h.handleMusicSearchCommand(ctx, msg, userID)
|
||||
default:
|
||||
h.sendText(ctx, msg.Chat.ID, "Unknown command")
|
||||
}
|
||||
@@ -207,9 +227,9 @@ func (h *Handler) HandleCallback(ctx context.Context, cb *models.CallbackQuery)
|
||||
}
|
||||
slog.Info("callback", "chat_id", chatID, "data", cb.Data)
|
||||
|
||||
h.mu.Lock()
|
||||
h.pendingInputMu.Lock()
|
||||
delete(h.pendingInput, chatID)
|
||||
h.mu.Unlock()
|
||||
h.pendingInputMu.Unlock()
|
||||
|
||||
msgID := cb.Message.Message.ID
|
||||
data := cb.Data
|
||||
@@ -226,6 +246,9 @@ func (h *Handler) HandleCallback(ctx context.Context, cb *models.CallbackQuery)
|
||||
case data == "download":
|
||||
h.handleDownloadPrompt(ctx, chatID, msgID, cb.ID, userID)
|
||||
h.answerCb(ctx, cb.ID)
|
||||
case data == "search":
|
||||
h.promptForInput(ctx, chatID, msgID, userID, PendingSearch, "Enter a YouTube search query:", map[string]string{"source": "youtube"})
|
||||
h.answerCb(ctx, cb.ID)
|
||||
case data == "settings":
|
||||
h.settingsCallback(ctx, chatID, msgID, cb.ID, userID)
|
||||
case data == "help":
|
||||
@@ -280,11 +303,13 @@ func (h *Handler) routePrefixedCallback(ctx context.Context, chatID int64, msgID
|
||||
h.handleSettingsSet(ctx, chatID, msgID, userID, data[13:])
|
||||
case strings.HasPrefix(data, "settings_"):
|
||||
h.handleSettingsSelection(ctx, chatID, msgID, userID, data[9:])
|
||||
case strings.HasPrefix(data, "search_"):
|
||||
h.handleSearchCallback(ctx, chatID, msgID, userID, data[7:])
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) setPending(ctx context.Context, chatID int64, msgID int, userID int64, kind PendingKind, data map[string]string) {
|
||||
h.mu.Lock()
|
||||
h.pendingInputMu.Lock()
|
||||
h.pendingInput[chatID] = &PendingInput{
|
||||
Kind: kind,
|
||||
UserID: userID,
|
||||
@@ -292,7 +317,7 @@ func (h *Handler) setPending(ctx context.Context, chatID int64, msgID int, userI
|
||||
MsgID: msgID,
|
||||
Data: data,
|
||||
}
|
||||
h.mu.Unlock()
|
||||
h.pendingInputMu.Unlock()
|
||||
}
|
||||
|
||||
func (h *Handler) processPendingInput(ctx context.Context, msg *models.Message, userID int64, pi *PendingInput) {
|
||||
@@ -302,6 +327,12 @@ func (h *Handler) processPendingInput(ctx context.Context, msg *models.Message,
|
||||
h.handleURLInput(ctx, msg.Chat.ID, text, userID)
|
||||
case PendingDRM:
|
||||
h.handleDRMSearch(ctx, msg.Chat.ID, text, userID)
|
||||
case PendingSearch:
|
||||
source := "youtube"
|
||||
if pi.Data != nil && pi.Data["source"] != "" {
|
||||
source = pi.Data["source"]
|
||||
}
|
||||
h.handleSearchQuery(ctx, msg.Chat.ID, text, userID, source)
|
||||
default:
|
||||
h.sendText(ctx, msg.Chat.ID, "Unknown input type")
|
||||
}
|
||||
@@ -396,26 +427,40 @@ func (h *Handler) backToMenu(ctx context.Context, chatID int64, msgID int, cbID
|
||||
}
|
||||
|
||||
func (h *Handler) clearActiveJob(userID int64) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if job, ok := h.activeJobs[userID]; ok {
|
||||
h.activeJobsMu.Lock()
|
||||
job, ok := h.activeJobs[userID]
|
||||
if ok {
|
||||
dl.CancelDownload(job)
|
||||
delete(h.activeJobs, userID)
|
||||
}
|
||||
h.activeJobsMu.Unlock()
|
||||
|
||||
h.cancelFuncsMu.Lock()
|
||||
if cancel, ok := h.cancelFuncs[userID]; ok {
|
||||
cancel()
|
||||
delete(h.cancelFuncs, userID)
|
||||
}
|
||||
h.cancelFuncsMu.Unlock()
|
||||
}
|
||||
|
||||
func (h *Handler) getActiveJob(userID int64) *domain.DownloadJob {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.activeJobsMu.Lock()
|
||||
defer h.activeJobsMu.Unlock()
|
||||
return h.activeJobs[userID]
|
||||
}
|
||||
|
||||
func (h *Handler) setActiveJob(userID int64, job *domain.DownloadJob) {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.activeJobsMu.Lock()
|
||||
defer h.activeJobsMu.Unlock()
|
||||
h.activeJobs[userID] = job
|
||||
}
|
||||
|
||||
func (h *Handler) setCancelFunc(userID int64, cancel context.CancelFunc) {
|
||||
h.cancelFuncsMu.Lock()
|
||||
defer h.cancelFuncsMu.Unlock()
|
||||
h.cancelFuncs[userID] = cancel
|
||||
}
|
||||
|
||||
func formatBytes(bytes int64) string {
|
||||
const unit = 1024
|
||||
if bytes < unit {
|
||||
@@ -439,7 +484,7 @@ func formatBytes(bytes int64) string {
|
||||
}
|
||||
|
||||
func (h *Handler) generateJobID() string {
|
||||
return uuid.NewString()[:8]
|
||||
return uuid.NewString()
|
||||
}
|
||||
|
||||
// handleStart shows the main menu and cleans up any stale state.
|
||||
@@ -457,6 +502,8 @@ func (h *Handler) handleHelp(ctx context.Context, msg *models.Message) {
|
||||
/settings - Open settings
|
||||
/help - Show this help
|
||||
/history - View download history
|
||||
/search - Search YouTube
|
||||
/music - Search YouTube Music
|
||||
|
||||
Just send me a URL to start downloading.`
|
||||
h.sendText(ctx, msg.Chat.ID, text)
|
||||
|
||||
@@ -14,6 +14,7 @@ func mainKeyboard() models.InlineKeyboardMarkup {
|
||||
InlineKeyboard: [][]models.InlineKeyboardButton{
|
||||
{
|
||||
{Text: "Download", CallbackData: "download"},
|
||||
{Text: "Search", CallbackData: "search"},
|
||||
{Text: "Settings", CallbackData: "settings"},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"uptodownBot/internal/domain"
|
||||
)
|
||||
@@ -164,6 +163,8 @@ func (h *Handler) handlePlaylistConfirm(ctx context.Context, chatID int64, msgID
|
||||
}
|
||||
}
|
||||
|
||||
dlCtx, dlCancel := context.WithCancel(context.Background())
|
||||
|
||||
job := &domain.DownloadJob{
|
||||
ID: h.generateJobID(),
|
||||
UserID: userID,
|
||||
@@ -176,19 +177,17 @@ func (h *Handler) handlePlaylistConfirm(ctx context.Context, chatID int64, msgID
|
||||
Status: domain.StatusDownloading,
|
||||
Title: entry.Title,
|
||||
FileSize: format.Filesize,
|
||||
Done: make(chan struct{}),
|
||||
}
|
||||
|
||||
h.setActiveJob(userID, job)
|
||||
h.runDownload(context.Background(), job)
|
||||
h.setCancelFunc(userID, dlCancel)
|
||||
|
||||
// Wait for active job to complete
|
||||
for {
|
||||
j := h.getActiveJob(userID)
|
||||
if j == nil || j.Status == domain.StatusCompleted || j.Status == domain.StatusFailed || j.Status == domain.StatusCancelled {
|
||||
break
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
h.runDownload(dlCtx, job)
|
||||
|
||||
// runDownload is synchronous — it blocks until the download completes.
|
||||
// The Done channel is closed inside runDownload when it exits.
|
||||
// No busy-polling needed.
|
||||
}
|
||||
|
||||
h.sendText(ctx, chatID, "All playlist downloads completed.")
|
||||
|
||||
136
internal/handler/search.go
Normal file
136
internal/handler/search.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-telegram/bot/models"
|
||||
|
||||
"uptodownBot/internal/domain"
|
||||
)
|
||||
|
||||
func (h *Handler) handleSearchCommand(ctx context.Context, msg *models.Message, userID int64) {
|
||||
h.promptForInput(ctx, msg.Chat.ID, 0, userID, PendingSearch,
|
||||
"Enter a YouTube search query:", map[string]string{"source": "youtube"})
|
||||
}
|
||||
|
||||
func (h *Handler) handleMusicSearchCommand(ctx context.Context, msg *models.Message, userID int64) {
|
||||
h.promptForInput(ctx, msg.Chat.ID, 0, userID, PendingSearch,
|
||||
"Enter a YouTube Music search query:", map[string]string{"source": "ytmusic"})
|
||||
}
|
||||
|
||||
func (h *Handler) handleSearchQuery(ctx context.Context, chatID int64, text string, userID int64, source string) {
|
||||
results, err := h.YtDlp.Search(text, source, 10)
|
||||
if err != nil {
|
||||
h.sendText(ctx, chatID, fmt.Sprintf("Search failed: %s", err.Error()))
|
||||
return
|
||||
}
|
||||
if len(results) == 0 {
|
||||
h.sendText(ctx, chatID, "No results found.")
|
||||
return
|
||||
}
|
||||
|
||||
state := &domain.SearchState{
|
||||
Query: text,
|
||||
Entries: results,
|
||||
Page: 0,
|
||||
PerPage: 5,
|
||||
Source: source,
|
||||
}
|
||||
ss := &stepState{SearchState: state}
|
||||
h.setUserStepState(chatID, ss)
|
||||
|
||||
kb := searchResultsKeyboard(state)
|
||||
h.sendWithKB(ctx, chatID, fmt.Sprintf("Search results for '%s':", text), kb)
|
||||
}
|
||||
|
||||
func (h *Handler) handleSearchCallback(ctx context.Context, chatID int64, msgID int, userID int64, data string) {
|
||||
switch {
|
||||
case data == "refresh":
|
||||
ss := h.getUserStepState(chatID)
|
||||
if ss == nil || ss.SearchState == nil {
|
||||
return
|
||||
}
|
||||
h.handleSearchQuery(ctx, chatID, ss.SearchState.Query, userID, ss.SearchState.Source)
|
||||
case strings.HasPrefix(data, "page_"):
|
||||
h.handleSearchPage(ctx, chatID, msgID, userID, data[5:])
|
||||
case strings.HasPrefix(data, "select_"):
|
||||
h.handleSearchSelect(ctx, chatID, msgID, userID, data[7:])
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) handleSearchPage(ctx context.Context, chatID int64, msgID int, userID int64, pageStr string) {
|
||||
ss := h.getUserStepState(chatID)
|
||||
if ss == nil || ss.SearchState == nil {
|
||||
return
|
||||
}
|
||||
page, err := strconv.Atoi(pageStr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
totalPages := (len(ss.SearchState.Entries) + ss.SearchState.PerPage - 1) / ss.SearchState.PerPage
|
||||
if page < 0 || page >= totalPages {
|
||||
return
|
||||
}
|
||||
ss.SearchState.Page = page
|
||||
h.setUserStepState(chatID, ss)
|
||||
|
||||
kb := searchResultsKeyboard(ss.SearchState)
|
||||
h.editText(ctx, chatID, msgID, fmt.Sprintf("Search results for '%s':", ss.SearchState.Query), &kb)
|
||||
}
|
||||
|
||||
func (h *Handler) handleSearchSelect(ctx context.Context, chatID int64, msgID int, userID int64, entryURL string) {
|
||||
h.clearStepState(chatID)
|
||||
h.editText(ctx, chatID, msgID, "Fetching media info...", nil)
|
||||
h.handleURLInput(ctx, chatID, entryURL, userID)
|
||||
}
|
||||
|
||||
func searchResultsKeyboard(state *domain.SearchState) models.InlineKeyboardMarkup {
|
||||
totalPages := (len(state.Entries) + state.PerPage - 1) / state.PerPage
|
||||
if totalPages < 1 {
|
||||
totalPages = 1
|
||||
}
|
||||
|
||||
rows := [][]models.InlineKeyboardButton{}
|
||||
start := state.Page * state.PerPage
|
||||
end := start + state.PerPage
|
||||
if end > len(state.Entries) {
|
||||
end = len(state.Entries)
|
||||
}
|
||||
|
||||
for i := start; i < end; i++ {
|
||||
e := state.Entries[i]
|
||||
title := e.Title
|
||||
if len(title) > 40 {
|
||||
title = title[:37] + "..."
|
||||
}
|
||||
label := title
|
||||
if e.Duration > 0 {
|
||||
label += fmt.Sprintf(" (%s)", formatDuration(e.Duration))
|
||||
}
|
||||
rows = append(rows, []models.InlineKeyboardButton{
|
||||
{Text: label, CallbackData: "search_select_" + e.URL},
|
||||
})
|
||||
}
|
||||
|
||||
navRow := []models.InlineKeyboardButton{}
|
||||
navRow = append(navRow, models.InlineKeyboardButton{
|
||||
Text: "<", CallbackData: fmt.Sprintf("search_page_%d", state.Page-1),
|
||||
})
|
||||
navRow = append(navRow, models.InlineKeyboardButton{
|
||||
Text: fmt.Sprintf("Page %d/%d", state.Page+1, totalPages), CallbackData: "noop",
|
||||
})
|
||||
navRow = append(navRow, models.InlineKeyboardButton{
|
||||
Text: ">", CallbackData: fmt.Sprintf("search_page_%d", state.Page+1),
|
||||
})
|
||||
rows = append(rows, navRow)
|
||||
|
||||
rows = append(rows, []models.InlineKeyboardButton{
|
||||
{Text: "New Search", CallbackData: "search_refresh"},
|
||||
{Text: "Cancel", CallbackData: "cancel_dl"},
|
||||
})
|
||||
|
||||
return models.InlineKeyboardMarkup{InlineKeyboard: rows}
|
||||
}
|
||||
Reference in New Issue
Block a user