Skip to content

Commit bcb007e

Browse files
authored
Release Version 4.7.0
Merge pull request #25 from HIM049/develop
2 parents bac0a8b + bc26b73 commit bcb007e

File tree

11 files changed

+317
-110
lines changed

11 files changed

+317
-110
lines changed

actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (a *App) GetTheme() (string, error) {
2222
return "", err
2323
}
2424

25-
return cfg.Thene, nil
25+
return cfg.Theme, nil
2626
}
2727

2828
// 获取列表中视频数量

app.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ func (a *App) startup(ctx context.Context) {
2525
runtime.LogError(a.ctx, "初始化文件夹失败")
2626

2727
}
28-
_ = os.MkdirAll(cfg.DownloadPath, 0755)
29-
_ = os.MkdirAll(cfg.CachePath, 0755)
30-
_ = os.MkdirAll(cfg.CachePath+"/music", 0755)
31-
_ = os.MkdirAll(cfg.CachePath+"/cover", 0755)
32-
_ = os.MkdirAll(cfg.CachePath+"/single/cover", 0755)
33-
_ = os.MkdirAll(cfg.CachePath+"/single/music", 0755)
28+
29+
downloadPath := cfg.FileConfig.DownloadPath
30+
cachePath := cfg.FileConfig.CachePath
31+
_ = os.MkdirAll(downloadPath, 0755)
32+
_ = os.MkdirAll(cachePath, 0755)
33+
_ = os.MkdirAll(cachePath+"/music", 0755)
34+
_ = os.MkdirAll(cachePath+"/cover", 0755)
35+
_ = os.MkdirAll(cachePath+"/single/cover", 0755)
36+
_ = os.MkdirAll(cachePath+"/single/music", 0755)
3437
}
3538

3639
// 程序关闭时
@@ -39,7 +42,7 @@ func (a *App) shutdown(ctx context.Context) {
3942
cfg := new(Config)
4043
cfg.Get()
4144
if cfg.DeleteCache {
42-
os.RemoveAll(cfg.CachePath)
45+
os.RemoveAll(cfg.FileConfig.CachePath)
4346
}
4447
}
4548

config.go

+153-21
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
package main
22

3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"os"
8+
)
9+
310
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"`
1430
}
1531

1632
type Account struct {
@@ -23,18 +39,117 @@ type Account struct {
2339
Sid string `json:"sid"`
2440
}
2541

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+
26136
// 初始化设置
27137
func (cfg *Config) init() {
28138
*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+
},
38153
Account: Account{
39154
IsLogin: false,
40155
UseAccount: false,
@@ -66,8 +181,25 @@ func (cfg *Config) Get() error {
66181
if err != nil {
67182
return err
68183
}
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+
}
71203
}
72204
}
73205
}

download.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (a *App) ListDownload(listPath string, opt DownloadOption) error {
3030

3131
// _ = os.MkdirAll(path.Join(cfg.DownloadPath, favlistId), 0755)
3232

33-
sem := make(chan struct{}, cfg.DownloadThreads+1)
33+
sem := make(chan struct{}, cfg.DownloadConfig.DownloadThreads+1)
3434
var wg sync.WaitGroup
3535

3636
videoList := new(VideoList)
@@ -42,7 +42,7 @@ func (a *App) ListDownload(listPath string, opt DownloadOption) error {
4242

4343
// 格式判断
4444
audioType := AudioType.m4a
45-
if cfg.ConvertFormat {
45+
if cfg.FileConfig.ConvertFormat {
4646
audioType = AudioType.mp3
4747
}
4848

@@ -60,25 +60,25 @@ func (a *App) ListDownload(listPath string, opt DownloadOption) error {
6060
runtime.EventsEmit(a.ctx, "downloadFinish", v.Meta.SongName)
6161
}()
6262

63-
// 处理音频标题
64-
finalfileName := v.Title
65-
// 如果是分 P (以分 P 命名为主)
66-
if true {
67-
finalfileName += "(" + v.PageTitle + ")"
68-
}
63+
// 处理文件名结构体
64+
fileName := new(FileName)
65+
fileName.Title = v.Title
66+
fileName.Subtitle = v.PageTitle
67+
fileName.ID = num
68+
fileName.Quality = "hires"
6969

7070
//判断是否已下载
71-
finalFile := path.Join(cfg.DownloadPath, v.Title+audioType)
71+
finalFile := path.Join(cfg.FileConfig.DownloadPath, v.Title+audioType)
7272
if IsFileExists(finalFile) {
7373
runtime.LogDebugf(a.ctx, "跳过已下载: %s", finalFile)
7474
return
7575
}
7676

7777
runtime.LogDebugf(a.ctx, "开始下载视频%d", num)
78-
musicPathAndName := cfg.CachePath + "/music/" + strconv.Itoa(v.Cid)
78+
musicPathAndName := cfg.FileConfig.CachePath + "/music/" + strconv.Itoa(v.Cid)
7979

8080
// 下载视频
81-
for i := 0; i < cfg.RetryCount; i++ {
81+
for i := 0; i < cfg.DownloadConfig.RetryCount; i++ {
8282

8383
// 音频下载逻辑
8484
if v.IsAudio {
@@ -118,10 +118,10 @@ func (a *App) ListDownload(listPath string, opt DownloadOption) error {
118118
}
119119

120120
// 判断文件类型并转码
121-
if v.Format == AudioType.m4a && cfg.ConvertFormat {
121+
if v.Format == AudioType.m4a && cfg.FileConfig.ConvertFormat {
122122
runtime.LogDebugf(a.ctx, "(视频%d) 转码为 MP3", num)
123123
v.Format = AudioType.mp3
124-
finalfileName = finalfileName + AudioType.mp3
124+
fileName.Format = AudioType.mp3
125125

126126
// 转码文件
127127
err = ConventFile(musicPathAndName+AudioType.m4a, musicPathAndName+AudioType.mp3)
@@ -132,11 +132,12 @@ func (a *App) ListDownload(listPath string, opt DownloadOption) error {
132132
}
133133
} else {
134134
runtime.LogDebugf(a.ctx, "(视频%d) 不转码", num)
135-
finalfileName = finalfileName + v.Format
135+
fileName.Format = v.Format
136136
}
137137

138138
// 写入元数据
139139
if v.Format != AudioType.flac {
140+
fileName.Quality = "normal"
140141
err = ChangeTag(cfg, &opt, &v)
141142
if err != nil {
142143
runtime.LogErrorf(a.ctx, "(视频%d) 写入元数据时发生错误:%s", num, err)
@@ -146,7 +147,7 @@ func (a *App) ListDownload(listPath string, opt DownloadOption) error {
146147
}
147148

148149
// 输出文件
149-
err = OutputFile(cfg, &v, finalfileName)
150+
err = OutputFile(cfg, &v, *fileName)
150151
if err != nil {
151152
runtime.LogErrorf(a.ctx, "输出文件时发生错误:%s", err)
152153
} else {
@@ -157,7 +158,7 @@ func (a *App) ListDownload(listPath string, opt DownloadOption) error {
157158

158159
go func(v VideoInformation, num int) {
159160
// 下载封面图片
160-
err = bilibili.SaveFromURL(v.Meta.Cover, cfg.CachePath+"/cover/"+strconv.Itoa(v.Cid)+".jpg")
161+
err = bilibili.SaveFromURL(v.Meta.Cover, cfg.FileConfig.CachePath+"/cover/"+strconv.Itoa(v.Cid)+".jpg")
161162
if err != nil {
162163
runtime.LogErrorf(a.ctx, "保存封面时发生错误:%s", err)
163164
} else {

frontend/src/components/collect_download/creat_videolist.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const status = computed({
3737
// 创建视频列表并保存路径
3838
function creatVideoList() {
3939
LoadConfig().then(result => {
40-
parms.value.videoListPath = result.videolist_path;
40+
parms.value.videoListPath = result.file_config.videolist_path;
4141
CreatVideoList();
4242
emit('nextpage');
4343
})

0 commit comments

Comments
 (0)