Skip to content

Commit

Permalink
更新错误处理, 增加搜索文件
Browse files Browse the repository at this point in the history
  • Loading branch information
iikira committed Aug 4, 2018
1 parent f97b4d2 commit eb5f66a
Show file tree
Hide file tree
Showing 37 changed files with 1,797 additions and 986 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ This project was largely inspired by [GangZhuo/BaiduPCS](https://github.com/Gang
* [列出目录](#列出目录)
* [列出目录树形图](#列出目录树形图)
* [获取单个文件/目录的元信息](#获取单个文件目录的元信息)
* [搜索文件](#搜索文件)
* [下载文件/目录](#下载文件目录)
* [上传文件/目录](#上传文件目录)
* [获取下载直链](#获取下载直链)
Expand Down Expand Up @@ -308,6 +309,25 @@ BaiduPCS-Go meta 我的资源
BaiduPCS-Go meta /
```

## 搜索文件

按文件名搜索文件(不支持查找目录)。
```
BaiduPCS-Go search [-path=<需要检索的目录>] [-r] <关键字>
```

#### 例子
```
# 搜索根目录的文件
BaiduPCS-Go search -path=/ 关键字
# 搜索当前工作目录的文件
BaiduPCS-Go search 关键字
# 递归搜索当前工作目录的文件
BaiduPCS-Go search -r 关键字
```

## 下载文件/目录
```
BaiduPCS-Go download <网盘文件或目录的路径1> <文件或目录2> <文件或目录3> ...
Expand Down
86 changes: 44 additions & 42 deletions baidupcs/baidupcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package baidupcs

import (
"errors"
"github.com/iikira/BaiduPCS-Go/baidupcs/pcserror"
"github.com/iikira/BaiduPCS-Go/pcsverbose"
"github.com/iikira/BaiduPCS-Go/requester"
"github.com/iikira/baidu-tools/pan"
"github.com/json-iterator/go"
"net/http"
"net/http/cookiejar"
"net/url"
Expand All @@ -22,6 +21,8 @@ const (
OperationFilesDirectoriesMeta = "获取文件/目录的元信息"
// OperationFilesDirectoriesList 获取目录下的文件列表
OperationFilesDirectoriesList = "获取目录下的文件列表"
// OperationSearch 搜索
OperationSearch = "搜索"
// OperationRemove 删除文件/目录
OperationRemove = "删除文件/目录"
// OperationMkdir 创建目录
Expand All @@ -40,6 +41,10 @@ const (
OperationUploadTmpFile = "分片上传—文件分片及上传"
// OperationUploadCreateSuperFile 分片上传—合并分片文件
OperationUploadCreateSuperFile = "分片上传—合并分片文件"
// OperationUploadPrecreate 分片上传—Precreate
OperationUploadPrecreate = "分片上传—Precreate"
// OperationUploadSuperfile2 分片上传—Superfile2
OperationUploadSuperfile2 = "分片上传—Superfile2"
// OperationDownloadFile 下载单个文件
OperationDownloadFile = "下载单个文件"
// OperationDownloadStreamFile 下载流式文件
Expand All @@ -62,6 +67,11 @@ const (
OperationShareCancel = "取消分享"
// OperationShareList 列出分享列表
OperationShareList = "列出分享列表"

// PCSBaiduCom pcs api地址
PCSBaiduCom = "pcs.baidu.com"
// PanBaiduCom 网盘首页api地址
PanBaiduCom = "pan.baidu.com"
// NetdiskUA 网盘客户端ua
NetdiskUA = "netdisk;7.8.1;Red;android-android;4.3"
)
Expand All @@ -70,20 +80,29 @@ var (
baiduPCSVerbose = pcsverbose.New("BAIDUPCS")
)

// BaiduPCS 百度 PCS API 详情
type BaiduPCS struct {
appID int // app_id
isHTTPS bool // 是否启用https
client *requester.HTTPClient // http 客户端
}
type (
// BaiduPCS 百度 PCS API 详情
BaiduPCS struct {
appID int // app_id
isHTTPS bool // 是否启用https
client *requester.HTTPClient // http 客户端
}

userInfoJSON struct {
*pcserror.PanErrorInfo
Records []struct {
Uk int64 `json:"uk"`
} `json:"records"`
}
)

// NewPCS 提供app_id, 百度BDUSS, 返回 BaiduPCS 对象
func NewPCS(appID int, bduss string) *BaiduPCS {
client := requester.NewHTTPClient()

pcsURL := &url.URL{
Scheme: "http",
Host: "pcs.baidu.com",
Host: PCSBaiduCom,
}

cookies := []*http.Cookie{
Expand All @@ -97,7 +116,7 @@ func NewPCS(appID int, bduss string) *BaiduPCS {
jar.SetCookies(pcsURL, cookies)
jar.SetCookies((&url.URL{
Scheme: "http",
Host: "pan.baidu.com",
Host: PanBaiduCom,
}), cookies)
client.SetCookiejar(jar)

Expand Down Expand Up @@ -156,7 +175,7 @@ func (pcs *BaiduPCS) SetHTTPS(https bool) {
func (pcs *BaiduPCS) URL() *url.URL {
return &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Host: "pcs.baidu.com",
Host: PCSBaiduCom,
}
}

Expand All @@ -180,7 +199,7 @@ func (pcs *BaiduPCS) generatePCSURL(subPath, method string, param ...map[string]
func (pcs *BaiduPCS) generatePCSURL2(subPath, method string, param ...map[string]string) *url.URL {
pcsURL2 := &url.URL{
Scheme: GetHTTPScheme(pcs.isHTTPS),
Host: "pan.baidu.com",
Host: PanBaiduCom,
Path: "/rest/2.0/" + subPath,
}

Expand All @@ -198,44 +217,27 @@ func (pcs *BaiduPCS) generatePCSURL2(subPath, method string, param ...map[string
}

// UK 获取用户 UK
func (pcs *BaiduPCS) UK() (uk int64, pcsError Error) {
pcs.lazyInit()

pcsURL := GetHTTPScheme(pcs.isHTTPS) + "://pan.baidu.com/api/user/getinfo?need_selfinfo=1"

errInfo := NewErrorInfo(OperationGetUK)
body, err := pcs.client.Fetch("GET", pcsURL, nil, map[string]string{
"User-Agent": NetdiskUA,
})
if err != nil {
errInfo.errType = ErrTypeNetError
errInfo.err = err
return 0, errInfo
func (pcs *BaiduPCS) UK() (uk int64, pcsError pcserror.Error) {
dataReadCloser, pcsError := pcs.PrepareUK()
if pcsError != nil {
return
}

jsonData := struct {
pan.RemoteErrInfo
Records []struct {
Uk int64 `json:"uk"`
} `json:"records"`
}{}
defer dataReadCloser.Close()

err = jsoniter.Unmarshal(body, &jsonData)
if err != nil {
errInfo.jsonError(err)
return 0, errInfo
errInfo := pcserror.NewPanErrorInfo(OperationGetUK)
jsonData := userInfoJSON{
PanErrorInfo: errInfo,
}

if jsonData.ErrNo != 0 {
jsonData.RemoteErrInfo.ParseErrMsg()
errInfo.ErrCode = jsonData.RemoteErrInfo.ErrNo
errInfo.ErrMsg = jsonData.RemoteErrInfo.ErrMsg
return 0, errInfo
pcsError = handleJSONParse(OperationGetUK, dataReadCloser, &jsonData)
if pcsError != nil {
return
}

if len(jsonData.Records) != 1 {
errInfo.errType = ErrTypeOthers
errInfo.err = errors.New("Unknown remote data")
errInfo.ErrType = pcserror.ErrTypeOthers
errInfo.Err = errors.New("Unknown remote data")
return 0, errInfo
}

Expand Down
Loading

0 comments on commit eb5f66a

Please sign in to comment.