github.com/fzfile/BaiduPCS-Go@v0.0.0-20200606205115-4408961cf336/internal/pcsconfig/baidu.go (about)

     1  package pcsconfig
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/fzfile/BaiduPCS-Go/baidupcs"
     7  	"github.com/fzfile/BaiduPCS-Go/pcstable"
     8  	"github.com/fzfile/BaiduPCS-Go/pcsutil/converter"
     9  	"github.com/fzfile/baidu-tools/tieba"
    10  	"github.com/olekukonko/tablewriter"
    11  	"path"
    12  	"path/filepath"
    13  	"strconv"
    14  	"strings"
    15  )
    16  
    17  var (
    18  	//ErrNoSuchBaiduUser 未登录任何百度帐号
    19  	ErrNoSuchBaiduUser = errors.New("no such baidu user")
    20  	//ErrBaiduUserNotFound 未找到百度帐号
    21  	ErrBaiduUserNotFound = errors.New("baidu user not found")
    22  )
    23  
    24  //BaiduBase Baidu基
    25  type BaiduBase struct {
    26  	UID  uint64 `json:"uid"`  // 百度ID对应的uid
    27  	Name string `json:"name"` // 真实ID
    28  }
    29  
    30  // Baidu 百度帐号对象
    31  type Baidu struct {
    32  	BaiduBase
    33  	Sex string  `json:"sex"` // 性别
    34  	Age float64 `json:"age"` // 帐号年龄
    35  
    36  	BDUSS  string `json:"bduss"`
    37  	PTOKEN string `json:"ptoken"`
    38  	STOKEN string `json:"stoken"`
    39  
    40  	Workdir string `json:"workdir"` // 工作目录
    41  }
    42  
    43  // BaiduPCS 初始化*baidupcs.BaiduPCS
    44  func (baidu *Baidu) BaiduPCS() *baidupcs.BaiduPCS {
    45  	pcs := baidupcs.NewPCS(Config.AppID, baidu.BDUSS)
    46  	pcs.SetStoken(baidu.STOKEN)
    47  	pcs.SetHTTPS(Config.EnableHTTPS)
    48  	pcs.SetPCSUserAgent(Config.PCSUA)
    49  	pcs.SetPanUserAgent(Config.PanUA)
    50  	pcs.SetUID(baidu.UID)
    51  	return pcs
    52  }
    53  
    54  // GetSavePath 根据提供的网盘文件路径 pcspath, 返回本地储存路径,
    55  // 返回绝对路径, 获取绝对路径出错时才返回相对路径...
    56  func (baidu *Baidu) GetSavePath(pcspath string) string {
    57  	dirStr := filepath.Join(Config.SaveDir, fmt.Sprintf("%d_%s", baidu.UID, converter.TrimPathInvalidChars(baidu.Name)), pcspath)
    58  	dir, err := filepath.Abs(dirStr)
    59  	if err != nil {
    60  		dir = filepath.Clean(dirStr)
    61  	}
    62  	return dir
    63  }
    64  
    65  // PathJoin 合并工作目录和相对路径p, 若p为绝对路径则忽略
    66  func (baidu *Baidu) PathJoin(p string) string {
    67  	if path.IsAbs(p) {
    68  		return p
    69  	}
    70  	return path.Join(baidu.Workdir, p)
    71  }
    72  
    73  // BaiduUserList 百度帐号列表
    74  type BaiduUserList []*Baidu
    75  
    76  // NewUserInfoByBDUSS 检测BDUSS有效性, 同时获取百度详细信息 (无法获取 ptoken 和 stoken)
    77  func NewUserInfoByBDUSS(bduss string) (b *Baidu, err error) {
    78  	t, err := tieba.NewUserInfoByBDUSS(bduss)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	b = &Baidu{
    84  		BaiduBase: BaiduBase{
    85  			UID:  t.Baidu.UID,
    86  			Name: t.Baidu.Name,
    87  		},
    88  		Sex:     t.Baidu.Sex,
    89  		Age:     t.Baidu.Age,
    90  		BDUSS:   bduss,
    91  		Workdir: "/",
    92  	}
    93  	return b, nil
    94  }
    95  
    96  // String 格式输出百度帐号列表
    97  func (bl *BaiduUserList) String() string {
    98  	builder := &strings.Builder{}
    99  
   100  	tb := pcstable.NewTable(builder)
   101  	tb.SetColumnAlignment([]int{tablewriter.ALIGN_DEFAULT, tablewriter.ALIGN_RIGHT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER})
   102  	tb.SetHeader([]string{"#", "uid", "用户名", "性别", "age"})
   103  
   104  	for k, baiduInfo := range *bl {
   105  		tb.Append([]string{strconv.Itoa(k), strconv.FormatUint(baiduInfo.UID, 10), baiduInfo.Name, baiduInfo.Sex, fmt.Sprint(baiduInfo.Age)})
   106  	}
   107  
   108  	tb.Render()
   109  
   110  	return builder.String()
   111  }