增加 pixiv 多线程下载与图片缓存

This commit is contained in:
fumiama 2022-01-24 14:01:30 +08:00
parent f7c6d67428
commit aa1f82d46f
5 changed files with 34 additions and 24 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/FloatTech/ZeroBot-Plugin
go 1.17 go 1.17
require ( require (
github.com/FloatTech/AnimeAPI v1.2.4-beta19 github.com/FloatTech/AnimeAPI v1.2.4-beta21
github.com/FloatTech/zbputils v1.2.4-beta7 github.com/FloatTech/zbputils v1.2.4-beta7
github.com/antchfx/htmlquery v1.2.4 github.com/antchfx/htmlquery v1.2.4
github.com/corona10/goimagehash v1.0.3 github.com/corona10/goimagehash v1.0.3

4
go.sum
View File

@ -1,5 +1,5 @@
github.com/FloatTech/AnimeAPI v1.2.4-beta19 h1:Yz19ppAdx9cZZbrJBEPqon6RNHY1TuCQl7SALFIW1lQ= github.com/FloatTech/AnimeAPI v1.2.4-beta21 h1:C0CoX6mrEAOEWl1sLUhGBTasvwnwMkEMEGcDI4fps1I=
github.com/FloatTech/AnimeAPI v1.2.4-beta19/go.mod h1:rhYvzqLH2YTKBOk7DfJsXsdZlnxhpBP3HvctwqlLykM= github.com/FloatTech/AnimeAPI v1.2.4-beta21/go.mod h1:rhYvzqLH2YTKBOk7DfJsXsdZlnxhpBP3HvctwqlLykM=
github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ= github.com/FloatTech/bot-manager v1.0.0/go.mod h1:8YYRJ16oroGHQGD2En0oVnmcKJkxR9O/jd5BPSfWfOQ=
github.com/FloatTech/zbputils v1.2.4-beta7 h1:DofQTSVEBU1BQnLb4GahNZqsqVqoj4Lu5GDJa2rJW9o= github.com/FloatTech/zbputils v1.2.4-beta7 h1:DofQTSVEBU1BQnLb4GahNZqsqVqoj4Lu5GDJa2rJW9o=
github.com/FloatTech/zbputils v1.2.4-beta7/go.mod h1:mPVpKu2scTyBiitNmzCrxGc9aIKs9rwfb6iqz/+heck= github.com/FloatTech/zbputils v1.2.4-beta7/go.mod h1:mPVpKu2scTyBiitNmzCrxGc9aIKs9rwfb6iqz/+heck=

View File

@ -54,8 +54,7 @@ func init() {
} }
url := json.Get("data.0.urls.original").Str url := json.Get("data.0.urls.original").Str
url = strings.ReplaceAll(url, "i.pixiv.cat", "i.pixiv.re") url = strings.ReplaceAll(url, "i.pixiv.cat", "i.pixiv.re")
id := json.Get("data.0.pid").String() m, err := imgpool.NewImage(ctx, url[:strings.LastIndex(url, "/")+1], url)
m, err := imgpool.NewImage(ctx, id, url)
if err == nil { if err == nil {
queue <- m.String() queue <- m.String()
} else { } else {

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
"github.com/FloatTech/AnimeAPI/ascii2d" "github.com/FloatTech/AnimeAPI/ascii2d"
"github.com/FloatTech/AnimeAPI/imgpool"
"github.com/FloatTech/AnimeAPI/pixiv" "github.com/FloatTech/AnimeAPI/pixiv"
"github.com/FloatTech/AnimeAPI/saucenao" "github.com/FloatTech/AnimeAPI/saucenao"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -49,22 +50,32 @@ func init() { // 插件主体
} }
if illust.Pid > 0 { if illust.Pid > 0 {
name := strconv.FormatInt(illust.Pid, 10) name := strconv.FormatInt(illust.Pid, 10)
filepath := datapath + name var imgs message.Message
for i, u := range illust.ImageUrls {
n := name + "_p" + strconv.Itoa(i)
filepath := datapath + n
f := ""
switch { switch {
case file.IsExist(filepath + ".jpg"): case file.IsExist(filepath + ".jpg"):
filepath = "file:///" + filepath + ".jpg" f = filepath + ".jpg"
case file.IsExist(filepath + ".png"): case file.IsExist(filepath + ".png"):
filepath = "file:///" + filepath + ".png" f = filepath + ".png"
case file.IsExist(filepath + ".gif"): case file.IsExist(filepath + ".gif"):
filepath = "file:///" + filepath + ".gif" f = filepath + ".gif"
default: default:
filepath = "" logrus.Debugln("[sausenao]开始下载", n)
} filepath, err = pixiv.Download(u, datapath, n)
if filepath == "" {
logrus.Debug("[sausenao]开始下载", name)
filepath, err = pixiv.Download(illust.ImageUrls, datapath, name)
if err == nil { if err == nil {
filepath = "file:///" + filepath f = filepath
}
}
if f != "" {
m, err := imgpool.NewImage(ctx, n, f)
if err == nil {
imgs = append(imgs, message.Image(m.String()))
} else {
imgs = append(imgs, message.Image("file:///"+f))
}
} }
} }
txt := message.Text( txt := message.Text(
@ -74,9 +85,9 @@ func init() { // 插件主体
"画师ID", illust.UserId, "\n", "画师ID", illust.UserId, "\n",
"直链:", "https://pixivel.moe/detail?id=", illust.Pid, "直链:", "https://pixivel.moe/detail?id=", illust.Pid,
) )
if filepath != "" { if imgs != nil {
// 发送搜索结果 // 发送搜索结果
ctx.SendChain(message.Image(filepath), message.Text("\n"), txt) ctx.Send(append(imgs, message.Text("\n"), txt))
} else { } else {
// 图片下载失败,仅发送文字结果 // 图片下载失败,仅发送文字结果
ctx.SendChain(txt) ctx.SendChain(txt)

View File

@ -245,7 +245,7 @@ func download(i *pixiv.Illust, filedir string) /*(string, */ error /*)*/ {
return /*filepath + ".gif",*/ nil return /*filepath + ".gif",*/ nil
} }
// 下载最大分辨率为 1200 的图片 // 下载最大分辨率为 1200 的图片
link := i.ImageUrls link := i.ImageUrls[0]
link = strings.ReplaceAll(link, "img-original", "img-master") link = strings.ReplaceAll(link, "img-original", "img-master")
link = strings.ReplaceAll(link, "_p0", "_p0_master1200") link = strings.ReplaceAll(link, "_p0", "_p0_master1200")
link = strings.ReplaceAll(link, ".png", ".jpg") link = strings.ReplaceAll(link, ".png", ".jpg")