mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 02:00:24 +00:00
feat(music): 龙珠聚合搜索 (#1179)
* 🐛 修改听歌问题 * ✨ 添加龙珠聚合搜索 * 🎨 优化聚合搜索 * 🐛 一定能点出歌 * 🎨 删除调试
This commit is contained in:
@@ -21,6 +21,10 @@ import (
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
)
|
||||
|
||||
var (
|
||||
longZhuURL = "https://www.hhlqilongzhu.cn/api/joox/juhe_music.php?msg=%v"
|
||||
)
|
||||
|
||||
func init() {
|
||||
control.AutoRegister(&ctrl.Options[*zero.Ctx]{
|
||||
DisableOnDefault: false,
|
||||
@@ -29,7 +33,8 @@ func init() {
|
||||
"- 网易点歌[xxx]\n" +
|
||||
"- 酷我点歌[xxx]\n" +
|
||||
"- 酷狗点歌[xxx]\n" +
|
||||
"- 咪咕点歌[xxx]",
|
||||
"- 咪咕点歌[xxx]\n" +
|
||||
"- qq点歌[xxx]\n",
|
||||
}).OnRegex(`^(.{0,2})点歌\s?(.{1,25})$`).SetBlock(true).Limit(ctxext.LimitByUser).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
// switch 平台
|
||||
@@ -42,12 +47,37 @@ func init() {
|
||||
ctx.SendChain(kugou(ctx.State["regex_matched"].([]string)[2]))
|
||||
case "网易":
|
||||
ctx.SendChain(cloud163(ctx.State["regex_matched"].([]string)[2]))
|
||||
default: // 默认 QQ音乐
|
||||
case "qq":
|
||||
ctx.SendChain(qqmusic(ctx.State["regex_matched"].([]string)[2]))
|
||||
default: // 默认聚合点歌
|
||||
ctx.SendChain(longzhu(ctx.State["regex_matched"].([]string)[2]))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// longzhu 聚合平台
|
||||
func longzhu(keyword string) message.Segment {
|
||||
data, _ := web.GetData(fmt.Sprintf(longZhuURL, url.QueryEscape(keyword)))
|
||||
// 假设 data 是包含整个 JSON 数组的字节切片
|
||||
results := gjson.ParseBytes(data).Array()
|
||||
for _, result := range results {
|
||||
if strings.Contains(strings.ToLower(result.Get("title").String()), strings.ToLower(keyword)) {
|
||||
if musicURL := result.Get("full_track").String(); musicURL != "" {
|
||||
return message.Record(musicURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
results = gjson.GetBytes(data, "#.full_track").Array()
|
||||
if len(results) > 0 {
|
||||
if musicURL := results[0].String(); musicURL != "" {
|
||||
return message.Record(musicURL)
|
||||
}
|
||||
}
|
||||
|
||||
return message.Text("点歌失败, 找不到 ", keyword, " 的相关结果")
|
||||
}
|
||||
|
||||
// migu 返回咪咕音乐卡片
|
||||
func migu(keyword string) message.Segment {
|
||||
headers := http.Header{
|
||||
|
||||
@@ -2,6 +2,7 @@ package score
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
@@ -11,7 +12,10 @@ import (
|
||||
var sdb *scoredb
|
||||
|
||||
// scoredb 分数数据库
|
||||
type scoredb gorm.DB
|
||||
type scoredb struct {
|
||||
db *gorm.DB
|
||||
scoremu sync.Mutex
|
||||
}
|
||||
|
||||
// scoretable 分数结构体
|
||||
type scoretable struct {
|
||||
@@ -52,25 +56,31 @@ func initialize(dbpath string) *scoredb {
|
||||
panic(err)
|
||||
}
|
||||
gdb.AutoMigrate(&scoretable{}).AutoMigrate(&signintable{})
|
||||
return (*scoredb)(gdb)
|
||||
return &scoredb{
|
||||
db: gdb,
|
||||
}
|
||||
}
|
||||
|
||||
// Close ...
|
||||
func (sdb *scoredb) Close() error {
|
||||
db := (*gorm.DB)(sdb)
|
||||
db := sdb.db
|
||||
return db.Close()
|
||||
}
|
||||
|
||||
// GetScoreByUID 取得分数
|
||||
func (sdb *scoredb) GetScoreByUID(uid int64) (s scoretable) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
sdb.scoremu.Lock()
|
||||
defer sdb.scoremu.Unlock()
|
||||
db := sdb.db
|
||||
db.Model(&scoretable{}).FirstOrCreate(&s, "uid = ? ", uid)
|
||||
return s
|
||||
}
|
||||
|
||||
// InsertOrUpdateScoreByUID 插入或更新分数
|
||||
func (sdb *scoredb) InsertOrUpdateScoreByUID(uid int64, score int) (err error) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
sdb.scoremu.Lock()
|
||||
defer sdb.scoremu.Unlock()
|
||||
db := sdb.db
|
||||
s := scoretable{
|
||||
UID: uid,
|
||||
Score: score,
|
||||
@@ -91,14 +101,18 @@ func (sdb *scoredb) InsertOrUpdateScoreByUID(uid int64, score int) (err error) {
|
||||
|
||||
// GetSignInByUID 取得签到次数
|
||||
func (sdb *scoredb) GetSignInByUID(uid int64) (si signintable) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
sdb.scoremu.Lock()
|
||||
defer sdb.scoremu.Unlock()
|
||||
db := sdb.db
|
||||
db.Model(&signintable{}).FirstOrCreate(&si, "uid = ? ", uid)
|
||||
return si
|
||||
}
|
||||
|
||||
// InsertOrUpdateSignInCountByUID 插入或更新签到次数
|
||||
func (sdb *scoredb) InsertOrUpdateSignInCountByUID(uid int64, count int) (err error) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
sdb.scoremu.Lock()
|
||||
defer sdb.scoremu.Unlock()
|
||||
db := sdb.db
|
||||
si := signintable{
|
||||
UID: uid,
|
||||
Count: count,
|
||||
@@ -118,7 +132,9 @@ func (sdb *scoredb) InsertOrUpdateSignInCountByUID(uid int64, count int) (err er
|
||||
}
|
||||
|
||||
func (sdb *scoredb) GetScoreRankByTopN(n int) (st []scoretable, err error) {
|
||||
db := (*gorm.DB)(sdb)
|
||||
sdb.scoremu.Lock()
|
||||
defer sdb.scoremu.Unlock()
|
||||
db := sdb.db
|
||||
err = db.Model(&scoretable{}).Order("score desc").Limit(n).Find(&st).Error
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user