Skip to content

Commit

Permalink
feat: 支持多选下载
Browse files Browse the repository at this point in the history
  • Loading branch information
fzdwx committed Aug 8, 2022
1 parent e8b90b8 commit 2a79288
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Flag:
1. `-k` 使用酷我(默认)
2. `-w` 使用网易云

![screen](https://user-images.githubusercontent.com/65269574/181920199-14fe4227-e522-4d9a-a1bb-afb3adcecfe8.gif)
![demo](https://user-images.githubusercontent.com/65269574/183438669-f101d4bc-1fd6-46ed-9047-f91c93b218cc.gif)

## Git相关命令

Expand All @@ -116,7 +116,8 @@ Flag:

示例: `get clone fzdwx/sky`

![screen](https://user-images.githubusercontent.com/65269574/181920482-c3c9ed9f-86bb-4e1a-a388-14d16a05e138.gif)
![demo](https://user-images.githubusercontent.com/65269574/183439217-e0f7a6a1-55ef-47cd-a155-a8f642ae84fa.gif)


---
**仅供个人学习使用**
**仅供个人学习使用**
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var rootCmd = &cobra.Command{
Example: `get mc 稻香
get clone fzdwx/get
`,
Version: "v0.0.8", // <---VERSION---> Updating this version, will also create a new GitHub release.
Version: "v0.0.9", // <---VERSION---> Updating this version, will also create a new GitHub release.
// Uncomment the following lines if your bare application has an action associated with it:
// RunE: func(cmd *cobra.Command, args []string) error {
// // Your code here
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/duke-git/lancet/v2 v2.1.4
github.com/fzdwx/infinite v0.3.6
github.com/fzdwx/infinite v0.4.2
github.com/pterm/pcli v0.4.6
github.com/pterm/pterm v0.12.45
github.com/rotisserie/eris v0.5.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/duke-git/lancet/v2 v2.1.4 h1:DmEz7O1U8nxFAf65CyIwKqFky98U0GbKdrNy5yN6
github.com/duke-git/lancet/v2 v2.1.4/go.mod h1:5Nawyf/bK783rCiHyVkZLx+jj8028oVVjLOrC21ZONA=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fzdwx/infinite v0.3.6 h1:VlMNHQCk9Ussg7LeRSoAw0uH3rHWBaGIJtKOt2HQy3g=
github.com/fzdwx/infinite v0.3.6/go.mod h1:nTjfw+EMM72D0m01OFxaW+7cvGdyWO89V9hyGwJCLXE=
github.com/fzdwx/infinite v0.4.2 h1:Dn/52AUZxYHAxgscl2/2sTDNXy+WN1opjP2PqnP+5ms=
github.com/fzdwx/infinite v0.4.2/go.mod h1:VX93yH6cs3yIwLednogbVXgKUewI14hoQR5emCFq8Lk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
Expand Down
53 changes: 31 additions & 22 deletions pkg/msc/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package msc
import (
"fmt"
"github.com/duke-git/lancet/v2/slice"
"github.com/fzdwx/get/pkg/ptermx"
"github.com/fzdwx/get/pkg/utils"
inf "github.com/fzdwx/infinite"
"github.com/fzdwx/infinite/color"
Expand All @@ -12,7 +11,6 @@ import (
"github.com/fzdwx/infinite/components/selection/multiselect"
"github.com/fzdwx/infinite/components/spinner"
"github.com/fzdwx/infinite/style"
"io"
"net/http"
"os"
)
Expand Down Expand Up @@ -74,32 +72,43 @@ func getRequestFunc(p Platform) func(name string) Request {
}

func process(selectedSongs []Songs) error {
err := progress.NewGroupWithCount(len(selectedSongs)).AppendRunner(func(progress *components.Progress) func() {
return func() {
s := selectedSongs[progress.Id-1]
resp, err := http.Get(s.DownloadUrl)
defer resp.Body.Close()
if err != nil {
// todo handle err
return
err := progress.NewGroupWithCount(len(selectedSongs)).AppendRunner(func(pro *components.Progress) func() {
s := selectedSongs[pro.Id-1]
resp, err := http.Get(s.DownloadUrl)
if err != nil {
resp.Body.Close()
return func() {
pro.WithDoneView(func() string {
return fmt.Sprintf("get error: %s", err)
})
}
}

file, err := os.OpenFile(fmt.Sprintf("%s%s", utils.NormalizeFileName(s.Name), s.EncodeType), os.O_CREATE|os.O_WRONLY, 0o777)
defer file.Close()
if err != nil {
// todo handle err
return
}
pro.WithTotal(resp.ContentLength)

progress.
WithTotal(resp.ContentLength).
WithPercentAgeFunc(func(total int64, current int64, percent float64) string {
return fmt.Sprintf(" %d/%d", current, total)
fileName := fmt.Sprintf("%s%s", utils.NormalizeFileName(s.Name), s.EncodeType)
dest, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, 0o777)
if err != nil {
dest.Close()
return func() {
pro.WithDoneView(func() string {
return fmt.Sprintf("open dest error: %s", err)
})
}
}
return func() {
defer resp.Body.Close()
defer dest.Close()

pro.WithDoneView(func() string {
return fmt.Sprintf("%s download success", fileName)
})

_, err = io.Copy(io.MultiWriter(file, ptermx.NewProgressWriter(progress)), resp.Body)
_, err := progress.StartTransfer(resp.Body, dest, pro)
if err != nil {
// todo handle err
pro.WithDoneView(func() string {
return fmt.Sprintf("transfer error: %s", err)
})
}
}
}).Display()
Expand Down

0 comments on commit 2a79288

Please sign in to comment.