feat(global, download): add —delay flag

This commit is contained in:
XMLHexagram 2024-03-24 20:03:23 +08:00
parent 0174e6dbed
commit 735bbc74f7
6 changed files with 22 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
@ -38,6 +39,7 @@ type Options struct {
Exclude []string
Desc bool
Takeout bool
Delay time.Duration
// resume opts
Continue, Restart bool
@ -107,6 +109,7 @@ func Run(ctx context.Context, c *telegram.Client, kvd kv.KV, opts Options) (rerr
Threads: viper.GetInt(consts.FlagThreads),
Iter: it,
Progress: newProgress(dlProgress, it, opts),
Delay: viper.GetDuration(consts.FlagDelay),
}
limit := viper.GetInt(consts.FlagLimit)

View File

@ -94,6 +94,14 @@ func newIter(pool dcpool.Pool, manager *peers.Manager, dialog [][]*tmessage.Dial
}, nil
}
func (i *iter) HasNext() bool {
i.mu.Lock()
defer i.mu.Unlock()
j := i.j + 1
return i.i < len(i.dialogs) && j < len(i.dialogs[i.i].Messages)
}
func (i *iter) Next(ctx context.Context) bool {
select {
case <-ctx.Done():

View File

@ -99,6 +99,7 @@ func New() *cobra.Command {
cmd.PersistentFlags().Duration(consts.FlagReconnectTimeout, 5*time.Minute, "Telegram client reconnection backoff timeout, infinite if set to 0") // #158
cmd.PersistentFlags().String(consts.FlagTest, "", "use test Telegram client, only for developer")
cmd.PersistentFlags().Duration(consts.FlagDelay, 0, "delay between each download, forward and upload task")
// completion
_ = cmd.RegisterFlagCompletionFunc(consts.FlagNamespace, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

View File

@ -13,4 +13,5 @@ const (
FlagReconnectTimeout = "reconnect-timeout"
FlagDlTemplate = "template"
FlagTest = "test"
FlagDelay = "delay"
)

View File

@ -2,6 +2,8 @@ package downloader
import (
"context"
"github.com/fatih/color"
"time"
"github.com/go-faster/errors"
"github.com/gotd/td/telegram/downloader"
@ -23,6 +25,7 @@ type Options struct {
Threads int
Iter Iter
Progress Progress
Delay time.Duration
}
func New(opts Options) *Downloader {
@ -51,6 +54,11 @@ func (d *Downloader) Download(ctx context.Context, limit int) error {
// don't return error, just log it
}
if d.opts.Delay != 0 && d.opts.Iter.HasNext() {
color.Yellow("Delay %s", d.opts.Delay.String())
<-time.After(d.opts.Delay)
}
return nil
})
}

View File

@ -9,6 +9,7 @@ import (
type Iter interface {
Next(ctx context.Context) bool
HasNext() bool
Value() Elem
Err() error
}