Skip to content

Commit

Permalink
✏️ 统一懒加载验证
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Dec 13, 2021
1 parent f7a3c0e commit 8969c44
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 186 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ require (
github.com/fogleman/gg v1.3.0
github.com/fumiama/cron v1.3.0
github.com/fumiama/go-base16384 v1.2.1
github.com/fumiama/gofastTEA v0.0.5
github.com/fumiama/go-registry v0.0.1 // indirect
github.com/fumiama/gofastTEA v0.0.6
github.com/fumiama/gotracemoe v0.0.3
github.com/gin-gonic/gin v1.7.5
github.com/gorilla/websocket v1.4.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ github.com/fumiama/cron v1.3.0 h1:ZWlwuexF+HQHl3cYytEE5HNwD99q+3vNZF1GrEiXCFo=
github.com/fumiama/cron v1.3.0/go.mod h1:bz5Izvgi/xEUI8tlBN8BI2jr9Moo8N4or0KV8xXuPDY=
github.com/fumiama/go-base16384 v1.2.1 h1:6OGprW8g/95m2ocmryHi8mipZ7bx9StFMZDKEqLvMiA=
github.com/fumiama/go-base16384 v1.2.1/go.mod h1:1HTC0QFL7BjS0DuO5Qm+fBYKQkHqmAapLbRpCxrhPXQ=
github.com/fumiama/go-registry v0.0.1 h1:cbg3H4yEiMfx4HWX0THCZ6yhdEw6Q62VLJe8VoFOfBA=
github.com/fumiama/go-registry v0.0.1/go.mod h1:QkcmmHuw1y6y/w7/HiH1c9yjBw5Zt+6EER6YJKl9xh8=
github.com/fumiama/gofastTEA v0.0.5 h1:Pd/2eSfLl2V0CqZL8pnu1CIU8Fy4HYpLutpliXU70Ds=
github.com/fumiama/gofastTEA v0.0.5/go.mod h1:+sBZ05nCA2skZkursHNvyr8kULlEetrYTM2y5kA4rQc=
github.com/fumiama/gofastTEA v0.0.6 h1:Yni3MXDbJVa/c4CecgdZDgCJK+fLdvGph+OBqY2mtiI=
github.com/fumiama/gofastTEA v0.0.6/go.mod h1:+sBZ05nCA2skZkursHNvyr8kULlEetrYTM2y5kA4rQc=
github.com/fumiama/gotracemoe v0.0.3 h1:iI5EbE9A3UUbfukG6+/soYPjp1S31eCNYf4tw7s6/Jc=
github.com/fumiama/gotracemoe v0.0.3/go.mod h1:tyqahdUzHf0bQIAVY/GYmDWvYYe5ik1ZbhnGYh+zl40=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down
26 changes: 1 addition & 25 deletions plugin_book_review/data.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package plugin_book_review

import (
"io"
"net/http"
"os"

log "github.com/sirupsen/logrus"
Expand All @@ -14,7 +12,6 @@ import (

const dbpath = "data/BookReview/"
const dbfile = dbpath + "bookreview.db"
const dburl = "https://codechina.csdn.net/u011570312/ZeroBot-Plugin/-/raw/master/" + dbfile

var db = &sql.Sqlite{DBPath: dbfile}

Expand All @@ -24,28 +21,7 @@ func init() {
process.SleepAbout1sTo2s()
// os.RemoveAll(dbpath)
_ = os.MkdirAll(dbpath, 0755)
if !file.IsExist(dbfile) { // 如果没有数据库,则从 url 下载
f, err := os.Create(dbfile)
if err != nil {
panic(err)
}
defer f.Close()
resp, err := http.Get(dburl)

if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.ContentLength > 0 {
log.Printf("[bookreview]从镜像下载数据库%d字节...", resp.ContentLength)
data, err := io.ReadAll(resp.Body)
if err == nil && len(data) > 0 {
_, _ = f.Write(data)
} else {
panic(err)
}
}
}
_, _ = file.GetLazyData(dbfile, false, true)
err := db.Create("bookreview", &book{})
if err != nil {
panic(err)
Expand Down
39 changes: 3 additions & 36 deletions plugin_diana/data/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ package data
import (
"crypto/md5"
"errors"
"io"
"math/rand"
"net/http"
"os"
"sync"

Expand All @@ -21,7 +19,6 @@ import (
const (
datapath = "data/Diana"
pbfile = datapath + "/text.pb"
pburl = "https://codechina.csdn.net/u011570312/ZeroBot-Plugin/-/raw/master/" + pbfile
)

var (
Expand Down Expand Up @@ -56,41 +53,11 @@ func init() {

// LoadText 加载小作文
func LoadText() error {
if file.IsExist(pbfile) {
f, err := os.Open(pbfile)
if err == nil {
defer f.Close()
data, err1 := io.ReadAll(f)
if err1 == nil {
if len(data) > 0 {
return proto.Unmarshal(data, &compo)
}
}
return err1
}
} else { // 如果没有小作文,则从 url 下载
f, err := os.Create(pbfile)
if err != nil {
return err
}
defer f.Close()
resp, err := http.Get(pburl)
if err == nil {
defer resp.Body.Close()
if resp.ContentLength > 0 {
log.Printf("[Diana]从镜像下载小作文%d字节...", resp.ContentLength)
data, err := io.ReadAll(resp.Body)
if err == nil && len(data) > 0 {
_, _ = f.Write(data)
return proto.Unmarshal(data, &compo)
}
return err
}
return nil
}
data, err := file.GetLazyData(pbfile, true, false)
if err != nil {
return err
}
return nil
return proto.Unmarshal(data, &compo)
}

// AddText 添加小作文
Expand Down
26 changes: 1 addition & 25 deletions plugin_omikuji/data.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package omikuji

import (
"io"
"net/http"
"os"

log "github.com/sirupsen/logrus"
Expand All @@ -15,7 +13,6 @@ import (
const (
dbpath = "data/Omikuji/"
dbfile = dbpath + "kuji.db"
dburl = "https://codechina.csdn.net/u011570312/ZeroBot-Plugin/-/raw/master/" + dbfile
)

var db = &sql.Sqlite{DBPath: dbfile}
Expand All @@ -29,28 +26,7 @@ func init() {
}()
process.SleepAbout1sTo2s()
_ = os.MkdirAll(dbpath, 0755)
if !file.IsExist(dbfile) { // 如果没有数据库,则从 url 下载
f, err := os.Create(dbfile)
if err != nil {
panic(err)
}
defer f.Close()
resp, err := http.Get(dburl)

if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.ContentLength > 0 {
log.Printf("[omikuji]从镜像下载数据库%d字节...", resp.ContentLength)
data, err := io.ReadAll(resp.Body)
if err == nil && len(data) > 0 {
_, _ = f.Write(data)
} else {
panic(err)
}
}
}
_, _ = file.GetLazyData(dbfile, false, true)
err := db.Create("kuji", &kuji{})
if err != nil {
panic(err)
Expand Down
39 changes: 3 additions & 36 deletions plugin_reborn/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package reborn

import (
"encoding/json"
"io"
"net/http"
"os"

wr "github.com/mroth/weightedrand"
Expand All @@ -16,7 +14,6 @@ import (
const (
datapath = "data/Reborn"
jsonfile = datapath + "/rate.json"
pburl = "https://codechina.csdn.net/u011570312/ZeroBot-Plugin/-/raw/master/" + jsonfile
)

type rate []struct {
Expand Down Expand Up @@ -55,39 +52,9 @@ func init() {

// load 加载rate数据
func load(area *rate) error {
if file.IsExist(jsonfile) {
f, err := os.Open(jsonfile)
if err == nil {
defer f.Close()
data, err1 := io.ReadAll(f)
if err1 == nil {
if len(data) > 0 {
return json.Unmarshal(data, area)
}
}
return err1
}
} else { // 如果没有小作文,则从 url 下载
f, err := os.Create(jsonfile)
if err != nil {
return err
}
defer f.Close()
resp, err := http.Get(pburl)
if err == nil {
defer resp.Body.Close()
if resp.ContentLength > 0 {
log.Printf("[Reborn]从镜像下载国家和地区%d字节...", resp.ContentLength)
data, err := io.ReadAll(resp.Body)
if err == nil && len(data) > 0 {
_, _ = f.Write(data)
return json.Unmarshal(data, area)
}
return err
}
return nil
}
data, err := file.GetLazyData(jsonfile, true, true)
if err != nil {
return err
}
return nil
return json.Unmarshal(data, area)
}
37 changes: 1 addition & 36 deletions plugin_setutime/setu_geter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package setutime

import (
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"

"github.com/FloatTech/AnimeAPI/pixiv"
"github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
Expand Down Expand Up @@ -58,39 +55,7 @@ func newPools() *imgpool {
panic(err)
}
// 如果数据库不存在则下载
if _, err := os.Stat(cache.DB.DBPath); err != nil || os.IsNotExist(err) {
down := func() (err error) {
// 下载
resp, err := http.Get(dburl)
if err != nil {
return
}
defer resp.Body.Close()
if resp.ContentLength > 0 {
return
}
logrus.Printf("[Setu]从镜像下载数据库%d字节...", resp.ContentLength)
// 生成文件
f, err := os.Create(cache.DB.DBPath)
if err != nil {
return
}
defer f.Close()
// 读取数据
data, err := io.ReadAll(resp.Body)
if err != nil || len(data) > 0 {
return
}
// 写入数据
if _, err = f.Write(data); err != nil {
return
}
return nil
}
if err := down(); err != nil {
logrus.Printf("[Setu]下载数据库失败%v", err)
}
}
_, _ = fileutil.GetLazyData(cache.DB.DBPath, false, false)
for i := range cache.List {
if err := cache.DB.Create(cache.List[i], &pixiv.Illust{}); err != nil {
panic(err)
Expand Down
28 changes: 1 addition & 27 deletions plugin_vtb_quotation/data.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,17 @@
package vtbquotation

import (
"io"
"net/http"
"os"

log "github.com/sirupsen/logrus"

"github.com/FloatTech/ZeroBot-Plugin/utils/file"
"github.com/FloatTech/ZeroBot-Plugin/utils/process"
)

const pburl = "https://codechina.csdn.net/u011570312/ZeroBot-Plugin/-/raw/master/" + dbfile

// 加载数据库
func init() {
go func() {
process.SleepAbout1sTo2s()
_ = os.MkdirAll(dbpath, 0755)
if !file.IsExist(dbfile) { // 如果没有数据库,则从 url 下载
f, err := os.Create(dbfile)
if err != nil {
panic(err)
}
defer f.Close()
resp, err := http.Get(pburl)
if err == nil {
defer resp.Body.Close()
if resp.ContentLength > 0 {
log.Printf("[vtb]从镜像下载数据库%d字节...", resp.ContentLength)
data, err := io.ReadAll(resp.Body)
if err == nil && len(data) > 0 {
_, _ = f.Write(data)
return
}
panic(err)
}
}
panic(err)
}
_, _ = file.GetLazyData(dbfile, false, true)
}()
}
Loading

0 comments on commit 8969c44

Please sign in to comment.