From 5faea0a87b14834582ba0f10a58801094050be13 Mon Sep 17 00:00:00 2001 From: 52funny Date: Mon, 23 Sep 2024 00:00:42 +0800 Subject: [PATCH 1/4] feat: using fastdown library to download --- cmd/download/download.go | 294 ++++++++++++--------------------------- go.mod | 5 +- go.sum | 6 +- 3 files changed, 94 insertions(+), 211 deletions(-) diff --git a/cmd/download/download.go b/cmd/download/download.go index 8c3e933..85dfb2a 100644 --- a/cmd/download/download.go +++ b/cmd/download/download.go @@ -4,35 +4,16 @@ import ( "os" "path/filepath" "strconv" - "unicode/utf8" + "sync" + "github.com/52funny/fastdown" "github.com/52funny/pikpakcli/conf" "github.com/52funny/pikpakcli/internal/pikpak" "github.com/52funny/pikpakcli/internal/utils" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/vbauerster/mpb/v8" - "github.com/vbauerster/mpb/v8/decor" ) -var DownloadCmd = &cobra.Command{ - Use: "download", - Aliases: []string{"d"}, - Short: `Download file from pikpak server`, - Run: func(cmd *cobra.Command, args []string) { - p := pikpak.NewPikPak(conf.Config.Username, conf.Config.Password) - err := p.Login() - if err != nil { - logrus.Errorln("Login Failed:", err) - } - if len(args) > 0 { - downloadFile(&p, args) - } else { - downloadFolder(&p) - } - }, -} - // Number of simultaneous downloads // // default 1 @@ -51,11 +32,6 @@ var parentId string // default current directory (.) var output string -// Progress bar -// -// default false -var progress bool - type warpFile struct { f *pikpak.File output string @@ -71,78 +47,74 @@ func init() { DownloadCmd.Flags().StringVarP(&output, "output", "o", ".", "output directory") DownloadCmd.Flags().StringVarP(&folder, "path", "p", "/", "specific the folder of the pikpak server\nonly support download folder") DownloadCmd.Flags().StringVarP(&parentId, "parent-id", "P", "", "the parent path id") - DownloadCmd.Flags().BoolVarP(&progress, "progress", "g", false, "show download progress") } -// Downloads all files in the specified directory -func downloadFolder(p *pikpak.PikPak) { - base := filepath.Base(folder) - var err error - if parentId == "" { - parentId, err = p.GetPathFolderId(folder) +var DownloadCmd = &cobra.Command{ + Use: "download", + Aliases: []string{"d"}, + Short: `Download file from pikpak server`, + Run: func(cmd *cobra.Command, args []string) { + p := pikpak.NewPikPak(conf.Config.Username, conf.Config.Password) + err := p.Login() if err != nil { - logrus.Errorln("Get Parent Folder Id Failed:", err) - return + logrus.Errorln("Login Failed:", err) } - - } - collectStat := make([]warpStat, 0) - recursive(p, &collectStat, parentId, filepath.Join(output, base)) - - statCh := make(chan warpStat, len(collectStat)) - statDone := make(chan struct{}) - - fileCh := make(chan warpFile, len(collectStat)) - fileDone := make(chan struct{}) - - for i := 0; i < 4; i += 1 { - go func(fileCh chan<- warpFile, statCh <-chan warpStat, statDone chan<- struct{}) { - for { - stat, ok := <-statCh - if !ok { - break - } - file, err := p.GetFile(stat.s.ID) - if err != nil { - logrus.Errorln("Get File Failed:", err) - } - fileCh <- warpFile{ - f: &file, - output: stat.output, - } - statDone <- struct{}{} + var collectStat []warpStat + if len(args) > 0 { + cst, err := collectFileStat(&p, args) + if err != nil { + logrus.Errorln("Collect File Stat Failed:", err) + return } - }(fileCh, statCh, statDone) - } + collectStat = cst + } else { + cst, err := collectFolderStat(&p) + if err != nil { + logrus.Errorln("Collect Folder Stat Failed:", err) + return + } + collectStat = cst + } + + for _, st := range collectStat { + logrus.Infoln("Download:", st.output, st.s.Name) + } + in := make(chan warpFile, count) + wait := new(sync.WaitGroup) - if progress { - pb := mpb.New(mpb.WithAutoRefresh()) for i := 0; i < count; i++ { - // if progress is true then show progress bar - go download(fileCh, fileDone, pb) + wait.Add(1) + go download(in, wait) + } + for _, st := range collectStat { + f, err := p.GetFile(st.s.ID) + if err != nil { + logrus.Errorln("Get File Failed:", err) + continue + } + in <- warpFile{ + f: &f, + output: st.output, + } } - } else { - go download(fileCh, fileDone, nil) - } + close(in) + wait.Wait() + }, +} - for i := 0; i < len(collectStat); i += 1 { - err := utils.CreateDirIfNotExist(collectStat[i].output) +// Downloads all files in the specified directory +func collectFolderStat(p *pikpak.PikPak) ([]warpStat, error) { + base := filepath.Base(folder) + var err error + if parentId == "" { + parentId, err = p.GetPathFolderId(folder) if err != nil { - logrus.Errorln("Create output directory failed:", err) - return + return nil, err } - statCh <- collectStat[i] - } - close(statCh) - - for i := 0; i < len(collectStat); i += 1 { - <-statDone - } - close(statDone) - - for i := 0; i < len(collectStat); i += 1 { - <-fileDone } + collectStat := make([]warpStat, 0) + recursive(p, &collectStat, parentId, filepath.Join(output, base)) + return collectStat, nil } func recursive(p *pikpak.PikPak, collectWarpFile *[]warpStat, parentId string, parentPath string) { @@ -155,161 +127,69 @@ func recursive(p *pikpak.PikPak, collectWarpFile *[]warpStat, parentId string, p if r.Kind == "drive#folder" { recursive(p, collectWarpFile, r.ID, filepath.Join(parentPath, r.Name)) } else { - // file, _ := p.GetFile(r.ID) *collectWarpFile = append(*collectWarpFile, warpStat{ s: r, output: parentPath, }) - // fmt.Println(r.Name, r.Size, r.Kind, parentPath) } } } -func downloadFile(p *pikpak.PikPak, args []string) { +func collectFileStat(p *pikpak.PikPak, args []string) ([]warpStat, error) { var err error if parentId == "" { parentId, err = p.GetPathFolderId(folder) if err != nil { - logrus.Errorln("get folder failed:", err) - return + return nil, err } } - // if output not exists then create. - if err := utils.CreateDirIfNotExist(output); err != nil { - logrus.Errorln("Create output directory failed:", err) - return - } - - sendCh := make(chan warpFile, 1) - receiveCh := make(chan struct{}, len(args)) + collectStat := make([]warpStat, 0, len(args)) - if progress { - pb := mpb.New(mpb.WithAutoRefresh()) - for i := 0; i < count; i++ { - // if progress is true then show progress bar - go download(sendCh, receiveCh, pb) - } - } else { - go download(sendCh, receiveCh, nil) - } - - for i := 0; i < count; i++ { - // if progress is true then show progress bar - switch progress { - case true: - go download(sendCh, receiveCh, mpb.New(mpb.WithAutoRefresh())) - case false: - go download(sendCh, receiveCh, nil) - } - } for _, path := range args { stat, err := p.GetFileStat(parentId, path) if err != nil { logrus.Errorln(path, "get parent id failed:", err) continue } - - file, err := p.GetFile(stat.ID) - if err != nil { - logrus.Errorln(path, "get file failed", err) - continue - } - sendCh <- warpFile{ - f: &file, + collectStat = append(collectStat, warpStat{ + s: stat, output: output, - } - } - close(sendCh) - for i := 0; i < len(args); i++ { - <-receiveCh + }) } - close(receiveCh) + return collectStat, nil } -func download(inCh <-chan warpFile, out chan<- struct{}, pb *mpb.Progress) { - for { - warp, ok := <-inCh - if !ok { - break - } +func download(inCh <-chan warpFile, wait *sync.WaitGroup) { + defer wait.Done() + for warp := range inCh { + // fmt.Println("in", warp, warp.f.Links.ApplicationOctetStream.URL) + utils.CreateDirIfNotExist(warp.output) path := filepath.Join(warp.output, warp.f.Name) - exist, err := utils.Exists(path) - if err != nil { - // logrus.Errorln("Access", path, "Failed:", err) - out <- struct{}{} - continue - } - flag := path + ".pikpakclidownload" - hasFlag, err := utils.Exists(flag) - if err != nil { - // logrus.Errorln("Access", flag, "Failed:", err) - out <- struct{}{} - continue - } - if exist && !hasFlag { - // logrus.Infoln("Skip downloaded file", warp.f.Name) - out <- struct{}{} - continue + exist, _ := utils.Exists(path) + if exist { + st, err := os.Stat(path) + if err != nil { + continue + } + remoteSize, _ := strconv.ParseInt(warp.f.Size, 10, 64) + + // if the file size is the same, skip downloading + if st.Size() == remoteSize { + continue + } } - err = utils.TouchFile(flag) + + dw, err := fastdown.NewDownloadWrapper(warp.f.Links.ApplicationOctetStream.URL, 12, warp.output, warp.f.Name) if err != nil { - // logrus.Errorln("Create flag file", flag, "Failed:", err) - out <- struct{}{} + logrus.Errorln("New download wrapper failed:", err) continue } - - siz, err := strconv.ParseInt(warp.f.Size, 10, 64) + err = dw.Download() if err != nil { - // logrus.Errorln("Parse File size", warp.f.Size, "Failed:", err) - out <- struct{}{} + logrus.Errorln("Download", warp.f.Name, "failed", err) continue } - - var bar *mpb.Bar = nil - - // This is simple way to display part names - // It may be replaced one day. - trimeName := func(name string) string { - // Fixed value, which is an unwise approach. - maxLen := 30 - if utf8.RuneCountInString(name)+3 > maxLen { - return name[:maxLen-3] + "..." - } - return name - } - - if pb != nil { - bar = pb.AddBar(siz, - mpb.PrependDecorators( - decor.Name(trimeName(warp.f.Name)), - decor.Percentage(decor.WCSyncSpace), - ), - mpb.AppendDecorators( - decor.EwmaETA(decor.ET_STYLE_GO, 30), - decor.Name(" ] "), - decor.EwmaSpeed(decor.SizeB1024(0), "% .2f", 60), - ), - ) - } - - // start downloading - err = warp.f.Download(path, bar) - // if hasn't error then remove flag file - if err == nil { - if pb == nil { - logrus.Infoln("Download", warp.f.Name, "Success") - } - os.Remove(flag) - } else { - if pb == nil { - logrus.Errorln("Download", warp.f.Name, "Failed:", err) - } - } - if bar != nil { - bar.Abort(true) - } - out <- struct{}{} } } diff --git a/go.mod b/go.mod index 3071516..9d15562 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,15 @@ module github.com/52funny/pikpakcli -go 1.21.3 +go 1.22.5 require ( + github.com/52funny/fastdown v0.0.0-20240922141535-b9982d970a9d github.com/52funny/pikpakhash v0.0.0-20231104025731-ef91a56eff9c github.com/fatih/color v1.15.0 github.com/json-iterator/go v1.1.12 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 github.com/tidwall/gjson v1.14.4 github.com/vbauerster/mpb/v8 v8.7.2 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index 7b42c75..ca8523a 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/52funny/fastdown v0.0.0-20240922141535-b9982d970a9d h1:lcDI78hsjJjq96Uo7DKcUiGf5c1MY6EkapyPr7RSnqU= +github.com/52funny/fastdown v0.0.0-20240922141535-b9982d970a9d/go.mod h1:X/tIbdoRedE29WSEneKIYR8+SsUObqjn6S/LRFUPeFw= github.com/52funny/pikpakhash v0.0.0-20231104025731-ef91a56eff9c h1:ecJG8tmvgH6exVE4+I3rFPPA1Mk3/lNb8VZ6A7dtcyI= github.com/52funny/pikpakhash v0.0.0-20231104025731-ef91a56eff9c/go.mod h1:YA/IS8XUrMTcrY+J4yOJ3CDgoyQ28NOOo4GnzOL6bTI= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= @@ -41,8 +43,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= From 0059f3c1cff73b28d5d3e20022696be495f0ea1b Mon Sep 17 00:00:00 2001 From: 52funny Date: Sun, 29 Sep 2024 09:53:30 +0800 Subject: [PATCH 2/4] fix: updated fastdown lib --- cmd/download/download.go | 8 ++++++-- go.mod | 3 ++- go.sum | 6 ++++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/download/download.go b/cmd/download/download.go index 85dfb2a..89572b9 100644 --- a/cmd/download/download.go +++ b/cmd/download/download.go @@ -19,6 +19,9 @@ import ( // default 1 var count int +// Number of go routines are used to download each file +var maxRoutine int = 1 + // Specifies the folder of the pikpak server // // default server root directory (.) @@ -181,11 +184,12 @@ func download(inCh <-chan warpFile, wait *sync.WaitGroup) { } } - dw, err := fastdown.NewDownloadWrapper(warp.f.Links.ApplicationOctetStream.URL, 12, warp.output, warp.f.Name) + sz, err := strconv.ParseInt(warp.f.Size, 10, 64) if err != nil { - logrus.Errorln("New download wrapper failed:", err) + logrus.Errorln("ParseInt", warp.f.Size, "failed", err) continue } + dw := fastdown.NewDownloadWrapper(warp.f.Links.ApplicationOctetStream.URL, maxRoutine, sz, warp.output, warp.f.Name) err = dw.Download() if err != nil { logrus.Errorln("Download", warp.f.Name, "failed", err) diff --git a/go.mod b/go.mod index 9d15562..46c2c12 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/52funny/pikpakcli go 1.22.5 require ( - github.com/52funny/fastdown v0.0.0-20240922141535-b9982d970a9d + github.com/52funny/fastdown v0.0.0-20240929014321-44d83625fa59 github.com/52funny/pikpakhash v0.0.0-20231104025731-ef91a56eff9c github.com/fatih/color v1.15.0 github.com/json-iterator/go v1.1.12 @@ -30,6 +30,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect + golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.16.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index ca8523a..1e16373 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/52funny/fastdown v0.0.0-20240922141535-b9982d970a9d h1:lcDI78hsjJjq96Uo7DKcUiGf5c1MY6EkapyPr7RSnqU= -github.com/52funny/fastdown v0.0.0-20240922141535-b9982d970a9d/go.mod h1:X/tIbdoRedE29WSEneKIYR8+SsUObqjn6S/LRFUPeFw= +github.com/52funny/fastdown v0.0.0-20240929014321-44d83625fa59 h1:opDgCJiBAdSXgXYiWiqVJMRyRob3saoxSjNXadil1M4= +github.com/52funny/fastdown v0.0.0-20240929014321-44d83625fa59/go.mod h1:97lnvI+R3CkbZxR6uHxGeHkCSMeC9uAKnxT+u1zn4d0= github.com/52funny/pikpakhash v0.0.0-20231104025731-ef91a56eff9c h1:ecJG8tmvgH6exVE4+I3rFPPA1Mk3/lNb8VZ6A7dtcyI= github.com/52funny/pikpakhash v0.0.0-20231104025731-ef91a56eff9c/go.mod h1:YA/IS8XUrMTcrY+J4yOJ3CDgoyQ28NOOo4GnzOL6bTI= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= @@ -53,6 +53,8 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/vbauerster/mpb/v8 v8.7.2 h1:SMJtxhNho1MV3OuFgS1DAzhANN1Ejc5Ct+0iSaIkB14= github.com/vbauerster/mpb/v8 v8.7.2/go.mod h1:ZFnrjzspgDHoxYLGvxIruiNk73GNTPG4YHgVNpR10VY= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= From 705306a4591c4202f4b1ac1e0b7911d41e80e39d Mon Sep 17 00:00:00 2001 From: 52funny <52funny@52funnydeMac-Pro.local> Date: Sun, 29 Sep 2024 14:27:13 +0800 Subject: [PATCH 3/4] feat: using pikpakapi lib --- cmd/download/download.go | 51 +++++++++++++++++++++++----------------- go.mod | 7 +++--- go.sum | 12 ++++++---- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/cmd/download/download.go b/cmd/download/download.go index 89572b9..1940684 100644 --- a/cmd/download/download.go +++ b/cmd/download/download.go @@ -7,8 +7,8 @@ import ( "sync" "github.com/52funny/fastdown" + "github.com/52funny/pikpakapi" "github.com/52funny/pikpakcli/conf" - "github.com/52funny/pikpakcli/internal/pikpak" "github.com/52funny/pikpakcli/internal/utils" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -36,12 +36,12 @@ var parentId string var output string type warpFile struct { - f *pikpak.File + f *pikpakapi.File output string } type warpStat struct { - s pikpak.FileStat + s pikpakapi.FileStat output string } @@ -57,11 +57,20 @@ var DownloadCmd = &cobra.Command{ Aliases: []string{"d"}, Short: `Download file from pikpak server`, Run: func(cmd *cobra.Command, args []string) { - p := pikpak.NewPikPak(conf.Config.Username, conf.Config.Password) + p := pikpakapi.NewPikPak(conf.Config.Username, conf.Config.Password) + + // Setup proxy + if conf.Config.Proxy != "" { + p.SetProxy(conf.Config.Proxy) + } + + // Login err := p.Login() if err != nil { logrus.Errorln("Login Failed:", err) } + logrus.Debugln("Login Success") + var collectStat []warpStat if len(args) > 0 { cst, err := collectFileStat(&p, args) @@ -80,9 +89,9 @@ var DownloadCmd = &cobra.Command{ } for _, st := range collectStat { - logrus.Infoln("Download:", st.output, st.s.Name) + logrus.Infoln("Download:", st.s.Name) } - in := make(chan warpFile, count) + in := make(chan warpFile, 1) wait := new(sync.WaitGroup) for i := 0; i < count; i++ { @@ -95,6 +104,7 @@ var DownloadCmd = &cobra.Command{ logrus.Errorln("Get File Failed:", err) continue } + logrus.Debug("Send to channel:", f.Name) in <- warpFile{ f: &f, output: st.output, @@ -106,11 +116,11 @@ var DownloadCmd = &cobra.Command{ } // Downloads all files in the specified directory -func collectFolderStat(p *pikpak.PikPak) ([]warpStat, error) { +func collectFolderStat(p *pikpakapi.PikPak) ([]warpStat, error) { base := filepath.Base(folder) var err error if parentId == "" { - parentId, err = p.GetPathFolderId(folder) + parentId, err = p.GetDirID(pikpakapi.NewPath(folder)) if err != nil { return nil, err } @@ -120,8 +130,8 @@ func collectFolderStat(p *pikpak.PikPak) ([]warpStat, error) { return collectStat, nil } -func recursive(p *pikpak.PikPak, collectWarpFile *[]warpStat, parentId string, parentPath string) { - statList, err := p.GetFolderFileStatList(parentId) +func recursive(p *pikpakapi.PikPak, collectWarpFile *[]warpStat, parentId string, parentPath string) { + statList, err := p.GetDirFilesStat(parentId) if err != nil { logrus.Errorln("Get Folder File Stat List Failed:", err) return @@ -138,10 +148,10 @@ func recursive(p *pikpak.PikPak, collectWarpFile *[]warpStat, parentId string, p } } -func collectFileStat(p *pikpak.PikPak, args []string) ([]warpStat, error) { +func collectFileStat(p *pikpakapi.PikPak, args []string) ([]warpStat, error) { var err error if parentId == "" { - parentId, err = p.GetPathFolderId(folder) + parentId, err = p.GetDirID(pikpakapi.NewPath(folder)) if err != nil { return nil, err } @@ -150,7 +160,7 @@ func collectFileStat(p *pikpak.PikPak, args []string) ([]warpStat, error) { collectStat := make([]warpStat, 0, len(args)) for _, path := range args { - stat, err := p.GetFileStat(parentId, path) + stat, err := p.GetFileStatByPath(pikpakapi.NewPath(path)) if err != nil { logrus.Errorln(path, "get parent id failed:", err) continue @@ -171,24 +181,23 @@ func download(inCh <-chan warpFile, wait *sync.WaitGroup) { path := filepath.Join(warp.output, warp.f.Name) exist, _ := utils.Exists(path) + sz, err := strconv.ParseInt(warp.f.Size, 10, 64) + if err != nil { + logrus.Errorln("ParseInt", warp.f.Size, "failed", err) + continue + } + if exist { st, err := os.Stat(path) if err != nil { continue } - remoteSize, _ := strconv.ParseInt(warp.f.Size, 10, 64) // if the file size is the same, skip downloading - if st.Size() == remoteSize { + if st.Size() == sz { continue } } - - sz, err := strconv.ParseInt(warp.f.Size, 10, 64) - if err != nil { - logrus.Errorln("ParseInt", warp.f.Size, "failed", err) - continue - } dw := fastdown.NewDownloadWrapper(warp.f.Links.ApplicationOctetStream.URL, maxRoutine, sz, warp.output, warp.f.Name) err = dw.Download() if err != nil { diff --git a/go.mod b/go.mod index 46c2c12..b80a087 100644 --- a/go.mod +++ b/go.mod @@ -1,16 +1,17 @@ module github.com/52funny/pikpakcli -go 1.22.5 +go 1.23.0 require ( github.com/52funny/fastdown v0.0.0-20240929014321-44d83625fa59 - github.com/52funny/pikpakhash v0.0.0-20231104025731-ef91a56eff9c + github.com/52funny/pikpakapi v0.0.0-20240923033422-d105d48371a0 + github.com/52funny/pikpakhash v0.0.0-20231105124054-e75c5c6694c2 github.com/fatih/color v1.15.0 github.com/json-iterator/go v1.1.12 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 github.com/stretchr/testify v1.9.0 - github.com/tidwall/gjson v1.14.4 + github.com/tidwall/gjson v1.17.3 github.com/vbauerster/mpb/v8 v8.7.2 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index 1e16373..28a2815 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,9 @@ github.com/52funny/fastdown v0.0.0-20240929014321-44d83625fa59 h1:opDgCJiBAdSXgXYiWiqVJMRyRob3saoxSjNXadil1M4= github.com/52funny/fastdown v0.0.0-20240929014321-44d83625fa59/go.mod h1:97lnvI+R3CkbZxR6uHxGeHkCSMeC9uAKnxT+u1zn4d0= -github.com/52funny/pikpakhash v0.0.0-20231104025731-ef91a56eff9c h1:ecJG8tmvgH6exVE4+I3rFPPA1Mk3/lNb8VZ6A7dtcyI= -github.com/52funny/pikpakhash v0.0.0-20231104025731-ef91a56eff9c/go.mod h1:YA/IS8XUrMTcrY+J4yOJ3CDgoyQ28NOOo4GnzOL6bTI= +github.com/52funny/pikpakapi v0.0.0-20240923033422-d105d48371a0 h1:NXniO0MPxX2F7Y8snamBAy2pXQmMjQiMBUYH75zfiS8= +github.com/52funny/pikpakapi v0.0.0-20240923033422-d105d48371a0/go.mod h1:O9wOf/mN/PMAgR/p634DDIM9al8qSmN+DDUk0TvZ3nU= +github.com/52funny/pikpakhash v0.0.0-20231105124054-e75c5c6694c2 h1:iKBUZ8YkxBfRooJNKyS1xT/BC4EBqvIWxMOTxDv00pk= +github.com/52funny/pikpakhash v0.0.0-20231105124054-e75c5c6694c2/go.mod h1:YA/IS8XUrMTcrY+J4yOJ3CDgoyQ28NOOo4GnzOL6bTI= github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8= @@ -15,6 +17,8 @@ github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBD github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -45,8 +49,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM= -github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94= +github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= From 79ce95039728939e96d4230fd2eb27da23d69e9b Mon Sep 17 00:00:00 2001 From: hxz393 Date: Thu, 6 Mar 2025 21:24:37 +0800 Subject: [PATCH 4/4] feat: add -n option for task name specification --- cmd/new/url/url.go | 7 +++++-- internal/pikpak/url.go | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cmd/new/url/url.go b/cmd/new/url/url.go index be25103..01174e9 100644 --- a/cmd/new/url/url.go +++ b/cmd/new/url/url.go @@ -59,6 +59,8 @@ var path string var parentId string +var name string + var input string var cli bool @@ -66,6 +68,7 @@ var cli bool func init() { NewUrlCommand.Flags().StringVarP(&path, "path", "p", "/", "The path of the folder") NewUrlCommand.Flags().StringVarP(&parentId, "parent-id", "P", "", "The parent id") + NewUrlCommand.Flags().StringVarP(&name, "name", "n", "", "The name of the task") NewUrlCommand.Flags().StringVarP(&input, "input", "i", "", "The input of the sha file") NewUrlCommand.Flags().BoolVarP(&cli, "cli", "c", false, "The cli mode") } @@ -81,7 +84,7 @@ func handleNewUrl(p *pikpak.PikPak, shas []string) { } } for _, url := range shas { - err := p.CreateUrlFile(parentId, url) + err := p.CreateUrlFile(parentId, url, name) if err != nil { logrus.Errorln("Create url file failed: ", err) continue @@ -108,7 +111,7 @@ func handleCli(p *pikpak.PikPak) { break } url := string(lineBytes) - err = p.CreateUrlFile(parentId, url) + err = p.CreateUrlFile(parentId, url, name) if err != nil { logrus.Errorln("Create url file failed: ", err) continue diff --git a/internal/pikpak/url.go b/internal/pikpak/url.go index e7dbeed..28085c7 100644 --- a/internal/pikpak/url.go +++ b/internal/pikpak/url.go @@ -9,7 +9,7 @@ import ( "github.com/sirupsen/logrus" ) -func (p *PikPak) CreateUrlFile(parentId, url string) error { +func (p *PikPak) CreateUrlFile(parentId, url string, name string) error { m := map[string]interface{}{ "kind": "drive#file", "upload_type": "UPLOAD_TYPE_URL", @@ -20,6 +20,9 @@ func (p *PikPak) CreateUrlFile(parentId, url string) error { if parentId != "" { m["parent_id"] = parentId } + if name != "" { + m["name"] = name + } bs, err := jsoniter.Marshal(&m) if err != nil { return err