From 8931b9a4a5b7f010c317a5dbfe3a2d444d8b2e68 Mon Sep 17 00:00:00 2001 From: RicheyJang <773602577@qq.com> Date: Thu, 24 Mar 2022 14:35:43 +0800 Subject: [PATCH] fix: better API --- plugins/pixiv/download.go | 37 ++++++++++++++++++++++++++++++------- plugins/pixiv/init.go | 13 ++++++++----- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/plugins/pixiv/download.go b/plugins/pixiv/download.go index aec2013..4ee5a80 100644 --- a/plugins/pixiv/download.go +++ b/plugins/pixiv/download.go @@ -126,13 +126,7 @@ func (pic *PictureInfo) GenSinglePicMsg() (message.Message, error) { } } // 下载图片 - path, err := images.GetNewTempSavePath("pixiv") - if err != nil { - return nil, err - } - c := client.NewHttpClient(&client.HttpOptions{TryTime: 2, Timeout: getTimeout()}) - c.SetUserAgent() - err = c.DownloadToFile(path, pic.URL) + path, err := pic.GetPicture() if err != nil { return nil, err } @@ -146,6 +140,35 @@ func (pic *PictureInfo) GenSinglePicMsg() (message.Message, error) { return message.Message{message.Text(pic.Title), picMsg, message.Text(tip)}, nil } +// GetPicture 下载图片,返回图片保存路径 +func (pic *PictureInfo) GetPicture() (path string, err error) { + // 初始化 + if pic == nil { + return "", fmt.Errorf("pic is nil") + } + if !pic.CheckNoSESE() { + return "", fmt.Errorf("不可以涩涩!") + } + if len(pic.URL) == 0 { + err = pic.GetURLByPID() + if err != nil { + return "", fmt.Errorf("GetURLByPID failed: %v", err) + } + } + // 下载图片 + path, err = images.GetNewTempSavePath("pixiv") + if err != nil { + return "", fmt.Errorf("GetNewTempSavePath err: %v", err) + } + c := client.NewHttpClient(&client.HttpOptions{TryTime: 2, Timeout: getTimeout()}) + c.SetUserAgent() + err = c.DownloadToFile(path, pic.URL) + if err != nil { + return "", err + } + return +} + // GetDescribe 获取图片说明 func (pic *PictureInfo) GetDescribe() string { if !pic.CheckNoSESE() { diff --git a/plugins/pixiv/init.go b/plugins/pixiv/init.go index 0a61c36..18605a4 100644 --- a/plugins/pixiv/init.go +++ b/plugins/pixiv/init.go @@ -35,13 +35,16 @@ var proxy *manager.PluginProxy type PictureInfo struct { Title string // 标题 - URL string // 图片链接 + // 下载所需 + URL string // 图片链接 + PID int64 // 下载图片时要么有URL;要么有PID及P + P int // 分P + + // 描述所需 Tags []string // 标签 - PID int64 - P int // 分P - Author string // 作者 - UID int64 // 作者UID + Author string // 作者 + UID int64 // 作者UID Src string // 无需填写,来源图库 }