From 18ae566d3470a31b62e255e6b09bf8d1dfbf295d Mon Sep 17 00:00:00 2001 From: fumiama Date: Fri, 14 Jan 2022 20:00:02 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A9=F0=9F=91=8C=20make=20lint=20happy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin_fortune/fortune.go | 11 +++----- plugin_gif/context.go | 53 ++++++--------------------------------- plugin_gif/gif.go | 18 ++++++------- plugin_gif/logo.go | 6 +++-- plugin_gif/png.go | 6 ++--- plugin_nativesetu/data.go | 2 +- 6 files changed, 28 insertions(+), 68 deletions(-) diff --git a/plugin_fortune/fortune.go b/plugin_fortune/fortune.go index 708c1b5c..45ac65e2 100644 --- a/plugin_fortune/fortune.go +++ b/plugin_fortune/fortune.go @@ -136,12 +136,7 @@ func init() { } // 随机获取签文 - title, text, err := randtext(seed) - if err != nil { - ctx.SendChain(message.Text("ERROR: ", err)) - return - } - + title, text := randtext(seed) digest := md5.Sum(helper.StringToBytes(zipfile + strconv.Itoa(index) + title + text)) cachefile := cache + hex.EncodeToString(digest[:]) @@ -197,10 +192,10 @@ func randimage(path string, seed int64) (im image.Image, index int, err error) { // @param file 文件路径 // @param seed 随机数种子 // @return 签名 & 签文 & 错误信息 -func randtext(seed int64) (string, string, error) { +func randtext(seed int64) (string, string) { rand.Seed(seed) r := rand.Intn(len(omikujis)) - return omikujis[r]["title"], omikujis[r]["content"], nil + return omikujis[r]["title"], omikujis[r]["content"] } // @function draw 绘制运势图 diff --git a/plugin_gif/context.go b/plugin_gif/context.go index 9ca66158..e6697466 100644 --- a/plugin_gif/context.go +++ b/plugin_gif/context.go @@ -1,13 +1,10 @@ package plugin_gif import ( - "bufio" - "io" - "net/http" "os" "strconv" - "strings" + "github.com/FloatTech/zbputils/file" "github.com/sirupsen/logrus" ) @@ -18,9 +15,8 @@ type context struct { func dlchan(name string, c *chan *string) { target := datapath + `materials/` + name - _, err := os.Stat(target) - if err != nil { - download(`https://codechina.csdn.net/u011570312/imagematerials/-/raw/main/`+name, target) + if file.IsNotExist(target) { + _ = file.DownloadTo(`https://codechina.csdn.net/u011570312/imagematerials/-/raw/main/`+name, target, true) } else { logrus.Debugln("[gif] dl", name, "exists") } @@ -29,9 +25,10 @@ func dlchan(name string, c *chan *string) { func dlblock(name string) string { target := datapath + `materials/` + name - _, err := os.Stat(target) - if err != nil { - download(`https://codechina.csdn.net/u011570312/imagematerials/-/raw/main/`+name, target) + if file.IsNotExist(target) { + _ = file.DownloadTo(`https://codechina.csdn.net/u011570312/imagematerials/-/raw/main/`+name, target, true) + } else { + logrus.Debugln("[gif] dl", name, "exists") } return target } @@ -49,43 +46,9 @@ func dlrange(prefix string, suffix string, end int) *[]chan *string { func newContext(user int64) *context { c := new(context) c.usrdir = datapath + "users/" + strconv.FormatInt(user, 10) + `/` - os.MkdirAll(c.usrdir, 0755) + _ = os.MkdirAll(c.usrdir, 0755) c.headimgsdir = make([]string, 2) c.headimgsdir[0] = c.usrdir + "0.gif" c.headimgsdir[1] = c.usrdir + "1.gif" return c } - -// 下载图片 -func download(url, dlpath string) error { - // 创建目录 - var List = strings.Split(dlpath, `/`) - err := os.MkdirAll(strings.TrimSuffix(dlpath, List[len(List)-1]), 0755) - if err != nil { - logrus.Errorln("[gif] mkdir err:", err) - return err - } - res, err := http.Get(url) - if err != nil { - logrus.Errorln("[gif] http get err:", err) - return err - } - // 获得get请求响应的reader对象 - reader := bufio.NewReaderSize(res.Body, 32*1024) - // 创建文件 - file, err := os.Create(dlpath) - if err != nil { - logrus.Errorln("[gif] create file err:", err) - return err - } - // 获得文件的writer对象 - writer := bufio.NewWriter(file) - written, err := io.Copy(writer, reader) - if err != nil { - logrus.Errorln("[gif] copy err:", err) - return err - } - res.Body.Close() - logrus.Debugln("[gif] dl len:", written) - return nil -} diff --git a/plugin_gif/gif.go b/plugin_gif/gif.go index a1914517..027daee6 100644 --- a/plugin_gif/gif.go +++ b/plugin_gif/gif.go @@ -18,7 +18,7 @@ func (cc *context) mo() string { img.LoadFirstFrame(*<-(*c)[3], 0, 0).InsertBottom(tou, 85, 75, 27, 37).Im, img.LoadFirstFrame(*<-(*c)[4], 0, 0).InsertBottom(tou, 90, 70, 22, 42).Im, } - img.SaveGif(img.MergeGif(1, mo), name) + _ = img.SaveGif(img.MergeGif(1, mo), name) return "file:///" + name } @@ -38,7 +38,7 @@ func (cc *context) cuo() string { img.LoadFirstFrame(*<-(*c)[3], 0, 0).InsertBottomC(m3.Im, 0, 0, 75, 130).Im, img.LoadFirstFrame(*<-(*c)[4], 0, 0).InsertBottomC(m4.Im, 0, 0, 75, 130).Im, } - img.SaveGif(img.MergeGif(5, cuo), name) + _ = img.SaveGif(img.MergeGif(5, cuo), name) return "file:///" + name } @@ -51,7 +51,7 @@ func (cc *context) qiao() string { img.LoadFirstFrame(*<-(*c)[0], 0, 0).InsertUp(tou, 40, 33, 57, 52).Im, img.LoadFirstFrame(*<-(*c)[1], 0, 0).InsertUp(tou, 38, 36, 58, 50).Im, } - img.SaveGif(img.MergeGif(1, qiao), name) + _ = img.SaveGif(img.MergeGif(1, qiao), name) return "file:///" + name } @@ -65,7 +65,7 @@ func (cc *context) chi() string { img.LoadFirstFrame(*<-(*c)[1], 0, 0).InsertBottom(tou, 0, 0, 1, 38).Im, img.LoadFirstFrame(*<-(*c)[2], 0, 0).InsertBottom(tou, 0, 0, 1, 38).Im, } - img.SaveGif(img.MergeGif(1, chi), name) + _ = img.SaveGif(img.MergeGif(1, chi), name) return "file:///" + name } @@ -83,7 +83,7 @@ func (cc *context) ceng() string { img.LoadFirstFrame(*<-(*c)[4], 0, 0).InsertUp(tou, 75, 77, 56, 110).InsertUp(img.Rotate(tou2, -66, 132, 80).Im, 0, 0, 78, 40).Im, img.LoadFirstFrame(*<-(*c)[5], 0, 0).InsertUp(tou, 75, 77, 62, 102).InsertUp(tou2, 71, 100, 110, 94).Im, } - img.SaveGif(img.MergeGif(8, ceng), name) + _ = img.SaveGif(img.MergeGif(8, ceng), name) return "file:///" + name } @@ -110,7 +110,7 @@ func (cc *context) ken() string { img.LoadFirstFrame(*<-(*c)[14], 0, 0).Im, img.LoadFirstFrame(*<-(*c)[15], 0, 0).Im, } - img.SaveGif(img.MergeGif(7, ken), name) + _ = img.SaveGif(img.MergeGif(7, ken), name) return "file:///" + name } @@ -123,7 +123,7 @@ func (cc *context) pai() string { img.LoadFirstFrame(*<-(*c)[0], 0, 0).InsertUp(tou, 0, 0, 1, 47).Im, img.LoadFirstFrame(*<-(*c)[1], 0, 0).InsertUp(tou, 0, 0, 1, 67).Im, } - img.SaveGif(img.MergeGif(1, pai), name) + _ = img.SaveGif(img.MergeGif(1, pai), name) return "file:///" + name } @@ -136,7 +136,7 @@ func (cc *context) chong() string { img.LoadFirstFrame(*<-(*c)[0], 0, 0).InsertUp(tou, 30, 30, 15, 53).Im, img.LoadFirstFrame(*<-(*c)[1], 0, 0).InsertUp(tou, 30, 30, 40, 53).Im, } - img.SaveGif(img.MergeGif(1, chong), name) + _ = img.SaveGif(img.MergeGif(1, chong), name) return "file:///" + name } @@ -155,6 +155,6 @@ func (cc *context) diu() string { img.LoadFirstFrame(*<-(*c)[6], 0, 0).InsertUp(tou, 35, 35, 259, 31).Im, img.LoadFirstFrame(*<-(*c)[7], 0, 0).InsertUp(tou, 175, 175, -50, 220).Im, } - img.SaveGif(img.MergeGif(7, diu), name) + _ = img.SaveGif(img.MergeGif(7, diu), name) return "file:///" + name } diff --git a/plugin_gif/logo.go b/plugin_gif/logo.go index 57914a28..b76be4b4 100644 --- a/plugin_gif/logo.go +++ b/plugin_gif/logo.go @@ -3,15 +3,17 @@ package plugin_gif import ( "strconv" "strings" + + "github.com/FloatTech/zbputils/file" ) func (c *context) prepareLogos(s ...string) { for i, v := range s { _, err := strconv.Atoi(v) if err != nil { - download("https://gchat.qpic.cn/gchatpic_new//--"+strings.ToUpper(v)+"/0", c.usrdir+strconv.Itoa(i)+".gif") + file.DownloadTo("https://gchat.qpic.cn/gchatpic_new//--"+strings.ToUpper(v)+"/0", c.usrdir+strconv.Itoa(i)+".gif", true) } else { - download("http://q4.qlogo.cn/g?b=qq&nk="+v+"&s=640", c.usrdir+strconv.Itoa(i)+".gif") + file.DownloadTo("http://q4.qlogo.cn/g?b=qq&nk="+v+"&s=640", c.usrdir+strconv.Itoa(i)+".gif", true) } } } diff --git a/plugin_gif/png.go b/plugin_gif/png.go index 71f6154d..b10a690a 100644 --- a/plugin_gif/png.go +++ b/plugin_gif/png.go @@ -16,7 +16,7 @@ func (cc *context) pa() string { rand := rand.Intn(60) + 1 dc := img.LoadFirstFrame(dlblock(`pa/`+strconv.Itoa(rand)+`.png`), 0, 0). InsertBottom(tou, 100, 100, 0, 400).Im - img.SavePng(dc, name) + _ = img.SavePng(dc, name) return "file:///" + name } @@ -29,7 +29,7 @@ func (cc *context) si() string { dc := img.LoadFirstFrame(dlblock(`si/0.png`), 0, 0). InsertBottom(im1.Im, im1.W, im1.H, -3, 370). InsertBottom(im2.Im, im2.W, im2.H, 653, 310).Im - img.SavePng(dc, name) + _ = img.SavePng(dc, name) return "file:///" + name } @@ -61,6 +61,6 @@ func (cc *context) other(value ...string) string { h, _ := strconv.Atoi(value[2]) a = img.Size(im.Im, w, h).Im } - img.SavePng(a, name) + _ = img.SavePng(a, name) return "file:///" + name } diff --git a/plugin_nativesetu/data.go b/plugin_nativesetu/data.go index 9d107556..60074eb2 100644 --- a/plugin_nativesetu/data.go +++ b/plugin_nativesetu/data.go @@ -10,7 +10,7 @@ import ( "github.com/corona10/goimagehash" "github.com/sirupsen/logrus" "github.com/wdvxdr1123/ZeroBot/utils/helper" - _ "golang.org/x/image/webp" + _ "golang.org/x/image/webp" // import webp decoding "github.com/FloatTech/zbputils/file" "github.com/FloatTech/zbputils/process"