mirror of
https://github.com/iyear/tdl
synced 2025-01-09 04:17:35 +08:00
feat(dl): skip same file
This commit is contained in:
parent
b13362816b
commit
32798a4953
@ -8,7 +8,7 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func Run(ctx context.Context, dir string, rewriteExt bool, template string, urls, files, include, exclude []string) error {
|
||||
func Run(ctx context.Context, dir string, rewriteExt, skipSame bool, template string, urls, files, include, exclude []string) error {
|
||||
c, kvd, err := tgc.NoLogin()
|
||||
if err != nil {
|
||||
return err
|
||||
@ -29,7 +29,8 @@ func Run(ctx context.Context, dir string, rewriteExt bool, template string, urls
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return downloader.New(c.API(), dir, rewriteExt, viper.GetInt(consts.FlagPartSize), viper.GetInt(consts.FlagThreads), it).
|
||||
return downloader.New(c.API(), dir, rewriteExt, skipSame,
|
||||
viper.GetInt(consts.FlagPartSize), viper.GetInt(consts.FlagThreads), it).
|
||||
Download(ctx, viper.GetInt(consts.FlagLimit))
|
||||
})
|
||||
}
|
||||
|
12
cmd/dl/dl.go
12
cmd/dl/dl.go
@ -10,10 +10,10 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
urls, files []string
|
||||
include, exclude []string
|
||||
dir string
|
||||
rewriteExt bool
|
||||
urls, files []string
|
||||
include, exclude []string
|
||||
dir string
|
||||
rewriteExt, skipSame bool
|
||||
)
|
||||
|
||||
var Cmd = &cobra.Command{
|
||||
@ -31,7 +31,7 @@ var Cmd = &cobra.Command{
|
||||
return err
|
||||
}
|
||||
|
||||
return dl.Run(cmd.Context(), dir, rewriteExt,
|
||||
return dl.Run(cmd.Context(), dir, rewriteExt, skipSame,
|
||||
viper.GetString(consts.FlagDlTemplate),
|
||||
urls, files, include, exclude)
|
||||
},
|
||||
@ -47,6 +47,8 @@ func init() {
|
||||
|
||||
Cmd.Flags().StringVarP(&dir, consts.FlagDlDir, "d", "downloads", "specify the download directory. If the directory does not exist, it will be created automatically")
|
||||
Cmd.Flags().BoolVar(&rewriteExt, consts.FlagDlRewriteExt, false, "rewrite file extension according to file header MIME")
|
||||
// do not match extension, because some files' extension is corrected by --rewrite-ext flag
|
||||
Cmd.Flags().BoolVar(&skipSame, consts.FlagDlSkipSame, false, "skip files with the same name(without extension) and size")
|
||||
|
||||
_ = viper.BindPFlag(consts.FlagDlTemplate, Cmd.Flags().Lookup(consts.FlagDlTemplate))
|
||||
}
|
||||
|
@ -19,4 +19,5 @@ const (
|
||||
FlagDlExclude = "exclude"
|
||||
FlagDlDir = "dir"
|
||||
FlagDlRewriteExt = "rewrite-ext"
|
||||
FlagDlSkipSame = "skip-same"
|
||||
)
|
||||
|
@ -23,16 +23,16 @@ const TempExt = ".tmp"
|
||||
var formatter = utils.Byte.FormatBinaryBytes
|
||||
|
||||
type Downloader struct {
|
||||
client *tg.Client
|
||||
pw progress.Writer
|
||||
partSize int
|
||||
threads int
|
||||
iter Iter
|
||||
dir string
|
||||
rewriteExt bool
|
||||
client *tg.Client
|
||||
pw progress.Writer
|
||||
partSize int
|
||||
threads int
|
||||
iter Iter
|
||||
dir string
|
||||
rewriteExt, skipSame bool
|
||||
}
|
||||
|
||||
func New(client *tg.Client, dir string, rewriteExt bool, partSize int, threads int, iter Iter) *Downloader {
|
||||
func New(client *tg.Client, dir string, rewriteExt, skipSame bool, partSize int, threads int, iter Iter) *Downloader {
|
||||
return &Downloader{
|
||||
client: client,
|
||||
pw: prog.New(formatter),
|
||||
@ -41,6 +41,7 @@ func New(client *tg.Client, dir string, rewriteExt bool, partSize int, threads i
|
||||
iter: iter,
|
||||
dir: dir,
|
||||
rewriteExt: rewriteExt,
|
||||
skipSame: skipSame,
|
||||
}
|
||||
}
|
||||
|
||||
@ -109,6 +110,14 @@ func (d *Downloader) download(ctx context.Context, item *Item) error {
|
||||
}
|
||||
|
||||
name := replacer.Replace(item.Name)
|
||||
if d.skipSame {
|
||||
if stat, err := os.Stat(filepath.Join(d.dir, name)); err == nil {
|
||||
if utils.FS.GetNameWithoutExt(name) == utils.FS.GetNameWithoutExt(stat.Name()) &&
|
||||
stat.Size() == item.Size {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
tracker := prog.AppendTracker(d.pw, formatter, name, item.Size)
|
||||
filename := fmt.Sprintf("%s%s", name, TempExt)
|
||||
path := filepath.Join(d.dir, filename)
|
||||
|
Loading…
Reference in New Issue
Block a user