Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions cmd/new/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,34 @@ import (

"github.com/52funny/pikpakcli/conf"
"github.com/52funny/pikpakcli/internal/pikpak"
"github.com/sirupsen/logrus"
"github.com/52funny/pikpakcli/internal/utils"
"github.com/spf13/cobra"
)

var (
path, parentId, input, outputFormatVal string
cli bool
err error
logClient utils.Log
)

var NewUrlCommand = &cobra.Command{
Use: "url",
Short: `Create a file according to url`,
Run: func(cmd *cobra.Command, args []string) {

flagset := cmd.InheritedFlags()
outputFormatVal, err = flagset.GetString("output")
if err != nil {
panic(err)
}

logClient = utils.NewLog(outputFormatVal)

p := pikpak.NewPikPak(conf.Config.Username, conf.Config.Password)
err := p.Login()
err = p.Login()
if err != nil {
logrus.Errorln("Login Failed:", err)
logClient.Errorf("Login Failed: %v", err)
}
if cli {
handleCli(&p)
Expand All @@ -30,7 +46,7 @@ var NewUrlCommand = &cobra.Command{
if strings.TrimSpace(input) != "" {
f, err := os.OpenFile(input, os.O_RDONLY, 0666)
if err != nil {
logrus.Errorln("Open file %s failed:", input, err)
logClient.Errorf("Open file %s failed: %v", input, err)
return
}
reader := bufio.NewReader(f)
Expand All @@ -50,19 +66,11 @@ var NewUrlCommand = &cobra.Command{
if len(args) > 0 {
handleNewUrl(&p, args)
} else {
logrus.Errorln("Please input the folder name")
logClient.Error("Please input the folder name")
}
},
}

var path string

var parentId string

var input string

var cli bool

func init() {
NewUrlCommand.Flags().StringVarP(&path, "path", "p", "/", "The path of the folder")
NewUrlCommand.Flags().StringVarP(&parentId, "parent-id", "P", "", "The parent id")
Expand All @@ -76,26 +84,29 @@ func handleNewUrl(p *pikpak.PikPak, shas []string) {
if parentId == "" {
parentId, err = p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
logClient.Errorf("Get parent id failed: %v\n", err)
return
}
}
for _, url := range shas {
err := p.CreateUrlFile(parentId, url)
taskInfo, err := p.CreateUrlFile(parentId, url)
if err != nil {
logrus.Errorln("Create url file failed: ", err)
logClient.Errorf("Create url file failed: %v\n", err)
continue
}
logrus.Infoln("Create url file success: ", url)

logClient.Infof("Create url file success: %s\n", url)
logClient.Info(taskInfo)
}

}

func handleCli(p *pikpak.PikPak) {
var err error
if parentId == "" {
parentId, err = p.GetPathFolderId(path)
if err != nil {
logrus.Errorf("Get parent id failed: %s\n", err)
logClient.Errorf("Get parent id failed: %v\n", err)
return
}
}
Expand All @@ -108,11 +119,13 @@ func handleCli(p *pikpak.PikPak) {
break
}
url := string(lineBytes)
err = p.CreateUrlFile(parentId, url)
taskInfo, err := p.CreateUrlFile(parentId, url)
if err != nil {
logrus.Errorln("Create url file failed: ", err)
logClient.Errorf("Create url file failed: %v\n", err)
continue
}
logrus.Infoln("Create url file success: ", url)
logClient.Infof("Create url file success: ", url)

logClient.Info(taskInfo)
}
}
11 changes: 6 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ var rootCmd = &cobra.Command{
},
}

// Config path
var configPath string

// Debug mode
var debug bool
var (
configPath, output string // Config Path
debug bool // Debug Mode
)

// Initialize the command line interface
func init() {
// debug

rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "debug mode")
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "text", "Output Format:[json,text]")
// config
rootCmd.PersistentFlags().StringVar(&configPath, "config", "config.yml", "config file path")
rootCmd.AddCommand(upload.UploadCmd)
Expand Down
14 changes: 7 additions & 7 deletions internal/pikpak/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/sirupsen/logrus"
)

func (p *PikPak) CreateUrlFile(parentId, url string) error {
func (p *PikPak) CreateUrlFile(parentId, url string) (string, error) {
m := map[string]interface{}{
"kind": "drive#file",
"upload_type": "UPLOAD_TYPE_URL",
Expand All @@ -22,12 +22,12 @@ func (p *PikPak) CreateUrlFile(parentId, url string) error {
}
bs, err := jsoniter.Marshal(&m)
if err != nil {
return err
return "", err
}
START:
req, err := http.NewRequest("POST", "https://api-drive.mypikpak.com/drive/v1/files", bytes.NewBuffer(bs))
if err != nil {
return err
return "", err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Product_flavor_name", "cha")
Expand All @@ -39,18 +39,18 @@ START:
req.Header.Set("Country", "CN")
bs, err = p.sendRequest(req)
if err != nil {
return err
return "", err
}
error_code := jsoniter.Get(bs, "error_code").ToInt()
if error_code != 0 {
if error_code == 9 {
err := p.AuthCaptchaToken("POST:/drive/v1/files")
if err != nil {
return err
return "", err
}
goto START
}
return fmt.Errorf("upload file error: %s", jsoniter.Get(bs, "error").ToString())
return "", fmt.Errorf("upload file error: %s", jsoniter.Get(bs, "error").ToString())
}

task := jsoniter.Get(bs, "task")
Expand All @@ -61,5 +61,5 @@ START:
// } else {
// return fmt.Errorf("create file error: %s", phase)
// }
return nil
return task.ToString(), nil
}
110 changes: 110 additions & 0 deletions internal/utils/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package utils

import (
"os"

"github.com/sirupsen/logrus"
)

const (
WarnLevel = logrus.WarnLevel
InfoLevel = logrus.InfoLevel
DebugLevel = logrus.DebugLevel
ErrorLevel = logrus.ErrorLevel
)

var (
timeFormat = "2006-01-02 15:04:05.999999 -0700"
)

type LogStruct struct {
log *logrus.Logger
}

type Log interface {
SetLevel(level logrus.Level)
SetLogFormat(format logrus.Formatter)
Info(message string)
Warn(message string)
Debug(message string)
Error(message string)

Infof(message string, args ...interface{})
Warnf(message string, args ...interface{})
Debugf(message string, args ...interface{})
Errorf(message string, args ...interface{})
}

func NewLog(format string) Log {

var (logFormat logrus.Formatter)
log := logrus.New()
log.SetOutput(os.Stdout)
log.SetLevel(InfoLevel)
logFieldMap := logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyLevel: "level",
logrus.FieldKeyMsg: "message",
logrus.FieldKeyFunc: "caller",
}


switch format {
case "json":
logFormat = &logrus.JSONFormatter{
FieldMap: logFieldMap,
TimestampFormat: timeFormat,
}
case "text":
logFormat = &logrus.TextFormatter{
FieldMap: logFieldMap,
TimestampFormat: timeFormat,
}
}

log.SetFormatter(logFormat)

return &LogStruct{
log: log,
}
}

func (l *LogStruct) SetLevel(level logrus.Level) {
l.log.SetLevel(level)
}

func (l *LogStruct) SetLogFormat(format logrus.Formatter) {
l.log.SetFormatter(format)
}

func (l *LogStruct) Info(message string) {
l.log.Info(message)
}

func (l *LogStruct) Infof(message string, args ...interface{}) {
l.log.Infof(message, args...)
}

func (l *LogStruct) Warn(message string) {
l.log.Warn(message)
}

func (l *LogStruct) Warnf(message string, args ...interface{}) {
l.log.Warnf(message, args...)
}

func (l *LogStruct) Debug(message string) {
l.log.Debug(message)
}

func (l *LogStruct) Debugf(message string, args ...interface{}) {
l.log.Debugf(message, args...)
}

func (l *LogStruct) Error(message string) {
l.log.Error(message)
}

func (l *LogStruct) Errorf(message string, args ...interface{}) {
l.log.Errorf(message, args...)
}