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