Skip to content

Commit

Permalink
修改包结构, 增加获取 ip 地址功能: BaiduPCS-Go tool getip
Browse files Browse the repository at this point in the history
  • Loading branch information
iikira committed Apr 27, 2018
1 parent 7a25dd7 commit 987b517
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 74 deletions.
4 changes: 2 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This project was largely inspired by [GangZhuo/BaiduPCS](https://github.com/Gang
* [6. 设置下载最大并发量](#6-设置下载最大并发量)
* [7. 退出程序](#7-退出程序)
- [常见问题](#常见问题)
- [TODO](#TODO)
- [TODO](#todo)
- [交流反馈](#交流反馈)
- [捐助](#捐助)

Expand Down
21 changes: 11 additions & 10 deletions baidupcs/cloud_dl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package baidupcs
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/pcsutil/pcstime"
"github.com/json-iterator/go"
"io"
"strconv"
Expand Down Expand Up @@ -58,16 +59,16 @@ type cloudDlTaskInfo struct {

func (ci *cloudDlTaskInfo) convert() *CloudDlTaskInfo {
ci2 := &CloudDlTaskInfo{
Status: pcsutil.MustInt(ci.Status),
FileSize: pcsutil.MustInt64(ci.FileSize),
FinishedSize: pcsutil.MustInt64(ci.FinishedSize),
CreateTime: pcsutil.MustInt64(ci.CreateTime),
StartTime: pcsutil.MustInt64(ci.StartTime),
FinishTime: pcsutil.MustInt64(ci.FinishTime),
Status: converter.MustInt(ci.Status),
FileSize: converter.MustInt64(ci.FileSize),
FinishedSize: converter.MustInt64(ci.FinishedSize),
CreateTime: converter.MustInt64(ci.CreateTime),
StartTime: converter.MustInt64(ci.StartTime),
FinishTime: converter.MustInt64(ci.FinishTime),
SavePath: ci.SavePath,
SourceURL: ci.SourceURL,
TaskName: ci.TaskName,
OdType: pcsutil.MustInt(ci.OdType),
OdType: converter.MustInt(ci.OdType),
Result: ci.Result,
}

Expand All @@ -79,7 +80,7 @@ func (ci *cloudDlTaskInfo) convert() *CloudDlTaskInfo {

ci2.FileList = append(ci2.FileList, &CloudDlFileInfo{
FileName: v.FileName,
FileSize: pcsutil.MustInt64(v.FileSize),
FileSize: converter.MustInt64(v.FileSize),
})
}

Expand Down Expand Up @@ -294,7 +295,7 @@ func (cl CloudDlTaskList) String() string {
tb := pcstable.NewTable(builder)
tb.SetHeader([]string{"#", "任务ID", "任务名称", "文件大小", "创建日期", "保存路径", "资源地址", "状态"})
for k, v := range cl {
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(v.TaskID, 10), v.TaskName, pcsutil.ConvertFileSize(v.FileSize), pcsutil.FormatTime(v.CreateTime), v.SavePath, v.SourceURL, v.StatusText})
tb.Append([]string{strconv.Itoa(k), strconv.FormatInt(v.TaskID, 10), v.TaskName, converter.ConvertFileSize(v.FileSize), pcstime.FormatTime(v.CreateTime), v.SavePath, v.SourceURL, v.StatusText})
}
tb.Render()
return builder.String()
Expand Down
13 changes: 7 additions & 6 deletions baidupcs/file_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package baidupcs
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/pcsutil/pcstime"
"github.com/iikira/BaiduPCS-Go/pcsverbose"
"github.com/json-iterator/go"
"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -56,8 +57,8 @@ func (fj *fdJSON) convert() *FileDirectory {
Mtime: fj.Mtime,
MD5: fj.MD5,
Size: fj.Size,
Isdir: pcsutil.IntToBool(fj.IsdirInt),
Ifhassubdir: pcsutil.IntToBool(fj.IfhassubdirInt),
Isdir: converter.IntToBool(fj.IsdirInt),
Ifhassubdir: converter.IntToBool(fj.IfhassubdirInt),
}
}

Expand Down Expand Up @@ -201,15 +202,15 @@ func (f *FileDirectory) String() string {
[]string{"类型", "文件"},
[]string{"文件路径", f.Path},
[]string{"文件名称", f.Filename},
[]string{"文件大小", strconv.FormatInt(f.Size, 10) + ", " + pcsutil.ConvertFileSize(f.Size)},
[]string{"文件大小", strconv.FormatInt(f.Size, 10) + ", " + converter.ConvertFileSize(f.Size)},
[]string{"md5 (截图请打码)", f.MD5},
})
}

tb.Append([]string{"fs_id", strconv.FormatInt(f.FsID, 10)})
tb.AppendBulk([][]string{
[]string{"创建日期", pcsutil.FormatTime(f.Ctime)},
[]string{"修改日期", pcsutil.FormatTime(f.Mtime)},
[]string{"创建日期", pcstime.FormatTime(f.Ctime)},
[]string{"修改日期", pcstime.FormatTime(f.Mtime)},
})

if f.Ifhassubdir {
Expand Down
4 changes: 2 additions & 2 deletions downloader/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package downloader

import (
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/requester"
)

Expand All @@ -10,7 +10,7 @@ var (
DownloadingFileSuffix = ".downloader_downloading"

// MinParallelSize 单个线程最小的数据量
MinParallelSize = 128 * pcsutil.KB
MinParallelSize = 128 * converter.KB
)

// Config 下载配置
Expand Down
10 changes: 5 additions & 5 deletions downloader/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package downloader

import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
)

// DoDownload 执行下载
Expand All @@ -27,15 +27,15 @@ func DoDownload(url string, cfg Config) {
}

if v.TotalSize <= 0 {
ts = pcsutil.ConvertFileSize(v.Downloaded, 2)
ts = converter.ConvertFileSize(v.Downloaded, 2)
} else {
ts = pcsutil.ConvertFileSize(v.TotalSize, 2)
ts = converter.ConvertFileSize(v.TotalSize, 2)
}

fmt.Printf("\r↓ %s/%s %s/s in %s ............",
pcsutil.ConvertFileSize(v.Downloaded, 2),
converter.ConvertFileSize(v.Downloaded, 2),
ts,
pcsutil.ConvertFileSize(v.Speeds, 2),
converter.ConvertFileSize(v.Speeds, 2),
v.TimeElapsed,
)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/pcscommand/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"github.com/iikira/BaiduPCS-Go/baidupcs"
"github.com/iikira/BaiduPCS-Go/internal/pcsconfig"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/BaiduPCS-Go/requester/downloader"
"github.com/iikira/BaiduPCS-Go/requester/rio"
Expand Down Expand Up @@ -120,9 +120,9 @@ func getDownloadFunc(id int, savePath string, cfg *downloader.Config, isPrintSta
}

fmt.Printf("\r[%d] ↓ %s/%s %s/s in %s, left %s ............", id,
pcsutil.ConvertFileSize(v.Downloaded(), 2),
pcsutil.ConvertFileSize(v.TotalSize(), 2),
pcsutil.ConvertFileSize(v.SpeedsPerSecond(), 2),
converter.ConvertFileSize(v.Downloaded(), 2),
converter.ConvertFileSize(v.TotalSize(), 2),
converter.ConvertFileSize(v.SpeedsPerSecond(), 2),
v.TimeElapsed()/1e7*1e7, leftStr,
)
}
Expand Down Expand Up @@ -322,7 +322,7 @@ func RunDownload(paths []string, option DownloadOption) {
totalSize += task.downloadInfo.Size
}

fmt.Printf("任务结束, 数据总量: %s\n", pcsutil.ConvertFileSize(totalSize))
fmt.Printf("任务结束, 数据总量: %s\n", converter.ConvertFileSize(totalSize))
}

// fileExist 检查文件是否存在,
Expand Down
9 changes: 5 additions & 4 deletions internal/pcscommand/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package pcscommand
import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/pcsutil/pcstime"
"github.com/olekukonko/tablewriter"
"os"
"strconv"
Expand Down Expand Up @@ -32,15 +33,15 @@ func RunLs(path string) {

for k, file := range files {
if file.Isdir {
tb.Append([]string{strconv.Itoa(k), "-", pcsutil.FormatTime(file.Ctime), file.Filename + "/"})
tb.Append([]string{strconv.Itoa(k), "-", pcstime.FormatTime(file.Ctime), file.Filename + "/"})
continue
}

tb.Append([]string{strconv.Itoa(k), pcsutil.ConvertFileSize(file.Size), pcsutil.FormatTime(file.Ctime), file.Filename})
tb.Append([]string{strconv.Itoa(k), converter.ConvertFileSize(file.Size), pcstime.FormatTime(file.Ctime), file.Filename})
}

fN, dN := files.Count()
tb.Append([]string{"", "总: " + pcsutil.ConvertFileSize(files.TotalSize()), "", fmt.Sprintf("文件总数: %d, 目录总数: %d", fN, dN)})
tb.Append([]string{"", "总: " + converter.ConvertFileSize(files.TotalSize()), "", fmt.Sprintf("文件总数: %d, 目录总数: %d", fN, dN)})

tb.Render()

Expand Down
6 changes: 3 additions & 3 deletions internal/pcscommand/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pcscommand

import (
"fmt"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
)

// RunGetQuota 执行 获取当前用户空间配额信息, 并输出
Expand All @@ -14,8 +14,8 @@ func RunGetQuota() {
}
fmt.Printf("用户名: %s, 总空间: %s, 已用空间: %s, 比率: %f%%\n",
GetActiveUser().Name,
pcsutil.ConvertFileSize(quota),
pcsutil.ConvertFileSize(used),
converter.ConvertFileSize(quota),
converter.ConvertFileSize(used),
100*float64(used)/float64(quota),
)
}
13 changes: 7 additions & 6 deletions internal/pcscommand/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/iikira/BaiduPCS-Go/baidupcs"
"github.com/iikira/BaiduPCS-Go/pcscache"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/BaiduPCS-Go/requester/multipartreader"
"github.com/iikira/BaiduPCS-Go/requester/rio"
Expand All @@ -25,7 +26,7 @@ import (
"time"
)

const requiredSliceLen = 256 * pcsutil.KB // 256 KB
const requiredSliceLen = 256 * converter.KB // 256 KB

type utask struct {
ListTask
Expand Down Expand Up @@ -362,7 +363,7 @@ func RunUpload(localPaths []string, savePath string) {
}
}

if task.uploadInfo.Length >= 128*pcsutil.MB {
if task.uploadInfo.Length >= 128*converter.MB {
fmt.Printf("[%d] 检测秒传中, 请稍候...\n", task.ID)
}

Expand Down Expand Up @@ -438,9 +439,9 @@ func RunUpload(localPaths []string, savePath string) {
}

fmt.Printf("\r[%d] ↑ %s/%s %s/s in %s ............", task.ID,
pcsutil.ConvertFileSize(v.Uploaded(), 2),
pcsutil.ConvertFileSize(v.TotalSize(), 2),
pcsutil.ConvertFileSize(v.SpeedsPerSecond(), 2),
converter.ConvertFileSize(v.Uploaded(), 2),
converter.ConvertFileSize(v.TotalSize(), 2),
converter.ConvertFileSize(v.SpeedsPerSecond(), 2),
v.TimeElapsed(),
)
}
Expand All @@ -465,7 +466,7 @@ func RunUpload(localPaths []string, savePath string) {
}

fmt.Printf("\n")
fmt.Printf("全部上传完毕, 总大小: %s\n", pcsutil.ConvertFileSize(totalSize))
fmt.Printf("全部上传完毕, 总大小: %s\n", converter.ConvertFileSize(totalSize))
}

// GetFileSum 获取文件的大小, md5, 前256KB切片的 md5, crc32
Expand Down
22 changes: 17 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/iikira/BaiduPCS-Go/pcsliner"
"github.com/iikira/BaiduPCS-Go/pcstable"
"github.com/iikira/BaiduPCS-Go/pcsutil"
"github.com/iikira/BaiduPCS-Go/pcsutil/converter"
"github.com/iikira/BaiduPCS-Go/pcsutil/getip"
"github.com/iikira/BaiduPCS-Go/pcsutil/pcstime"
"github.com/iikira/BaiduPCS-Go/pcsverbose"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/args"
Expand Down Expand Up @@ -150,7 +153,7 @@ func main() {
if activeUser.Name != "" {
// 格式: BaiduPCS-Go:<工作目录> <百度ID>$
// 工作目录太长的话会自动缩略
prompt = app.Name + ":" + pcsutil.ShortDisplay(path.Base(activeUser.Workdir), 20) + " " + activeUser.Name + "$ "
prompt = app.Name + ":" + converter.ShortDisplay(path.Base(activeUser.Workdir), 20) + " " + activeUser.Name + "$ "
} else {
// BaiduPCS-Go >
prompt = app.Name + " > "
Expand Down Expand Up @@ -960,7 +963,7 @@ func main() {
return nil
}

taskIDs := pcsutil.SliceStringToInt64(c.Args())
taskIDs := converter.SliceStringToInt64(c.Args())

if len(taskIDs) == 0 {
fmt.Printf("未找到合法的任务ID, task_id\n")
Expand Down Expand Up @@ -992,7 +995,7 @@ func main() {
return nil
}

taskIDs := pcsutil.SliceStringToInt64(c.Args())
taskIDs := converter.SliceStringToInt64(c.Args())

if len(taskIDs) == 0 {
fmt.Printf("未找到合法的任务ID, task_id\n")
Expand All @@ -1014,7 +1017,7 @@ func main() {
return nil
}

taskIDs := pcsutil.SliceStringToInt64(c.Args())
taskIDs := converter.SliceStringToInt64(c.Args())

if len(taskIDs) == 0 {
fmt.Printf("未找到合法的任务ID, task_id\n")
Expand Down Expand Up @@ -1127,7 +1130,16 @@ func main() {
Name: "showtime",
Usage: "显示当前时间(北京时间)",
Action: func(c *cli.Context) error {
fmt.Printf(pcsutil.BeijingTimeOption("printLog"))
fmt.Printf(pcstime.BeijingTimeOption("printLog"))
return nil
},
},
{
Name: "getip",
Usage: "获取IP地址和IP位置",
Action: func(c *cli.Context) error {
ipAddr, location := getip.IPInfo()
fmt.Printf("IP地址: %s, IP位置: %s\n", ipAddr, location)
return nil
},
},
Expand Down
2 changes: 1 addition & 1 deletion pcsutil/convert.go → pcsutil/converter/converter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pcsutil
package converter

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// go test -test.bench=".*"
package pcsutil
package converter

import (
"testing"
Expand Down
Loading

0 comments on commit 987b517

Please sign in to comment.