2
2
3
3
import com .alibaba .fastjson .JSONArray ;
4
4
import com .alibaba .fastjson .JSONObject ;
5
+ import cz .mallat .uasparser .OnlineUpdater ;
6
+ import cz .mallat .uasparser .UASparser ;
7
+ import me .naptie .bilidownload .objects .Downloader ;
5
8
import me .naptie .bilidownload .utils .ConfigManager ;
6
9
import me .naptie .bilidownload .utils .HttpManager ;
7
10
import me .naptie .bilidownload .utils .LoginManager ;
8
11
import me .naptie .bilidownload .utils .SignUtil ;
12
+ import org .apache .commons .codec .digest .DigestUtils ;
9
13
10
- import java .io .*;
11
- import java .net .MalformedURLException ;
14
+ import java .io .File ;
15
+ import java .io .FileNotFoundException ;
16
+ import java .io .IOException ;
12
17
import java .net .URL ;
13
18
import java .net .URLConnection ;
14
19
import java .text .SimpleDateFormat ;
@@ -60,7 +65,7 @@ private static int inputInt() {
60
65
}
61
66
62
67
private static String input () {
63
- String input = scanner .next ();
68
+ String input = scanner .nextLine ();
64
69
if (input .equalsIgnoreCase ("*exit" )) {
65
70
System .out .println ("\n 程序运行结束;总运行时间:" + getFormattedTime (System .currentTimeMillis () - beginTime ));
66
71
System .exit (0 );
@@ -119,6 +124,7 @@ private static String[] login() throws IOException {
119
124
sessData = LoginManager .sessData ;
120
125
if (sessData .isEmpty ()) {
121
126
System .out .println ("登录失败" );
127
+ System .out .println ("本次 WEB 端二维码登录所用 UA 的浏览器为 " + (new UASparser (OnlineUpdater .getVendoredInputStream ()).parse (LoginManager .userAgent )).getUaFamily () + ",请于登陆操作通知中核实对照" );
122
128
continue ;
123
129
}
124
130
} else if (method == 2 ) {
@@ -145,6 +151,7 @@ private static String[] login() throws IOException {
145
151
if (login .getJSONObject ("data" ).getBoolean ("isLogin" )) {
146
152
webSuccess = loginSuccess = true ;
147
153
System .out .println ("登录成功" + (debug ? "\n ID:" + login .getJSONObject ("data" ).getString ("uname" ) + "\n UID:" + login .getJSONObject ("data" ).getIntValue ("mid" ) : "" ));
154
+ System .out .println ("本次 WEB 端二维码登录所用 UA 的浏览器为 " + (new UASparser (OnlineUpdater .getVendoredInputStream ()).parse (LoginManager .userAgent )).getUaFamily () + ",请于登录操作通知中核实对照" );
148
155
if (hint ) System .out .println ("请决定是否保存该 SESSDATA(输入“Y”代表是,输入“N”代表否):" );
149
156
if (input ().equalsIgnoreCase ("Y" )) {
150
157
if (!config .exists ()) config .createNewFile ();
@@ -403,8 +410,9 @@ private static void download(Object[] details, String[] path) throws IOException
403
410
}
404
411
String audioDownloadUrl = getAudioDownload (videoWeb );
405
412
System .out .println ("\n 成功获取音频下载地址:" + audioDownloadUrl );
406
- File video = ffmpegSuccess == -1 ? new File (path [0 ], path [1 ] + ".mp4" ) : new File (path [0 ], "tmpVid.mp4" );
407
- File audio = ffmpegSuccess == -1 ? new File (path [0 ], path [1 ] + ".aac" ) : new File (path [0 ], "tmpAud.aac" );
413
+ String md5 = DigestUtils .md5Hex (path [1 ] + System .currentTimeMillis ());
414
+ File video = ffmpegSuccess == -1 ? new File (path [0 ], path [1 ] + ".mp4" ) : new File (path [0 ], "tmpVid_" + md5 + ".mp4" );
415
+ File audio = ffmpegSuccess == -1 ? new File (path [0 ], path [1 ] + ".aac" ) : new File (path [0 ], "tmpAud_" + md5 + ".aac" );
408
416
System .out .println ("\n 正在下载视频至 " + video .getAbsolutePath ());
409
417
long lenVid = downloadFromUrl (videoDownloadUrl , video .getAbsolutePath ());
410
418
videoSuccess = video .length () == lenVid ;
@@ -423,6 +431,8 @@ private static void download(Object[] details, String[] path) throws IOException
423
431
} else {
424
432
System .out .println ("合并失败" );
425
433
}
434
+ } else {
435
+ System .out .println ("\n 合并失败" );
426
436
}
427
437
break ;
428
438
}
@@ -557,53 +567,42 @@ private static String watermark(boolean availability) {
557
567
else return "无水印" ;
558
568
}
559
569
560
- private static long downloadFromUrl (String address , String path ) {
561
- long beginTime = System .currentTimeMillis ();
562
- int byteRead ;
563
- URL url ;
564
- try {
565
- url = new URL (address );
566
- } catch (MalformedURLException e1 ) {
567
- e1 .printStackTrace ();
570
+ private static long downloadFromUrl (String address , String path ) throws IOException {
571
+ Downloader downloader = new Downloader (address , path );
572
+ URLConnection request = (new URL (address )).openConnection ();
573
+ request .setRequestProperty ("Referer" , "https://www.bilibili.com" );
574
+ long totalLen = request .getContentLengthLong ();
575
+ long result = downloader .download (totalLen );
576
+ double total = totalLen / 1024.0 / 1024.0 ;
577
+ System .out .println ("文件大小:" + String .format ("%,.3f" , total ) + "MB" );
578
+ if (result == -1 ) {
579
+ System .out .println ("磁盘空间不足" );
568
580
return -1 ;
569
581
}
570
- try {
571
- URLConnection request = url .openConnection ();
572
- request .setRequestProperty ("Referer" , "https://www.bilibili.com" );
573
- InputStream inStream = request .getInputStream ();
574
- FileOutputStream fs = new FileOutputStream (path );
575
-
576
- byte [] buffer = new byte [1024 ];
577
- StringBuilder progress = new StringBuilder ();
578
- double total = request .getContentLengthLong () / 1024.0 / 1024.0 ;
579
- System .out .print ("进度:" );
580
- while ((byteRead = inStream .read (buffer )) != -1 ) {
581
- fs .write (buffer , 0 , byteRead );
582
- int lastByteLength = progress .toString ().getBytes ().length ;
583
- int lastLength = progress .length ();
584
- if (progress .length () > 0 ) {
585
- for (int i = 0 ; i < lastByteLength ; i ++)
586
- System .out .print ("\b " );
587
- }
588
- double downloaded = fs .getChannel ().size () / 1024.0 / 1024.0 ;
589
- double speed = ((System .currentTimeMillis () - beginTime ) / 1000.0 == 0 ) ? 0 : downloaded / ((System .currentTimeMillis () - beginTime ) / 1000.0 );
590
- progress = new StringBuilder (String .format ("%.2f" , (fs .getChannel ().size () * 100.0 / request .getContentLengthLong ())) + "%(" + String .format ("%,.3f" , downloaded ) + "MB / " + String .format ("%,.3f" , total ) + "MB);速度:" + String .format ("%,.3f" , speed ) + "MB/s;剩余时间:" + String .format ("%,.3f" , (total - downloaded ) / speed ) + "s" );
591
- for (int i = 0 ; i <= lastLength - progress .length (); i ++)
592
- progress .append (" " );
593
- System .out .print (progress );
582
+ StringBuilder progress = new StringBuilder ();
583
+ long downloadedLen = downloader .getDownloaded ();
584
+ double downloaded , speed ;
585
+ while (downloadedLen != totalLen ) {
586
+ int lastLength = progress .length ();
587
+ int lastByteLength = progress .toString ().getBytes ().length ;
588
+ if (progress .length () > 0 ) {
589
+ for (int i = 0 ; i < lastByteLength ; i ++)
590
+ System .out .print ("\b " );
594
591
}
595
- System .out .print ("\b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b " );
596
- String timeSpent = "用时:" + String .format ("%,.3f" , (System .currentTimeMillis () - beginTime ) / 1000.0 ) + "s" ;
597
- System .out .print (timeSpent );
598
- for (int i = 0 ; i < 13 - timeSpent .length (); i ++)
599
- System .out .print (" " );
600
- inStream .close ();
601
- fs .close ();
602
- return request .getContentLengthLong ();
603
- } catch (IOException e ) {
604
- e .printStackTrace ();
605
- return -1 ;
592
+ downloadedLen = downloader .getDownloaded ();
593
+ downloaded = downloadedLen / 1024.0 / 1024.0 ;
594
+ speed = ((System .currentTimeMillis () - beginTime ) / 1000.0 == 0 ) ? 0 : downloaded / ((System .currentTimeMillis () - beginTime ) / 1000.0 );
595
+ progress = new StringBuilder ("进度:" + String .format ("%.2f" , (downloadedLen * 100.0 / totalLen )) + "%(" + String .format ("%,.3f" , downloaded ) + "MB / " + String .format ("%,.3f" , total ) + "MB);速度:" + String .format ("%,.3f" , speed ) + "MB/s;剩余时间:" + String .format ("%,.3f" , (total - downloaded ) / speed ).replace ("Infinity" , "∞" ) + "s" );
596
+ for (int i = 0 ; i <= lastLength - progress .length (); i ++)
597
+ progress .append (" " );
598
+ System .out .print (progress );
606
599
}
600
+ System .out .print ("\b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b \b " );
601
+ String timeSpent = "用时:" + String .format ("%,.3f" , (System .currentTimeMillis () - beginTime ) / 1000.0 ) + "s" ;
602
+ System .out .print (timeSpent );
603
+ for (int i = 0 ; i < 13 - timeSpent .length (); i ++)
604
+ System .out .print (" " );
605
+ return totalLen ;
607
606
}
608
607
609
- }
608
+ }
0 commit comments