Skip to content

Commit 5f8f4b1

Browse files
committed
♻️ 重构 AppConfigLoader.java
1 parent c9357ff commit 5f8f4b1

17 files changed

+121
-120
lines changed

src/main/java/com/pcdd/sonovel/Main.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
import com.openhtmltopdf.util.XRLog;
88
import com.pcdd.sonovel.action.CheckUpdateAction;
99
import com.pcdd.sonovel.context.HttpClientContext;
10+
import com.pcdd.sonovel.core.AppConfigLoader;
1011
import com.pcdd.sonovel.core.OkHttpClientFactory;
1112
import com.pcdd.sonovel.launch.CliLauncher;
1213
import com.pcdd.sonovel.launch.TuiLauncher;
14+
import com.pcdd.sonovel.model.AppConfig;
1315
import com.pcdd.sonovel.repository.ClientReportRepository;
14-
import com.pcdd.sonovel.util.ConfigUtils;
1516
import com.pcdd.sonovel.util.EnvUtils;
1617
import com.pcdd.sonovel.web.WebServer;
1718
import picocli.CommandLine;
@@ -32,6 +33,8 @@
3233
*/
3334
public class Main {
3435

36+
private static final AppConfig APP_CONFIG = AppConfigLoader.APP_CONFIG;
37+
3538
static {
3639
if (EnvUtils.isDev()) {
3740
Console.log(render("当前为开发环境!", "red"));
@@ -53,19 +56,19 @@ public class Main {
5356
}
5457

5558
public static void main(String[] args) {
56-
HttpClientContext.set(OkHttpClientFactory.create(ConfigUtils.defaultConfig()));
59+
HttpClientContext.set(OkHttpClientFactory.create(APP_CONFIG));
5760

5861
new Thread(ClientReportRepository::report).start();
59-
if (ConfigUtils.defaultConfig().getAutoUpdate() == 1) {
62+
if (APP_CONFIG.getAutoUpdate() == 1) {
6063
new CheckUpdateAction(5000).execute();
6164
}
6265

6366
String mode = System.getProperty("mode", "tui");
6467

65-
if ("web".equalsIgnoreCase(mode) || ConfigUtils.defaultConfig().getWebEnabled() == 1) {
68+
if ("web".equalsIgnoreCase(mode) || APP_CONFIG.getWebEnabled() == 1) {
6669
new WebServer().start();
6770
} else if (args.length == 0 && "tui".equalsIgnoreCase(mode)) {
68-
TuiLauncher.launch(ConfigUtils.defaultConfig());
71+
TuiLauncher.launch(APP_CONFIG);
6972
} else if (args.length > 0 || "cli".equalsIgnoreCase(mode)) {
7073
new CommandLine(new CliLauncher()).execute(ArrayUtil.isEmpty(args) ? new String[]{"-h"} : args);
7174
System.exit(0);

src/main/java/com/pcdd/sonovel/action/BatchDownloadAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
public class BatchDownloadAction {
2727

2828
private final AppConfig config;
29-
public static final String DIVIDER = "=".repeat(50);
29+
private static final String DIVIDER = "=".repeat(50);
3030

3131
public void execute() {
3232
Scanner sc = Console.scanner();

src/main/java/com/pcdd/sonovel/action/CheckUpdateAction.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import cn.hutool.setting.dialect.Props;
1515
import cn.hutool.system.OsInfo;
1616
import cn.hutool.system.SystemUtil;
17-
import com.pcdd.sonovel.util.ConfigUtils;
17+
import com.pcdd.sonovel.core.AppConfigLoader;
18+
import com.pcdd.sonovel.model.AppConfig;
1819
import com.pcdd.sonovel.util.FileUtils;
1920
import com.pcdd.sonovel.util.RandomUA;
2021
import me.tongfei.progressbar.ProgressBar;
@@ -30,9 +31,9 @@
3031
*/
3132
public class CheckUpdateAction {
3233

34+
private static final AppConfig APP_CONFIG = AppConfigLoader.APP_CONFIG;
3335
private static final String RELEASE_URL = "https://api.github.com/repos/freeok/so-novel/releases";
3436
private static final String ASSETS_URL = "https://github.com/freeok/so-novel/releases/download/{}/sonovel-{}.tar.gz";
35-
private final String GH_PROXY = ConfigUtils.defaultConfig().getGhProxy();
3637
private final int timeoutMills;
3738

3839
public CheckUpdateAction() {
@@ -51,7 +52,7 @@ public void execute() {
5152
.header(Header.USER_AGENT, RandomUA.generate())
5253
.execute()) {
5354

54-
Props sys = ConfigUtils.sys();
55+
Props sys = AppConfigLoader.sys();
5556
String jsonStr = resp.body();
5657
JSONArray arr = JSONUtil.parseArray(jsonStr);
5758
JSONObject latest = JSONUtil.parseObj(arr.getFirst());
@@ -87,7 +88,7 @@ private String getDownloadUrl(String version) {
8788
fileName = "linux";
8889
}
8990

90-
return GH_PROXY + StrUtil.format(ASSETS_URL, version, fileName);
91+
return APP_CONFIG.getGhProxy() + StrUtil.format(ASSETS_URL, version, fileName);
9192
}
9293

9394
private void download(String url) {

src/main/java/com/pcdd/sonovel/action/DownloadAction.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import cn.hutool.core.collection.CollUtil;
44
import cn.hutool.core.lang.Console;
55
import cn.hutool.core.util.StrUtil;
6+
import com.pcdd.sonovel.core.AppConfigLoader;
67
import com.pcdd.sonovel.core.Crawler;
78
import com.pcdd.sonovel.model.AppConfig;
89
import com.pcdd.sonovel.model.Chapter;
910
import com.pcdd.sonovel.model.SearchResult;
1011
import com.pcdd.sonovel.parse.TocParser;
11-
import com.pcdd.sonovel.util.ConfigUtils;
1212

1313
import java.util.List;
1414
import java.util.Scanner;
@@ -22,7 +22,7 @@
2222
public class DownloadAction {
2323

2424
private static final String GREEN = "green";
25-
private final AppConfig config = ConfigUtils.defaultConfig();
25+
private static final AppConfig APP_CONFIG = AppConfigLoader.APP_CONFIG;
2626
private final Scanner sc = Console.scanner();
2727

2828
public void execute(List<SearchResult> results) {
@@ -51,9 +51,9 @@ public void execute(List<SearchResult> results) {
5151
if (num < 0 || num > results.size()) continue;
5252

5353
sr = results.get(num - 1);
54-
config.setSourceId(sr.getSourceId());
54+
APP_CONFIG.setSourceId(sr.getSourceId());
5555
Console.log("<== 正在获取章节目录...");
56-
TocParser tocParser = new TocParser(config);
56+
TocParser tocParser = new TocParser(APP_CONFIG);
5757
toc = tocParser.parse(sr.getUrl(), 1, Integer.MAX_VALUE);
5858

5959
Console.log("<== 你选择了《{}》({}),共计 {} 章,书源 {}: {}", sr.getBookName(), sr.getAuthor(), toc.size(), sr.getSourceId(), sr.getUrl());
@@ -98,7 +98,7 @@ public void execute(List<SearchResult> results) {
9898
}
9999
if (action == 4) continue;
100100

101-
new Crawler(config).crawl(sr.getUrl(), toc);
101+
new Crawler(APP_CONFIG).crawl(sr.getUrl(), toc);
102102
}
103103
}
104104

src/main/java/com/pcdd/sonovel/action/ShowSourcesAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import cn.hutool.core.lang.Console;
44
import cn.hutool.core.lang.ConsoleTable;
5+
import com.pcdd.sonovel.core.AppConfigLoader;
56
import com.pcdd.sonovel.core.OkHttpClientFactory;
67
import com.pcdd.sonovel.model.Rule;
78
import com.pcdd.sonovel.model.SourceInfo;
8-
import com.pcdd.sonovel.util.ConfigUtils;
99
import com.pcdd.sonovel.util.EnvUtils;
1010
import com.pcdd.sonovel.util.RandomUA;
1111
import com.pcdd.sonovel.util.SourceUtils;
@@ -55,7 +55,7 @@ private static List<SourceInfo> testWebsiteDelays(List<Rule> rules) {
5555
List<SourceInfo> res = new ArrayList<>();
5656
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
5757
CompletionService<SourceInfo> completionService = new ExecutorCompletionService<>(executor);
58-
OkHttpClient client = OkHttpClientFactory.create(ConfigUtils.defaultConfig());
58+
OkHttpClient client = OkHttpClientFactory.create(AppConfigLoader.APP_CONFIG);
5959

6060
for (Rule r : rules) {
6161
completionService.submit(() -> {

src/main/java/com/pcdd/sonovel/util/ConfigUtils.java renamed to src/main/java/com/pcdd/sonovel/core/AppConfigLoader.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package com.pcdd.sonovel.util;
1+
package com.pcdd.sonovel.core;
22

33
import cn.hutool.core.io.FileUtil;
44
import cn.hutool.core.util.StrUtil;
55
import cn.hutool.setting.Setting;
66
import cn.hutool.setting.dialect.Props;
77
import com.pcdd.sonovel.model.AppConfig;
8+
import com.pcdd.sonovel.util.EnvUtils;
9+
import com.pcdd.sonovel.util.LangUtil;
810
import lombok.experimental.UtilityClass;
911

1012
import java.io.File;
@@ -17,7 +19,7 @@
1719
* Created at 2024/3/23
1820
*/
1921
@UtilityClass
20-
public class ConfigUtils {
22+
public class AppConfigLoader {
2123

2224
private final String SELECTION_GLOBAL = "global";
2325
private final String SELECTION_DOWNLOAD = "download";
@@ -26,6 +28,7 @@ public class ConfigUtils {
2628
private final String SELECTION_WEB = "web";
2729
private final String SELECTION_COOKIE = "cookie";
2830
private final String SELECTION_PROXY = "proxy";
31+
public final AppConfig APP_CONFIG = loadConfig();
2932

3033
/**
3134
* 加载应用属性
@@ -54,49 +57,49 @@ public Setting usr() {
5457
return new Setting(absolutePath.toString());
5558
}
5659

57-
public AppConfig defaultConfig() {
58-
AppConfig config = new AppConfig();
59-
config.setVersion(sys().getStr("version"));
60+
public AppConfig loadConfig() {
61+
AppConfig cfg = new AppConfig();
62+
cfg.setVersion(sys().getStr("version"));
6063
Setting usr = usr();
6164

6265
// [global]
63-
config.setAutoUpdate(usr.getInt("auto-update", SELECTION_GLOBAL, 0));
64-
config.setGhProxy(usr.getStr("gh-proxy", SELECTION_GLOBAL, ""));
66+
cfg.setAutoUpdate(usr.getInt("auto-update", SELECTION_GLOBAL, 0));
67+
cfg.setGhProxy(usr.getStr("gh-proxy", SELECTION_GLOBAL, ""));
6568

6669
// [download]
67-
config.setDownloadPath(getStrOrDefault(usr, "download-path", SELECTION_DOWNLOAD, "downloads"));
68-
config.setExtName(getStrOrDefault(usr, "extname", SELECTION_DOWNLOAD, "epub").toLowerCase());
69-
config.setTxtEncoding(getStrOrDefault(usr, "txt-encoding", SELECTION_DOWNLOAD, "UTF-8"));
70-
config.setPreserveChapterCache(usr.getInt("preserve-chapter-cache", SELECTION_DOWNLOAD, 0));
70+
cfg.setDownloadPath(getStrOrDefault(usr, "download-path", SELECTION_DOWNLOAD, "downloads"));
71+
cfg.setExtName(getStrOrDefault(usr, "extname", SELECTION_DOWNLOAD, "epub").toLowerCase());
72+
cfg.setTxtEncoding(getStrOrDefault(usr, "txt-encoding", SELECTION_DOWNLOAD, "UTF-8"));
73+
cfg.setPreserveChapterCache(usr.getInt("preserve-chapter-cache", SELECTION_DOWNLOAD, 0));
7174

7275
// [source]
73-
config.setLanguage(getStrOrDefault(usr, "language", SELECTION_SOURCE, LangUtil.getCurrentLang()));
74-
config.setActiveRules(getStrOrDefault(usr, "active-rules", SELECTION_SOURCE, "main-rules.json"));
75-
config.setSourceId(usr.getInt("source-id", SELECTION_SOURCE, -1));
76-
config.setSearchLimit(usr.getInt("search-limit", SELECTION_SOURCE, -1));
76+
cfg.setLanguage(getStrOrDefault(usr, "language", SELECTION_SOURCE, LangUtil.getCurrentLang()));
77+
cfg.setActiveRules(getStrOrDefault(usr, "active-rules", SELECTION_SOURCE, "main-rules.json"));
78+
cfg.setSourceId(usr.getInt("source-id", SELECTION_SOURCE, -1));
79+
cfg.setSearchLimit(usr.getInt("search-limit", SELECTION_SOURCE, -1));
7780

7881
// [crawl]
79-
config.setThreads(usr.getInt("threads", SELECTION_CRAWL, -1));
80-
config.setMinInterval(usr.getInt("min-interval", SELECTION_CRAWL, 200));
81-
config.setMaxInterval(usr.getInt("max-interval", SELECTION_CRAWL, 400));
82-
config.setEnableRetry(usr.getInt("enable-retry", SELECTION_CRAWL, 1));
83-
config.setMaxRetries(usr.getInt("max-retries", SELECTION_CRAWL, 5));
84-
config.setRetryMinInterval(usr.getInt("retry-min-interval", SELECTION_CRAWL, 2000));
85-
config.setRetryMaxInterval(usr.getInt("retry-max-interval", SELECTION_CRAWL, 4000));
82+
cfg.setThreads(usr.getInt("threads", SELECTION_CRAWL, -1));
83+
cfg.setMinInterval(usr.getInt("min-interval", SELECTION_CRAWL, 200));
84+
cfg.setMaxInterval(usr.getInt("max-interval", SELECTION_CRAWL, 400));
85+
cfg.setEnableRetry(usr.getInt("enable-retry", SELECTION_CRAWL, 1));
86+
cfg.setMaxRetries(usr.getInt("max-retries", SELECTION_CRAWL, 5));
87+
cfg.setRetryMinInterval(usr.getInt("retry-min-interval", SELECTION_CRAWL, 2000));
88+
cfg.setRetryMaxInterval(usr.getInt("retry-max-interval", SELECTION_CRAWL, 4000));
8689

8790
// [web]
88-
config.setWebEnabled(usr.getInt("enabled", SELECTION_WEB, 0));
89-
config.setWebPort(usr.getInt("port", SELECTION_WEB, 7765));
91+
cfg.setWebEnabled(usr.getInt("enabled", SELECTION_WEB, 0));
92+
cfg.setWebPort(usr.getInt("port", SELECTION_WEB, 7765));
9093

9194
// [cookie]
92-
config.setQidianCookie(usr.getStr("qidian", SELECTION_COOKIE, ""));
95+
cfg.setQidianCookie(usr.getStr("qidian", SELECTION_COOKIE, ""));
9396

9497
// [proxy]
95-
config.setProxyEnabled(usr.getInt("enabled", SELECTION_PROXY, 0));
96-
config.setProxyHost(getStrOrDefault(usr, "host", SELECTION_PROXY, "127.0.0.1"));
97-
config.setProxyPort(usr.getInt("port", SELECTION_PROXY, 7890));
98+
cfg.setProxyEnabled(usr.getInt("enabled", SELECTION_PROXY, 0));
99+
cfg.setProxyHost(getStrOrDefault(usr, "host", SELECTION_PROXY, "127.0.0.1"));
100+
cfg.setProxyPort(usr.getInt("port", SELECTION_PROXY, 7890));
98101

99-
return config;
102+
return cfg;
100103
}
101104

102105
// 修复 hutool 空串不能触发默认值的 bug

src/main/java/com/pcdd/sonovel/core/CoverUpdater.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.hankcs.hanlp.HanLP;
1414
import com.pcdd.sonovel.model.AppConfig;
1515
import com.pcdd.sonovel.model.Book;
16-
import com.pcdd.sonovel.util.ConfigUtils;
1716
import com.pcdd.sonovel.util.RandomUA;
1817
import lombok.experimental.UtilityClass;
1918
import org.jsoup.Jsoup;
@@ -35,8 +34,8 @@
3534
@UtilityClass
3635
public class CoverUpdater {
3736

38-
private final AppConfig config = ConfigUtils.defaultConfig();
39-
private static final String DEFAULT_COVER = "https://bookcover.yuewen.com/qdbimg/no-cover";
37+
private final AppConfig APP_CONFIG = AppConfigLoader.APP_CONFIG;
38+
private final String DEFAULT_COVER = "https://bookcover.yuewen.com/qdbimg/no-cover";
4039

4140
/**
4241
* 依次尝试不同来源获取封面
@@ -63,12 +62,12 @@ public String fetchCover(Book book, String coverUrl) {
6362
* 起点中文网
6463
*/
6564
public String fetchQidian(Book book) {
66-
if (StrUtil.isEmpty(config.getQidianCookie())) return "";
65+
if (StrUtil.isEmpty(APP_CONFIG.getQidianCookie())) return "";
6766
String url = StrUtil.format("https://www.qidian.com/so/{}.html", book.getBookName());
6867
try (HttpResponse resp = HttpRequest.get(url).
6968
headerMap(Map.of(
7069
Header.USER_AGENT.getValue(), RandomUA.generate(),
71-
Header.COOKIE.getValue(), config.getQidianCookie()
70+
Header.COOKIE.getValue(), APP_CONFIG.getQidianCookie()
7271
), true).execute()) {
7372
Document document = Jsoup.parse(resp.body());
7473

src/main/java/com/pcdd/sonovel/core/Source.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import cn.hutool.core.lang.Opt;
44
import com.pcdd.sonovel.model.AppConfig;
55
import com.pcdd.sonovel.model.Rule;
6-
import com.pcdd.sonovel.util.ConfigUtils;
76
import com.pcdd.sonovel.util.SourceUtils;
87

98
/**
@@ -27,7 +26,7 @@ public Source(int sourceId) {
2726
public Source(Rule rule, AppConfig config) {
2827
this.rule = rule;
2928
// 使用配置文件中的配置,若为空则使用默认配置
30-
this.config = Opt.ofNullable(config).orElse(ConfigUtils.defaultConfig());
29+
this.config = Opt.ofNullable(config).orElse(AppConfigLoader.APP_CONFIG);
3130

3231
// 规则爬取配置覆盖默认配置
3332
Rule.Crawl crawl = rule.getCrawl();

src/main/java/com/pcdd/sonovel/launch/CliLauncher.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.pcdd.sonovel.launch;
22

33
import cn.hutool.core.lang.Console;
4+
import com.pcdd.sonovel.core.AppConfigLoader;
45
import com.pcdd.sonovel.core.Crawler;
56
import com.pcdd.sonovel.model.AppConfig;
67
import com.pcdd.sonovel.model.Book;
78
import com.pcdd.sonovel.model.Rule;
89
import com.pcdd.sonovel.model.SearchResult;
910
import com.pcdd.sonovel.parse.BookParser;
10-
import com.pcdd.sonovel.util.ConfigUtils;
1111
import com.pcdd.sonovel.util.SourceUtils;
1212
import picocli.CommandLine;
1313

@@ -31,7 +31,7 @@ public class CliLauncher implements Runnable {
3131
@CommandLine.Option(names = {"--ext", "-e"}, description = "下载格式,可选 txt|epub|html|pdf,默认 epub", defaultValue = "epub")
3232
String extName;
3333

34-
private static final AppConfig config = ConfigUtils.defaultConfig();
34+
private static final AppConfig APP_CONFIG = AppConfigLoader.APP_CONFIG;
3535

3636
/**
3737
* 全本下载
@@ -43,10 +43,10 @@ public void run() {
4343
下载格式: {}""", bookUrl, extName);
4444

4545
Rule rule = SourceUtils.getSource(bookUrl);
46-
config.setSourceId(rule.getId());
47-
config.setExtName(extName);
46+
APP_CONFIG.setSourceId(rule.getId());
47+
APP_CONFIG.setExtName(extName);
4848

49-
Book book = new BookParser(config).parse(bookUrl);
49+
Book book = new BookParser(APP_CONFIG).parse(bookUrl);
5050
SearchResult sr = SearchResult.builder()
5151
.url(book.getUrl())
5252
.bookName(book.getBookName())
@@ -55,13 +55,13 @@ public void run() {
5555
.lastUpdateTime(book.getLastUpdateTime())
5656
.build();
5757
Console.log("<== {}》({}),正在解析目录...", sr.getBookName(), sr.getAuthor());
58-
new Crawler(config).crawl(sr.getUrl());
58+
new Crawler(APP_CONFIG).crawl(sr.getUrl());
5959
}
6060

6161
static class VersionProvider implements CommandLine.IVersionProvider {
6262
@Override
6363
public String[] getVersion() {
64-
return new String[]{render(config.getVersion(), "green")};
64+
return new String[]{render(APP_CONFIG.getVersion(), "green")};
6565
}
6666
}
6767

0 commit comments

Comments
 (0)