23
23
@ SuppressWarnings ("ResultOfMethodCallIgnored" )
24
24
public class Main {
25
25
26
- private static boolean debug ;
26
+ private static boolean debug , hint , isFileInput ;
27
27
private static Scanner scanner ;
28
28
private static File config ;
29
29
private static long beginTime ;
@@ -54,9 +54,13 @@ private static void setScanner() throws FileNotFoundException {
54
54
File input = new File ("Input.txt" );
55
55
if (input .exists () && input .length () > 0 ) {
56
56
scanner = new Scanner (input );
57
+ isFileInput = true ;
58
+ hint = debug ;
57
59
System .out .println ("检测到 Input.txt,已切换输入源\n " );
58
60
} else {
59
61
scanner = new Scanner (System .in );
62
+ isFileInput = false ;
63
+ hint = true ;
60
64
}
61
65
}
62
66
@@ -66,19 +70,21 @@ private static String inputStr() {
66
70
System .out .println ("\n 程序运行结束;总运行时间:" + getFormattedTime (System .currentTimeMillis () - beginTime ));
67
71
System .exit (0 );
68
72
}
73
+ if (debug && hint && isFileInput ) System .out .println (input );
69
74
return input ;
70
75
}
71
76
72
77
private static int inputInt () {
73
78
String input = scanner .next ();
74
79
if (input .equalsIgnoreCase ("*exit" )) {
75
- Thread . currentThread (). interrupt ( );
80
+ System . exit ( 0 );
76
81
}
82
+ if (debug && hint && isFileInput ) System .out .println (input );
77
83
return Integer .parseInt (input );
78
84
}
79
85
80
86
private static String getNumber () {
81
- System .out .println ("请输入一个 AV 号或 BV 号:" );
87
+ if ( hint ) System .out .println ("请输入一个 AV 号或 BV 号:" );
82
88
return inputStr ();
83
89
}
84
90
@@ -102,7 +108,7 @@ private static String login() throws IOException {
102
108
}
103
109
}
104
110
while (!loginSuccess ) {
105
- System .out .println ("\n 请输入 Cookie 中 SESSDATA 的值(若无请填“#”):" );
111
+ if ( hint ) System .out .println ("\n 请输入 Cookie 中 SESSDATA 的值(若无请填“#”):" );
106
112
sessData = inputStr ();
107
113
if (sessData .equals ("#" )) {
108
114
cookie = "#" ;
@@ -114,7 +120,7 @@ private static String login() throws IOException {
114
120
if (login .getJSONObject ("data" ).getBoolean ("isLogin" )) {
115
121
loginSuccess = true ;
116
122
System .out .println ("登录成功\n ID:" + login .getJSONObject ("data" ).getString ("uname" ) + "\n UID:" + login .getJSONObject ("data" ).getIntValue ("mid" ));
117
- System .out .println ("请决定是否保存该 SESSDATA(输入“Y”或“N”):" );
123
+ if ( hint ) System .out .println ("请决定是否保存该 SESSDATA(输入“Y”或“N”):" );
118
124
if (inputStr ().equalsIgnoreCase ("Y" )) {
119
125
if (!config .exists ()) config .createNewFile ();
120
126
ConfigManager .init (config );
@@ -123,7 +129,7 @@ private static String login() throws IOException {
123
129
map = new LinkedHashMap <>();
124
130
map .put ("sess-data" , sessData );
125
131
ConfigManager .dump (map );
126
- System .out .println ("已保存 SESSDATA" );
132
+ if ( hint ) System .out .println ("已保存 SESSDATA" );
127
133
}
128
134
} else {
129
135
System .out .println ("登录失败" );
@@ -137,11 +143,11 @@ private static String login() throws IOException {
137
143
}
138
144
139
145
private static JSONObject getVideoInfo (String id , String cookie ) throws IOException {
140
- System .out .println (" \n 正在获取稿件信息 ······" );
146
+ System .out .println (( hint ? " \n " : "" ) + "正在获取稿件信息 ······" );
141
147
JSONObject info = readJsonFromUrl ("http://api.bilibili.com/x/web-interface/view?" + (id .toLowerCase ().startsWith ("av" ) ? "aid=" + id .substring (2 ) : "bvid=" + id ), cookie );
142
148
if (info .getIntValue ("code" ) != 0 ) {
143
149
System .out .println (info .getString ("message" ));
144
- System .out .println ("\n 程序运行结束;总运行时间:" + getFormattedTime (System .currentTimeMillis () - beginTime ));
150
+ System .out .println ("\n 程序运行结束,错误代码:" + info . getIntValue ( "code" ) + " ;总运行时间:" + getFormattedTime (System .currentTimeMillis () - beginTime ));
145
151
System .exit (info .getIntValue ("code" ));
146
152
} else {
147
153
info = info .getJSONObject ("data" );
@@ -166,7 +172,7 @@ private static Object[] specify(JSONObject info) {
166
172
for (int i = 0 ; i < pages .size (); i ++) {
167
173
System .out .println (String .format ("%3d" , (i + 1 )) + ". P" + String .format ("%-5d" , pages .getJSONObject (i ).getIntValue ("page" )) + "CID:" + pages .getJSONObject (i ).getIntValue ("cid" ) + " 时长:" + getFormattedTime (pages .getJSONObject (i ).getIntValue ("duration" ), pages .getJSONObject (i ).getIntValue ("duration" ) >= 3600 ) + " 标题:" + pages .getJSONObject (i ).getString ("part" ));
168
174
}
169
- System .out .println ("请选择分P(输入 1~" + pages .size () + " 之间的整数):" );
175
+ if ( hint ) System .out .println ("请选择分P(输入 1~" + pages .size () + " 之间的整数):" );
170
176
int part = inputInt ();
171
177
if (part > pages .size ()) {
172
178
System .out .println ("输入的数字“" + part + "”太大,已为您选择末尾的分P " + pages .getJSONObject (pages .size () - 1 ).getString ("part" ));
@@ -197,7 +203,7 @@ private static Object[] getResolutions(JSONObject info, String cookie, int cid)
197
203
for (int i = 1 ; i < qualities .size (); i ++) {
198
204
System .out .println (String .format ("%3d" , i ) + ". " + qualities .getString (i ));
199
205
}
200
- System .out .println ("请选择清晰度(输入 1~" + (qualities .size () - 1 ) + " 之间的整数):" );
206
+ if ( hint ) System .out .println ("请选择清晰度(输入 1~" + (qualities .size () - 1 ) + " 之间的整数):" );
201
207
int quality = inputInt ();
202
208
String videoDownloadUrl ;
203
209
if (qualities .getIntValue (0 ) == 1 ) {
@@ -248,11 +254,11 @@ private static Path getPath(String name) throws IOException {
248
254
}
249
255
}
250
256
while (!pathSuccess ) {
251
- System .out .println ("\n 请输入保存路径:" );
257
+ if ( hint ) System .out .println ("\n 请输入保存路径:" );
252
258
savePath = inputStr ();
253
259
File file = new File (savePath );
254
260
if (!file .exists ()) {
255
- System .out .println ("该目录不存在,请决定是否创建该目录(输入“Y”或“N”):" );
261
+ if ( hint ) System .out .println ("该目录不存在,请决定是否创建该目录(输入“Y”或“N”):" );
256
262
if (inputStr ().equalsIgnoreCase ("Y" )) {
257
263
pathSuccess = file .mkdirs ();
258
264
if (!pathSuccess ) System .out .println ("创建目录失败" );
@@ -261,7 +267,7 @@ private static Path getPath(String name) throws IOException {
261
267
pathSuccess = true ;
262
268
}
263
269
if (pathSuccess ) {
264
- System .out .println ("请决定是否保存该保存路径(输入“Y”或“N”):" );
270
+ if ( hint ) System .out .println ("请决定是否保存该保存路径(输入“Y”或“N”):" );
265
271
if (inputStr ().equalsIgnoreCase ("Y" )) {
266
272
if (!config .exists ()) config .createNewFile ();
267
273
ConfigManager .init (config );
@@ -270,7 +276,7 @@ private static Path getPath(String name) throws IOException {
270
276
map = new LinkedHashMap <>();
271
277
map .put ("save-path" , savePath );
272
278
ConfigManager .dump (map );
273
- System .out .println ("已保存该保存路径" );
279
+ if ( hint ) System .out .println ("已保存该保存路径" );
274
280
}
275
281
}
276
282
}
@@ -282,7 +288,7 @@ private static void download(Object[] details, Path path) throws IOException, In
282
288
JSONArray qualities = (JSONArray ) details [1 ];
283
289
int quality = (int ) details [2 ];
284
290
JSONObject videoWeb = (JSONObject ) details [3 ];
285
- System .out .println ("\n 下载选项:\n 1. 视频+音频(合并需要 FFmpeg)\n 2. 仅视频\n 3. 仅音频\n 请选择下载选项(输入 1~3 之间的整数):" );
291
+ if ( hint ) System .out .println ("\n 下载选项:\n 1. 视频+音频(合并需要 FFmpeg)\n 2. 仅视频\n 3. 仅音频\n 请选择下载选项(输入 1~3 之间的整数):" );
286
292
int choice = inputInt ();
287
293
if (choice > 3 ) {
288
294
System .out .println ("输入的数字“" + choice + "”太大,已为您选择最后一个选项 仅音频" );
@@ -309,7 +315,7 @@ private static void download(Object[] details, Path path) throws IOException, In
309
315
}
310
316
}
311
317
while (ffmpegSuccess == 0 ) {
312
- System .out .println ("\n 请输入 ffmpeg.exe 目录(跳过合并请填“#”):" );
318
+ if ( hint ) System .out .println ("\n 请输入 ffmpeg.exe 目录(跳过合并请填“#”):" );
313
319
String ffmpegPath = inputStr ();
314
320
if (ffmpegPath .equals ("#" )) {
315
321
ffmpegSuccess = -1 ;
@@ -318,7 +324,7 @@ private static void download(Object[] details, Path path) throws IOException, In
318
324
ffmpeg = ffmpegPath .endsWith ("ffmpeg.exe" ) ? new File (ffmpegPath ) : new File (ffmpegPath , "ffmpeg.exe" );
319
325
ffmpegSuccess = ffmpeg .exists () ? 1 : 0 ;
320
326
if (ffmpegSuccess == 1 ) {
321
- System .out .println ("请决定是否保存 FFmpeg 路径(输入“Y”或“N”):" );
327
+ if ( hint ) System .out .println ("请决定是否保存 FFmpeg 路径(输入“Y”或“N”):" );
322
328
if (inputStr ().equalsIgnoreCase ("Y" )) {
323
329
if (!config .exists ()) config .createNewFile ();
324
330
ConfigManager .init (config );
@@ -327,7 +333,7 @@ private static void download(Object[] details, Path path) throws IOException, In
327
333
map = new LinkedHashMap <>();
328
334
map .put ("ffmpeg-path" , ffmpeg .getAbsolutePath ());
329
335
ConfigManager .dump (map );
330
- System .out .println ("已保存 FFmpeg 路径" );
336
+ if ( hint ) System .out .println ("已保存 FFmpeg 路径" );
331
337
}
332
338
}
333
339
}
@@ -452,14 +458,14 @@ private static JSONArray summarize(JSONArray qualitiesTV, JSONArray qualitiesWeb
452
458
qualities .add (1 );
453
459
for (int i = 0 ; i < qualitiesTV .size (); i ++)
454
460
if (!videoTV .getJSONArray ("accept_watermark" ).getBoolean (i ))
455
- qualities .add (String .format ("%-11s" , "TV " + qualitiesTV .getString (i )) + watermark (videoTV .getJSONArray ("accept_watermark" ).getBoolean (i )));
461
+ qualities .add (String .format ("%-11s" , qualitiesTV .getString (i )) + watermark (videoTV .getJSONArray ("accept_watermark" ).getBoolean (i )));
456
462
else
457
463
qualities .add (qualitiesTV .getString (i ));
458
464
} else {
459
465
qualities .add (0 );
460
466
}
461
467
for (int i = 0 ; i < qualitiesWeb .size (); i ++)
462
- qualities .add ("Web " + qualitiesWeb .getString (i ));
468
+ qualities .add (qualitiesWeb .getString (i ));
463
469
return qualities ;
464
470
}
465
471
0 commit comments