forked from GopeedLab/gopeed
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
209 additions
and
97 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type args struct { | ||
url string | ||
connections *int | ||
dir *string | ||
} | ||
|
||
func parse() *args { | ||
dir, err := filepath.Abs(filepath.Dir(os.Args[0])) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var args args | ||
args.connections = flag.Int("C", 16, "Concurrent connections.") | ||
args.dir = flag.String("D", dir, "Save directory.") | ||
flag.Parse() | ||
t := flag.Args() | ||
if len(t) > 0 { | ||
args.url = t[0] | ||
} else { | ||
gPrintln("missing url parameter") | ||
gPrintln("try 'gopeed -h' for more information") | ||
os.Exit(1) | ||
} | ||
return &args | ||
} | ||
|
||
func gPrint(msg string) { | ||
fmt.Print("gopeed: " + msg) | ||
} | ||
|
||
func gPrintln(msg string) { | ||
gPrint(msg + "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/monkeyWie/gopeed-core/download" | ||
"github.com/monkeyWie/gopeed-core/download/base" | ||
"github.com/monkeyWie/gopeed-core/download/http" | ||
"github.com/monkeyWie/gopeed-core/util" | ||
"path/filepath" | ||
"sync" | ||
) | ||
|
||
const progressWidth = 20 | ||
|
||
func main() { | ||
args := parse() | ||
|
||
downloader := download.NewDownloader(http.FetcherBuilder) | ||
res, err := downloader.Resolve(&base.Request{ | ||
URL: args.url, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
err = downloader.Create(res, &base.Options{ | ||
Path: *args.dir, | ||
Connections: *args.connections, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
downloader.Listener(func(taskInfo *download.TaskInfo, eventKey base.EventKey) { | ||
if eventKey == base.EventKeyProgress { | ||
printProgress(taskInfo, "downloading...") | ||
} | ||
if eventKey == base.EventKeyDone { | ||
printProgress(taskInfo, "complete") | ||
wg.Done() | ||
} | ||
}) | ||
wg.Wait() | ||
fmt.Println() | ||
gPrint("saving file " + filepath.Join(*args.dir, res.Files[0].Name)) | ||
} | ||
|
||
func printProgress(taskInfo *download.TaskInfo, title string) { | ||
rate := float64(taskInfo.Progress.Downloaded) / float64(taskInfo.Res.TotalSize) | ||
completeWidth := int(progressWidth * rate) | ||
speed := util.ByteFmt(taskInfo.Progress.Speed) | ||
totalSize := util.ByteFmt(taskInfo.Res.TotalSize) | ||
fmt.Printf("\r%s [", title) | ||
for i := 0; i < progressWidth; i++ { | ||
if i < completeWidth { | ||
fmt.Print("■") | ||
} else { | ||
fmt.Print("□") | ||
} | ||
} | ||
fmt.Printf("] %.1f%% %s/s %s", rate*100, speed, totalSize) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
var unitArr = []string{"B", "KB", "MB", "GB", "TB", "EB"} | ||
|
||
func ByteFmt(size int64) string { | ||
if size == 0 { | ||
return "unknown" | ||
} | ||
fs := float64(size) | ||
p := int(math.Log(fs) / math.Log(1024)) | ||
val := fs / math.Pow(1024, float64(p)) | ||
_, frac := math.Modf(val) | ||
if frac > 0 { | ||
return fmt.Sprintf("%.1f%s", math.Floor(val*10)/10, unitArr[p]) | ||
} else { | ||
return fmt.Sprintf("%d%s", int(val), unitArr[p]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package util | ||
|
||
import "testing" | ||
|
||
func TestByteFmt(t *testing.T) { | ||
type args struct { | ||
size int64 | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "100B", | ||
args: args{size: int64(100)}, | ||
want: "100B", | ||
}, | ||
{ | ||
name: "1KB", | ||
args: args{size: int64(1024)}, | ||
want: "1KB", | ||
}, | ||
{ | ||
name: "1.9KB", | ||
args: args{size: int64(1024*2 - 1)}, | ||
want: "1.9KB", | ||
}, | ||
{ | ||
name: "2KB", | ||
args: args{size: int64(1024 * 2)}, | ||
want: "2KB", | ||
}, | ||
{ | ||
name: "1MB", | ||
args: args{size: int64(1024 * 1024)}, | ||
want: "1MB", | ||
}, | ||
{ | ||
name: "1.9MB", | ||
args: args{size: int64(1024*1024*2 - 1)}, | ||
want: "1.9MB", | ||
}, | ||
{ | ||
name: "2MB", | ||
args: args{size: int64(1024 * 1024 * 2)}, | ||
want: "2MB", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := ByteFmt(tt.args.size); got != tt.want { | ||
t.Errorf("ByteFmt() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |