Skip to content

Commit 4604499

Browse files
committed
Merge remote-tracking branch 'origin/pppp' into pppp
2 parents fd11da7 + 0f75562 commit 4604499

File tree

2 files changed

+69
-52
lines changed

2 files changed

+69
-52
lines changed

airecord/recordcfg.go

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,89 @@
22
package airecord
33

44
import (
5-
"encoding/json"
65
"fmt"
76
"os"
87
"strings"
8+
"sync"
9+
"time"
910

10-
"github.com/sirupsen/logrus"
11+
sql "github.com/FloatTech/sqlite"
1112
)
1213

14+
// Storage 语音记录配置存储
15+
type Storage struct {
16+
sync.RWMutex
17+
db sql.Sqlite
18+
}
19+
1320
var (
14-
// RecCfg 语音记录配置
15-
RecCfg recordconfig
16-
configPath = "data/airecord/recordconfig.json" // 配置文件路径
21+
sdb = &Storage{
22+
db: sql.New("data/airecord/recordconfig.db"),
23+
}
1724
)
1825

1926
func init() {
20-
if err := loadConfig(); err != nil {
21-
logrus.Warnln("[airecord] WARN: 加载配置文件失败,使用默认配置:", err)
22-
} else {
23-
logrus.Infoln("[airecord] 成功从文件加载语音记录配置")
27+
if err := os.MkdirAll("data/airecord", 0755); err != nil {
28+
panic(err)
29+
}
30+
if err := sdb.db.Open(time.Hour * 24); err != nil {
31+
panic(err)
32+
}
33+
if err := sdb.db.Create("config", &recordconfig{}); err != nil {
34+
panic(err)
2435
}
2536
}
2637

2738
// recordconfig 存储语音记录相关配置
2839
type recordconfig struct {
29-
ModelName string `json:"modelName"` // 语音模型名称
30-
ModelID string `json:"modelID"` // 语音模型ID
31-
Customgid int64 `json:"customgid"` // 自定义群ID
40+
ID int64 `db:"id"` // 主键ID
41+
ModelName string `db:"modelName"` // 语音模型名称
42+
ModelID string `db:"modelID"` // 语音模型ID
43+
Customgid int64 `db:"customgid"` // 自定义群ID
44+
}
45+
46+
// GetConfig 获取当前配置
47+
func GetConfig() recordconfig {
48+
sdb.RLock()
49+
defer sdb.RUnlock()
50+
cfg := recordconfig{}
51+
_ = sdb.db.Find("config", &cfg, "WHERE id = 1")
52+
return cfg
3253
}
3354

3455
// SetRecordModel 设置语音记录模型
3556
func SetRecordModel(modelName, modelID string) error {
36-
RecCfg.ModelName = modelName
37-
RecCfg.ModelID = modelID
38-
return saveConfig() // 保存配置
57+
cfg := GetConfig()
58+
sdb.Lock()
59+
defer sdb.Unlock()
60+
return sdb.db.Insert("config", &recordconfig{
61+
ID: 1,
62+
ModelName: modelName,
63+
ModelID: modelID,
64+
Customgid: cfg.Customgid,
65+
})
3966
}
4067

4168
// SetCustomGID 设置自定义群ID
4269
func SetCustomGID(gid int64) error {
43-
RecCfg.Customgid = gid
44-
return saveConfig() // 保存配置
70+
cfg := GetConfig()
71+
sdb.Lock()
72+
defer sdb.Unlock()
73+
return sdb.db.Insert("config", &recordconfig{
74+
ID: 1,
75+
ModelName: cfg.ModelName,
76+
ModelID: cfg.ModelID,
77+
Customgid: gid,
78+
})
4579
}
4680

4781
// PrintRecordConfig 生成格式化的语音记录配置信息字符串
48-
func PrintRecordConfig(recCfg recordconfig) string {
82+
func PrintRecordConfig() string {
83+
cfg := GetConfig()
4984
var builder strings.Builder
5085
builder.WriteString("当前语音记录配置:\n")
51-
builder.WriteString(fmt.Sprintf("• 语音模型名称:%s\n", recCfg.ModelName))
52-
builder.WriteString(fmt.Sprintf("• 语音模型ID:%s\n", recCfg.ModelID))
53-
builder.WriteString(fmt.Sprintf("• 自定义群ID:%d\n", recCfg.Customgid))
86+
builder.WriteString(fmt.Sprintf("• 语音模型名称:%s\n", cfg.ModelName))
87+
builder.WriteString(fmt.Sprintf("• 语音模型ID:%s\n", cfg.ModelID))
88+
builder.WriteString(fmt.Sprintf("• 自定义群ID:%d\n", cfg.Customgid))
5489
return builder.String()
5590
}
56-
57-
// saveConfig 将配置保存到JSON文件
58-
func saveConfig() error {
59-
data, err := json.MarshalIndent(RecCfg, "", " ")
60-
if err != nil {
61-
logrus.Warnln("ERROR: 序列化配置失败:", err)
62-
return err
63-
}
64-
err = os.WriteFile(configPath, data, 0644)
65-
if err != nil {
66-
logrus.Warnln("ERROR: 写入配置文件失败:", err)
67-
return err
68-
}
69-
return nil
70-
}
71-
72-
// loadConfig 从JSON文件加载配置
73-
func loadConfig() error {
74-
data, err := os.ReadFile(configPath)
75-
if err != nil {
76-
return err
77-
}
78-
err = json.Unmarshal(data, &RecCfg)
79-
if err != nil {
80-
logrus.Warnln("ERROR: 解析配置文件失败:", err)
81-
return err
82-
}
83-
return nil
84-
}

bilibili/api.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616
// ErrAPINeedCookie ...
1717
var ErrAPINeedCookie = errors.New("api need cookie")
1818

19+
// Ua 默认UA
20+
var Ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
21+
1922
// SearchUser 查找b站用户
2023
func SearchUser(cookiecfg *CookieConfig, keyword string) (r []SearchResult, err error) {
2124
client := &http.Client{}
@@ -166,9 +169,17 @@ func GetArticleInfo(id string) (card Card, err error) {
166169
}
167170

168171
// GetLiveRoomInfo 用直播间id查直播间信息
169-
func GetLiveRoomInfo(roomID string) (card RoomCard, err error) {
172+
func GetLiveRoomInfo(roomID string, cookie string) (card RoomCard, err error) {
170173
var data []byte
171-
data, err = web.GetData(fmt.Sprintf(ArticleInfoURL, roomID))
174+
if cookie != "" {
175+
data, err = web.RequestDataWithHeaders(web.NewDefaultClient(), fmt.Sprintf(LiveRoomInfoURL, roomID), "GET", func(req *http.Request) error {
176+
req.Header.Add("cookie", cookie)
177+
req.Header.Set("User-Agent", Ua)
178+
return nil
179+
}, nil)
180+
} else {
181+
data, err = web.GetData(fmt.Sprintf(LiveRoomInfoURL, roomID))
182+
}
172183
if err != nil {
173184
return
174185
}

0 commit comments

Comments
 (0)