5
5
import me .naptie .bilidownload .utils .ConfigManager ;
6
6
import me .naptie .bilidownload .utils .HttpManager ;
7
7
import me .naptie .bilidownload .utils .LoginManager ;
8
+ import me .naptie .bilidownload .utils .SignUtil ;
8
9
9
10
import java .io .*;
10
11
import java .net .MalformedURLException ;
@@ -29,20 +30,13 @@ public static void main(String[] args) throws IOException, InterruptedException
29
30
debug = args .length > 0 && args [0 ].equalsIgnoreCase ("debug" );
30
31
config = new File ("config.yml" );
31
32
setScanner ();
32
-
33
33
String id = getNumber ();
34
-
35
- String cookie = login ();
36
-
37
- JSONObject info = getVideoInfo (id , cookie );
34
+ String [] login = login ();
35
+ JSONObject info = getVideoInfo (id , login [0 ], !login [1 ].isEmpty ());
38
36
Object [] specified = specify (info );
39
-
40
- Object [] details = getResolutions (info , cookie , (int ) specified [0 ]);
41
-
37
+ Object [] details = getResolutions (info , login , !login [1 ].isEmpty (), (int ) specified [0 ]);
42
38
String [] path = getPath ((String ) specified [1 ]);
43
-
44
39
download (details , path );
45
-
46
40
System .out .println ("\n 程序运行结束;总运行时间:" + getFormattedTime (System .currentTimeMillis () - beginTime ));
47
41
}
48
42
@@ -80,9 +74,9 @@ private static String getNumber() {
80
74
return input ();
81
75
}
82
76
83
- private static String login () throws IOException {
77
+ private static String [] login () throws IOException {
84
78
boolean loginSuccess = false ;
85
- String sessData , cookie = "#" ;
79
+ String sessData = "#" , accessToken = "" , cookie = "#" ;
86
80
if (config .exists ()) {
87
81
ConfigManager .init (config );
88
82
Map <String , Object > map = ConfigManager .get ();
@@ -91,57 +85,76 @@ private static String login() throws IOException {
91
85
if (map .containsKey ("sess-data" )) {
92
86
sessData = (String ) map .get ("sess-data" );
93
87
cookie = "SESSDATA=" + sessData + "; Path=/; Domain=bilibili.com;" ;
94
- JSONObject login = HttpManager .readJsonFromUrl ("https://api.bilibili.com/x/web-interface/nav" , cookie );
88
+ JSONObject login = HttpManager .readJsonFromUrl ("https://api.bilibili.com/x/web-interface/nav" , cookie , false );
95
89
if (login .getIntValue ("code" ) == 0 )
96
90
if (login .getJSONObject ("data" ).getBoolean ("isLogin" )) {
97
91
if (debug )
98
92
System .out .println ("检测到配置文件,已自动填充 SESSDATA\n ID:" + login .getJSONObject ("data" ).getString ("uname" ) + "\n UID:" + login .getJSONObject ("data" ).getIntValue ("mid" ));
99
93
loginSuccess = true ;
100
94
}
101
95
}
96
+ if (map .containsKey ("access-token" )) {
97
+ accessToken = (String ) map .get ("access-token" );
98
+ String params = "access_key=" + accessToken + "&appkey=4409e2ce8ffd12b8&ts=" + System .currentTimeMillis ();
99
+ JSONObject login = HttpManager .readJsonFromUrl ("https://app.bilibili.com/x/v2/account/myinfo?" + params + "&sign=" + SignUtil .generate (params ), "#" , true );
100
+ if (login .getIntValue ("code" ) == 0 ) {
101
+ if (debug )
102
+ System .out .println ("检测到配置文件,已自动填充 token\n ID:" + login .getJSONObject ("data" ).getString ("name" ) + "\n UID:" + login .getJSONObject ("data" ).getIntValue ("mid" ));
103
+ loginSuccess = true ;
104
+ }
105
+ }
102
106
}
103
- while (! loginSuccess ) {
104
- System .out .println ("\n 登录方式:\n 1. Web 端二维码登录\n 2. TV 端二维码登录\n 3. 输入 SESSDATA 登录\n 4. 跳过登录\n 请选择登录方式(输入 1~4 之间的整数):" );
107
+ do {
108
+ System .out .println ("\n 登录方式:\n 1. WEB 端二维码登录\n 2. TV 端二维码登录\n 3. 输入 SESSDATA 登录\n 4. 跳过登录\n 请选择登录方式(输入 1~4 之间的整数):" );
105
109
int method = inputInt ();
106
110
if (method < 1 ) {
107
- System .out .println ("输入的数字“" + method + "”太小,已为您选择 Web 端二维码登录" );
111
+ System .out .println ("输入的数字“" + method + "”太小,已为您选择 WEB 端二维码登录" );
108
112
method = 1 ;
109
113
}
110
114
if (method > 4 ) {
111
115
System .out .println ("输入的数字“" + method + "”太大,已为您选择跳过登录" );
112
116
method = 4 ;
113
117
}
114
118
if (method == 1 ) {
115
- LoginManager .showQRCode ( false );
116
- while (true ) {
119
+ LoginManager .showQRCodeFromWeb ( );
120
+ while (true )
117
121
if (!LoginManager .sessData .equalsIgnoreCase ("*Not_Yet_Prepared*" ))
118
122
break ;
119
- }
120
123
sessData = LoginManager .sessData ;
121
124
if (sessData .isEmpty ()) {
122
125
System .out .println ("登录失败" );
123
- continue ;
126
+ if (hint ) System .out .println ("请决定是否继续登录(输入“Y”或“N”):" );
127
+ if (input ().equalsIgnoreCase ("Y" ))
128
+ continue ;
124
129
}
125
130
} else if (method == 2 ) {
126
- sessData = "*TV_Login*" ;
127
- LoginManager .showQRCode (true );
128
-
131
+ LoginManager .showQRCodeFromTV ();
132
+ while (true )
133
+ if (!LoginManager .accessToken .equalsIgnoreCase ("*Not_Yet_Prepared*" ))
134
+ break ;
135
+ accessToken = LoginManager .accessToken ;
136
+ if (accessToken .isEmpty ()) {
137
+ System .out .println ("登录失败" );
138
+ if (hint ) System .out .println ("请决定是否继续登录(输入“Y”或“N”):" );
139
+ if (input ().equalsIgnoreCase ("Y" ))
140
+ continue ;
141
+ }
129
142
} else if (method == 3 ) {
130
143
if (hint ) System .out .println ("\n 请输入 Cookie 中 SESSDATA 的值:" );
131
144
sessData = input ();
132
145
} else {
133
- sessData = "#" ;
146
+ break ;
134
147
}
135
148
if (sessData .equals ("#" )) {
136
149
cookie = "#" ;
137
150
break ;
138
- } else {
151
+ } else if ( method != 2 ) {
139
152
cookie = "SESSDATA=" + sessData + "; Path=/; Domain=bilibili.com;" ;
140
- JSONObject login = HttpManager .readJsonFromUrl ("https://api.bilibili.com/x/web-interface/nav" , cookie );
153
+ JSONObject login = HttpManager .readJsonFromUrl ("https://api.bilibili.com/x/web-interface/nav" , cookie , false );
141
154
if (login .getIntValue ("code" ) == 0 )
142
155
if (login .getJSONObject ("data" ).getBoolean ("isLogin" )) {
143
156
loginSuccess = true ;
144
- System .out .println ("登录成功\n ID:" + login .getJSONObject ("data" ).getString ("uname" ) + "\n UID:" + login .getJSONObject ("data" ).getIntValue ("mid" ));
157
+ System .out .println ("登录成功" + ( debug ? " \n ID:" + login .getJSONObject ("data" ).getString ("uname" ) + "\n UID:" + login .getJSONObject ("data" ).getIntValue ("mid" ) : " " ));
145
158
if (hint ) System .out .println ("请决定是否保存该 SESSDATA(输入“Y”或“N”):" );
146
159
if (input ().equalsIgnoreCase ("Y" )) {
147
160
if (!config .exists ()) config .createNewFile ();
@@ -153,20 +166,61 @@ private static String login() throws IOException {
153
166
ConfigManager .dump (map );
154
167
if (hint ) System .out .println ("已保存 SESSDATA" );
155
168
}
169
+ if (hint ) System .out .println ("请决定是否继续登录(输入“Y”或“N”):" );
170
+ if (input ().equalsIgnoreCase ("Y" ))
171
+ loginSuccess = false ;
156
172
} else {
157
173
System .out .println ("登录失败" );
174
+ if (loginSuccess ) {
175
+ if (hint ) System .out .println ("请决定是否继续登录(输入“Y”或“N”):" );
176
+ if (input ().equalsIgnoreCase ("Y" ))
177
+ loginSuccess = false ;
178
+ }
158
179
}
159
180
else {
160
181
System .out .println ("登录失败" );
182
+ if (loginSuccess ) {
183
+ if (hint ) System .out .println ("请决定是否继续登录(输入“Y”或“N”):" );
184
+ if (input ().equalsIgnoreCase ("Y" ))
185
+ loginSuccess = false ;
186
+ }
187
+ }
188
+ } else {
189
+ String params = "access_key=" + accessToken + "&appkey=4409e2ce8ffd12b8&ts=" + System .currentTimeMillis ();
190
+ JSONObject login = HttpManager .readJsonFromUrl ("https://app.bilibili.com/x/v2/account/myinfo?" + params + "&sign=" + SignUtil .generate (params ), "#" , true );
191
+ if (login .getIntValue ("code" ) == 0 ) {
192
+ loginSuccess = true ;
193
+ System .out .println ("登录成功" + (debug ? "\n ID:" + login .getJSONObject ("data" ).getString ("name" ) + "\n UID:" + login .getJSONObject ("data" ).getIntValue ("mid" ) : "" ));
194
+ if (hint ) System .out .println ("请决定是否保存该 token(输入“Y”或“N”):" );
195
+ if (input ().equalsIgnoreCase ("Y" )) {
196
+ if (!config .exists ()) config .createNewFile ();
197
+ ConfigManager .init (config );
198
+ Map <String , Object > map = ConfigManager .get ();
199
+ if (map == null )
200
+ map = new LinkedHashMap <>();
201
+ map .put ("access-token" , sessData );
202
+ ConfigManager .dump (map );
203
+ if (hint ) System .out .println ("已保存 token" );
204
+ }
205
+ if (hint ) System .out .println ("请决定是否继续登录(输入“Y”或“N”):" );
206
+ if (input ().equalsIgnoreCase ("Y" ))
207
+ loginSuccess = false ;
208
+ } else {
209
+ System .out .println ("登录失败" );
210
+ if (loginSuccess ) {
211
+ if (hint ) System .out .println ("请决定是否继续登录(输入“Y”或“N”):" );
212
+ if (input ().equalsIgnoreCase ("Y" ))
213
+ loginSuccess = false ;
214
+ }
161
215
}
162
216
}
163
- }
164
- return cookie ;
217
+ } while (! loginSuccess );
218
+ return new String []{ cookie , accessToken } ;
165
219
}
166
220
167
- private static JSONObject getVideoInfo (String id , String cookie ) throws IOException {
221
+ private static JSONObject getVideoInfo (String id , String cookie , boolean tv ) throws IOException {
168
222
System .out .println ((hint ? "\n " : "" ) + "正在获取稿件信息······" );
169
- JSONObject info = HttpManager .readJsonFromUrl ("https://api.bilibili.com/x/web-interface/view?" + (id .toLowerCase ().startsWith ("av" ) ? "aid=" + id .substring (2 ) : "bvid=" + id ), cookie );
223
+ JSONObject info = HttpManager .readJsonFromUrl ("https://api.bilibili.com/x/web-interface/view?" + (id .toLowerCase ().startsWith ("av" ) ? "aid=" + id .substring (2 ) : "bvid=" + id ), tv ? "#" : cookie , tv );
170
224
if (info .getIntValue ("code" ) != 0 ) {
171
225
System .out .println (info .getString ("message" ));
172
226
System .out .println ("\n 程序运行结束,错误代码:" + info .getIntValue ("code" ) + ";总运行时间:" + getFormattedTime (System .currentTimeMillis () - beginTime ));
@@ -212,12 +266,12 @@ private static Object[] specify(JSONObject info) {
212
266
return new Object []{cid , name };
213
267
}
214
268
215
- private static Object [] getResolutions (JSONObject info , String cookie , int cid ) throws IOException {
269
+ private static Object [] getResolutions (JSONObject info , String [] auth , boolean tv , int cid ) throws IOException {
216
270
System .out .println ("\n 正在获取清晰度信息······" );
217
- String videoUrlTV = "https://api.snm0516.aisee.tv/x/tv/ugc/playurl?avid=" + info .getIntValue ("aid" ) + "&mobi_app=android_tv_yst&fnval=16 &qn=120&cid=" + cid + "& platform=android&build=103800&fnver=0" ;
271
+ String videoUrlTV = "https://api.snm0516.aisee.tv/x/tv/ugc/playurl?avid=" + info .getIntValue ("aid" ) + "&mobi_app=android_tv_yst&fnval=80 &qn=120&cid=" + cid + ( tv ? "&access_key=" + auth [ 1 ] : "" ) + "&fourk=1& platform=android&device =android&build=103800&fnver=0" ;
218
272
String videoUrlWeb = "https://api.bilibili.com/x/player/playurl?avid=" + info .getIntValue ("aid" ) + "&cid=" + cid + "&fnval=80&fourk=1" ;
219
- JSONObject videoTV = HttpManager .readJsonFromUrl (videoUrlTV , cookie );
220
- JSONObject videoWeb = HttpManager .readJsonFromUrl (videoUrlWeb , cookie ).getJSONObject ("data" );
273
+ JSONObject videoTV = HttpManager .readJsonFromUrl (videoUrlTV , "#" , true );
274
+ JSONObject videoWeb = HttpManager .readJsonFromUrl (videoUrlWeb , auth [ 0 ], false ).getJSONObject ("data" );
221
275
JSONArray qualitiesTV = videoTV .getJSONArray ("accept_description" );
222
276
JSONArray qualitiesWeb = videoWeb .getJSONArray ("accept_description" );
223
277
JSONArray qualities = summarize (qualitiesTV , qualitiesWeb , videoTV );
0 commit comments