From 7fc4a6425b9a8f95e3dabd63db92ed665b77c7b8 Mon Sep 17 00:00:00 2001 From: fumiama Date: Wed, 8 Dec 2021 16:44:46 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20=20=E8=BF=81=E7=A7=BB?= =?UTF-8?q?=E5=88=B0=20utils/sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin_book_review/book_review.go | 31 +++---------- plugin_book_review/data.go | 10 ++++- plugin_book_review/model.go | 16 +++++++ plugin_book_review/model/model.go | 73 ------------------------------- 4 files changed, 30 insertions(+), 100 deletions(-) create mode 100644 plugin_book_review/model.go delete mode 100644 plugin_book_review/model/model.go diff --git a/plugin_book_review/book_review.go b/plugin_book_review/book_review.go index ae9bd32143..859766cb2e 100644 --- a/plugin_book_review/book_review.go +++ b/plugin_book_review/book_review.go @@ -1,48 +1,27 @@ package plugin_book_review import ( - log "github.com/sirupsen/logrus" - zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" - "github.com/FloatTech/ZeroBot-Plugin/plugin_book_review/model" ) -const dbpath = "data/BookReview/" -const dbfile = dbpath + "bookreview.db" - -var ( - engine = control.Register("bookreview", &control.Options{ +func init() { + engine := control.Register("bookreview", &control.Options{ DisableOnDefault: false, Help: "哀伤雪刃推书记录\n- 书评[xxx]\n- 随机书评", }) -) -func init() { engine.OnRegex("^书评(.{1,25})$").SetBlock(true). Handle(func(ctx *zero.Ctx) { - db, err := model.Open(dbfile) - if err != nil { - log.Errorln(err) - return - } - BookReviewList := db.GetBookReviewByKeyword(ctx.State["regex_matched"].([]string)[1]) - ctx.SendChain(message.Text(BookReviewList.BookReview)) - db.Close() + b := getBookReviewByKeyword(ctx.State["regex_matched"].([]string)[1]) + ctx.SendChain(message.Text(b.BookReview)) }) engine.OnFullMatch("随机书评").SetBlock(true). Handle(func(ctx *zero.Ctx) { - db, err := model.Open(dbfile) - if err != nil { - log.Errorln(err) - return - } - br := db.GetRandomBookReview() + br := getRandomBookReview() ctx.SendChain(message.Text(br.BookReview)) - db.Close() }) - } diff --git a/plugin_book_review/data.go b/plugin_book_review/data.go index 64d6c097d6..b650fb5d33 100644 --- a/plugin_book_review/data.go +++ b/plugin_book_review/data.go @@ -9,10 +9,15 @@ import ( "github.com/FloatTech/ZeroBot-Plugin/utils/file" "github.com/FloatTech/ZeroBot-Plugin/utils/process" + "github.com/FloatTech/ZeroBot-Plugin/utils/sql" ) +const dbpath = "data/BookReview/" +const dbfile = dbpath + "bookreview.db" const dburl = "https://codechina.csdn.net/anto_july/bookreview/-/raw/master/bookreview.db" +var db = &sql.Sqlite{DBPath: dbfile} + // 加载数据库 func init() { go func() { @@ -34,12 +39,15 @@ func init() { data, err := io.ReadAll(resp.Body) if err == nil && len(data) > 0 { _, _ = f.Write(data) - return } panic(err) } } panic(err) } + err := db.Create("book_review", &book{}) + if err != nil { + panic(err) + } }() } diff --git a/plugin_book_review/model.go b/plugin_book_review/model.go new file mode 100644 index 0000000000..5fa0353fa5 --- /dev/null +++ b/plugin_book_review/model.go @@ -0,0 +1,16 @@ +package plugin_book_review + +type book struct { + BookReview string `db:"book_review"` +} + +// 暂时随机选择一个书评 +func getBookReviewByKeyword(keyword string) (b book) { + db.Find("book_review", &b, "where book_review LIKE %"+keyword+"%") + return +} + +func getRandomBookReview() (b book) { + db.Pick("book_review", &b) + return +} diff --git a/plugin_book_review/model/model.go b/plugin_book_review/model/model.go deleted file mode 100644 index 425cee6c7e..0000000000 --- a/plugin_book_review/model/model.go +++ /dev/null @@ -1,73 +0,0 @@ -package model - -import ( - "math/rand" - "os" - "time" - - log "github.com/sirupsen/logrus" - - "github.com/jinzhu/gorm" - _ "github.com/logoove/sqlite" -) - -type BrDB gorm.DB - -func Initialize(dbpath string) *BrDB { - var err error - if _, err = os.Stat(dbpath); err != nil || os.IsNotExist(err) { - // 生成文件 - f, err := os.Create(dbpath) - if err != nil { - return nil - } - defer f.Close() - } - gdb, err := gorm.Open("sqlite3", dbpath) - if err != nil { - panic(err) - } - gdb.AutoMigrate(&BookReview{}) - return (*BrDB)(gdb) -} - -func Open(dbpath string) (*BrDB, error) { - db, err := gorm.Open("sqlite3", dbpath) - if err != nil { - return nil, err - } else { - return (*BrDB)(db), nil - } -} - -type BookReview struct { - gorm.Model - BookReview string `gorm:"column:book_review"` -} - -func (BookReview) TableName() string { - return "book_review" -} - -// 暂时随机选择一个书评 -func (brdb *BrDB) GetBookReviewByKeyword(keyword string) (BookReviewList BookReview) { - db := (*gorm.DB)(brdb) - rand.Seed(time.Now().UnixNano()) - var count int - db.Debug().Model(&BookReview{}).Where("book_review LIKE ?", "%"+keyword+"%").Count(&count).Offset(rand.Intn(count)).Take(&BookReviewList) - log.Println(BookReviewList) - return BookReviewList -} - -func (brdb *BrDB) GetRandomBookReview() (bookReview BookReview) { - db := (*gorm.DB)(brdb) - rand.Seed(time.Now().UnixNano()) - var count int - db.Debug().Model(&BookReview{}).Count(&count).Offset(rand.Intn(count)).Take(&bookReview) - return bookReview -} - -func (brdb *BrDB) Close() error { - db := (*gorm.DB)(brdb) - return db.Close() -}