-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
221 lines (200 loc) · 5.19 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"encoding/json"
"fmt"
"io"
"os"
)
type Config struct {
ConfigVersion int `json:"config_version"`
DeleteCache bool `json:"delete_cache"`
Theme string `json:"theme"`
DownloadConfig DownloadConfig `json:"download_config"`
FileConfig FileConfig `json:"file_config"`
Account Account
}
type DownloadConfig struct {
DownloadThreads int `json:"download_threads"`
RetryCount int `json:"retry_count"`
}
type FileConfig struct {
ConvertFormat bool `json:"convert_format"`
FileNameTemplate string `json:"file_name_template"`
DownloadPath string `json:"download_path"`
CachePath string `json:"cache_path"`
VideoListPath string `json:"videolist_path"`
}
type Account struct {
IsLogin bool `json:"is_login"`
UseAccount bool `json:"use_account"`
SESSDATA string `json:"sessdata"`
Bili_jct string `json:"bili_jct"`
DedeUserID string `json:"dede_user_id"`
DedeUserID__ckMd5 string `json:"dede_user_id__ck_md5"`
Sid string `json:"sid"`
}
func UpdateConfig(filePath string) error {
// 打开 JSON 文件
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open config file: %w", err)
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("failed to read config file: %w", err)
}
// 将 JSON 文件反序列化为 map
var configMap map[string]interface{}
err = json.Unmarshal(data, &configMap)
if err != nil {
return fmt.Errorf("failed to parse config file: %w", err)
}
config := new(Config)
config.init()
// 匹配结构体字段
config.ConfigVersion = CONFIG_VERSION
if v, ok := configMap["delete_cache"].(bool); ok {
config.DeleteCache = v
}
if v, ok := configMap["theme"].(string); ok {
config.Theme = v
}
if downloadConfig, ok := configMap["download_config"].(map[string]interface{}); ok {
if v, ok := downloadConfig["download_threads"].(float64); ok {
config.DownloadConfig.DownloadThreads = int(v)
}
if v, ok := downloadConfig["retry_count"].(float64); ok {
config.DownloadConfig.RetryCount = int(v)
}
}
if fileConfig, ok := configMap["file_config"].(map[string]interface{}); ok {
if v, ok := fileConfig["convert_format"].(bool); ok {
config.FileConfig.ConvertFormat = v
}
if v, ok := fileConfig["file_name_template"].(string); ok {
config.FileConfig.FileNameTemplate = v
}
if v, ok := fileConfig["download_path"].(string); ok {
config.FileConfig.DownloadPath = v
}
if v, ok := fileConfig["cache_path"].(string); ok {
config.FileConfig.CachePath = v
}
if v, ok := fileConfig["videolist_path"].(string); ok {
config.FileConfig.VideoListPath = v
}
}
if account, ok := configMap["account"].(map[string]interface{}); ok {
if v, ok := account["is_login"].(bool); ok {
config.Account.IsLogin = v
}
if v, ok := account["use_account"].(bool); ok {
config.Account.UseAccount = v
}
if v, ok := account["sessdata"].(string); ok {
config.Account.SESSDATA = v
}
if v, ok := account["bili_jct"].(string); ok {
config.Account.Bili_jct = v
}
if v, ok := account["dede_user_id"].(string); ok {
config.Account.DedeUserID = v
}
if v, ok := account["dede_user_id__ck_md5"].(string); ok {
config.Account.DedeUserID__ckMd5 = v
}
if v, ok := account["sid"].(string); ok {
config.Account.Sid = v
}
}
// 保存设置
err = config.Save()
if err != nil {
return err
}
return nil
}
// 初始化设置
func (cfg *Config) init() {
*cfg = Config{
ConfigVersion: CONFIG_VERSION,
DeleteCache: true,
Theme: "lightPink",
DownloadConfig: DownloadConfig{
DownloadThreads: 5,
RetryCount: 10,
},
FileConfig: FileConfig{
ConvertFormat: Checkffmpeg(),
FileNameTemplate: "{{.ID}}_{{.Title}}({{.Subtitle}})_{{.Quality}}.{{.Format}}",
DownloadPath: "./Download",
CachePath: "./Cache",
VideoListPath: "./Cache/video_list.json",
},
Account: Account{
IsLogin: false,
UseAccount: false,
SESSDATA: "",
Bili_jct: "",
DedeUserID: "",
DedeUserID__ckMd5: "",
Sid: "",
},
}
}
// 读取设置文件
func (cfg *Config) Get() error {
for {
// 判断设置文件是否已经存在
if !IsFileExists("./config.json") {
// 文件不存在
file := new(Config)
file.init()
err := file.Save()
if err != nil {
return err
}
} else {
// 文件已存在
var file Config
err := LoadJsonFile("./config.json", &file)
if err != nil {
return err
}
if file.ConfigVersion == CONFIG_VERSION {
*cfg = file
return nil
}
if file.ConfigVersion < CONFIG_VERSION {
err := UpdateConfig("./config.json")
if err != nil {
return err
} else {
continue
}
}
if file.ConfigVersion > CONFIG_VERSION {
cfg.init()
err := cfg.Save()
if err != nil {
return err
}
}
}
}
}
func (cfg *Config) Check() {
if cfg.ConfigVersion != CONFIG_VERSION {
cfg.ConfigVersion = CONFIG_VERSION
cfg.Save()
}
}
// 保存设置到文件
func (cfg *Config) Save() error {
err := SaveJsonFile("./config.json", cfg)
if err != nil {
return err
}
return nil
}