github.com/qjfoidnh/BaiduPCS-Go@v0.0.0-20231011165705-caa18a3765f3/internal/pcscommand/locatedonwload.go (about)

     1  package pcscommand
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/qjfoidnh/BaiduPCS-Go/internal/pcsconfig"
     6  	"github.com/qjfoidnh/BaiduPCS-Go/pcstable"
     7  	"github.com/qjfoidnh/BaiduPCS-Go/pcsutil/converter"
     8  	"os"
     9  	"strconv"
    10  )
    11  
    12  // RunLocateDownload 执行获取直链
    13  func RunLocateDownload(pcspaths []string, opt *LocateDownloadOption) {
    14  	if opt == nil {
    15  		opt = &LocateDownloadOption{}
    16  	}
    17  
    18  	absPaths, err := matchPathByShellPattern(pcspaths...)
    19  	if err != nil {
    20  		fmt.Println(err)
    21  		return
    22  	}
    23  
    24  	pcs := GetBaiduPCS()
    25  
    26  	if opt.FromPan {
    27  		fds, err := pcs.FilesDirectoriesBatchMeta(absPaths...)
    28  		if err != nil {
    29  			fmt.Printf("%s\n", err)
    30  			return
    31  		}
    32  
    33  		fidList := make([]int64, 0, len(fds))
    34  		for i := range fds {
    35  			fidList = append(fidList, fds[i].FsID)
    36  		}
    37  
    38  		list, err := pcs.LocatePanAPIDownload(fidList...)
    39  		if err != nil {
    40  			fmt.Printf("%s\n", err)
    41  			return
    42  		}
    43  
    44  		tb := pcstable.NewTable(os.Stdout)
    45  		tb.SetHeader([]string{"#", "fs_id", "路径", "链接"})
    46  
    47  		var (
    48  			i          int
    49  			fidStrList = converter.SliceInt64ToString(fidList)
    50  		)
    51  		for k := range fidStrList {
    52  			for i = range list {
    53  				if fidStrList[k] == list[i].FsID {
    54  					tb.Append([]string{strconv.Itoa(k), list[i].FsID, fds[k].Path, list[i].Dlink})
    55  					list = append(list[:i], list[i+1:]...)
    56  					break
    57  				}
    58  			}
    59  		}
    60  		tb.Render()
    61  		fmt.Printf("\n注意: 以上链接不能直接访问, 需要登录百度帐号才可以下载\n")
    62  		return
    63  	}
    64  
    65  	for i, pcspath := range absPaths {
    66  		info, err := pcs.LocateDownload(pcspath)
    67  		if err != nil {
    68  			fmt.Printf("[%d] %s, 路径: %s\n", i, err, pcspath)
    69  			continue
    70  		}
    71  
    72  		fmt.Printf("[%d] %s: \n", i, pcspath)
    73  		tb := pcstable.NewTable(os.Stdout)
    74  		tb.SetHeader([]string{"#", "链接"})
    75  		for k, u := range info.URLStrings(pcsconfig.Config.EnableHTTPS) {
    76  			tb.Append([]string{strconv.Itoa(k), u.String()})
    77  		}
    78  		tb.Render()
    79  		fmt.Println()
    80  	}
    81  	fmt.Printf("提示: 访问下载链接, 需将下载器的 User-Agent 设置为: %s\n", pcsconfig.Config.PanUA)
    82  }