Update Bili.java
This commit is contained in:
parent
d370776430
commit
d0baac0f6e
|
|
@ -1,10 +1,13 @@
|
||||||
package com.github.catvod.spider;
|
package com.github.catvod.spider;
|
||||||
|
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
|
import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.drawable.ColorDrawable;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.webkit.CookieManager;
|
import android.webkit.CookieManager;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
|
|
@ -17,8 +20,8 @@ import com.github.catvod.crawler.Spider;
|
||||||
import com.github.catvod.net.OkHttp;
|
import com.github.catvod.net.OkHttp;
|
||||||
import com.github.catvod.utils.Prefers;
|
import com.github.catvod.utils.Prefers;
|
||||||
import com.github.catvod.utils.QRCode;
|
import com.github.catvod.utils.QRCode;
|
||||||
import com.github.catvod.utils.Utils;
|
|
||||||
import com.github.catvod.utils.Trans;
|
import com.github.catvod.utils.Trans;
|
||||||
|
import com.github.catvod.utils.Utils;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
|
@ -39,49 +42,43 @@ import java.util.concurrent.TimeUnit;
|
||||||
*/
|
*/
|
||||||
public class Bili extends Spider {
|
public class Bili extends Spider {
|
||||||
|
|
||||||
private static final String url = "https://www.bilibili.com";
|
private final String url = "https://www.bilibili.com";
|
||||||
private JSONObject ext;
|
|
||||||
private ScheduledExecutorService service;
|
private ScheduledExecutorService service;
|
||||||
private ImageView view;
|
private AlertDialog dialog;
|
||||||
private static boolean login;
|
private JSONObject ext;
|
||||||
private static String finalUrl = "";
|
private String cookie;
|
||||||
private static boolean FirstTime = false;
|
private String extend;
|
||||||
private static String cookie = "";
|
private boolean login;
|
||||||
|
|
||||||
private void getCookie() {
|
private void getCookie() {
|
||||||
cookie = Prefers.getString("BiliCookie", "");
|
cookie = Prefers.getString("BiliCookie");
|
||||||
HashMap<String, List<String>> respHeaderMap = new HashMap<>();
|
HashMap<String, List<String>> respHeader = new HashMap<>();
|
||||||
OkHttp.string(url, getHeaders(), respHeaderMap);
|
OkHttp.string(url, getHeaders(), respHeader);
|
||||||
if (cookie.isEmpty()) {
|
setCookie(respHeader);
|
||||||
for (String kk : Objects.requireNonNull(respHeaderMap.get("set-cookie"))) {
|
|
||||||
cookie += kk.split(";")[0] + ";";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HashMap<String, String> getHeaders() {
|
private HashMap<String, String> getHeaders() {
|
||||||
HashMap<String, String> headers = new HashMap<>();
|
HashMap<String, String> headers = new HashMap<>();
|
||||||
|
if (!cookie.isEmpty()) headers.put("cookie", cookie);
|
||||||
headers.put("User-Agent", Utils.CHROME);
|
headers.put("User-Agent", Utils.CHROME);
|
||||||
headers.put("Referer", url);
|
headers.put("Referer", url);
|
||||||
if (!cookie.isEmpty()) headers.put("cookie", cookie);
|
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void fetchRule() throws Exception {
|
||||||
|
if (login && !TextUtils.isEmpty(cookie)) return;
|
||||||
|
if (extend.startsWith("http")) extend = OkHttp.string(extend);
|
||||||
|
ext = new JSONObject(extend);
|
||||||
|
String cookie = ext.optString("cookie", "");
|
||||||
|
if (cookie.isEmpty()) getCookie();
|
||||||
|
else this.cookie = cookie.startsWith("http") ? OkHttp.string(cookie) : cookie;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(Context context, String extend) {
|
public void init(Context context, String extend) {
|
||||||
try {
|
try {
|
||||||
if (extend.startsWith("http")) {
|
this.extend = extend;
|
||||||
extend = OkHttp.string(extend);
|
fetchRule();
|
||||||
}
|
|
||||||
ext = new JSONObject(extend);
|
|
||||||
if (ext.optString("cookie").length() > 0) {
|
|
||||||
cookie = ext.optString("cookie");
|
|
||||||
if (cookie.startsWith("http")) cookie = OkHttp.string(cookie);
|
|
||||||
} else {
|
|
||||||
getCookie();
|
|
||||||
}
|
|
||||||
String content = OkHttp.string("https://api.bilibili.com/x/web-interface/nav", getHeaders());
|
|
||||||
login = new JSONObject(content).getJSONObject("data").getBoolean("isLogin");
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
@ -89,11 +86,13 @@ public class Bili extends Spider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String homeContent(boolean filter) throws Exception {
|
public String homeContent(boolean filter) throws Exception {
|
||||||
|
fetchRule();
|
||||||
return Result.string(Class.arrayFrom(ext.getJSONArray("classes").toString()), ext.getJSONObject("filter"));
|
return Result.string(Class.arrayFrom(ext.getJSONArray("classes").toString()), ext.getJSONObject("filter"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String homeVideoContent() throws Exception {
|
public String homeVideoContent() throws Exception {
|
||||||
|
fetchRule();
|
||||||
return categoryContent("窗 白噪音", "1", true, new HashMap<>());
|
return categoryContent("窗 白噪音", "1", true, new HashMap<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -153,11 +152,7 @@ public class Bili extends Spider {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
|
public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
|
||||||
if (!login && !FirstTime) {
|
checkLogin();
|
||||||
FirstTime = true;
|
|
||||||
checkService();
|
|
||||||
getQRCode();
|
|
||||||
}
|
|
||||||
String[] ids = id.split("\\+");
|
String[] ids = id.split("\\+");
|
||||||
String aid = ids[0];
|
String aid = ids[0];
|
||||||
String cid = ids[1];
|
String cid = ids[1];
|
||||||
|
|
@ -167,62 +162,89 @@ public class Bili extends Spider {
|
||||||
return Result.get().url(url).header(getHeaders()).string();
|
return Result.get().url(url).header(getHeaders()).string();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkService() {
|
private void checkLogin() throws Exception {
|
||||||
if (service != null) service.shutdownNow();
|
if (login) return;
|
||||||
if (view != null) Init.run(() -> Utils.removeView(view));
|
String content = OkHttp.string("https://api.bilibili.com/x/web-interface/nav", getHeaders());
|
||||||
|
login = new JSONObject(content).getJSONObject("data").getBoolean("isLogin");
|
||||||
|
if (login) return;
|
||||||
|
stopService();
|
||||||
|
getQRCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void getQRCode() throws JSONException {
|
private void getQRCode() throws JSONException {
|
||||||
JSONObject data = new JSONObject(OkHttp.string("https://passport.bilibili.com/x/passport-login/web/qrcode/generate?source=main-mini", null));
|
String json = OkHttp.string("https://passport.bilibili.com/x/passport-login/web/qrcode/generate?source=main-mini", null);
|
||||||
String qrcode = data.getJSONObject("data").getString("url");
|
JSONObject data = new JSONObject(json).getJSONObject("data");
|
||||||
Init.run(() -> showCode(qrcode));
|
Init.run(() -> showQRCode(data));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void showQRCode(JSONObject data) {
|
||||||
|
try {
|
||||||
|
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Utils.dp2px(240), Utils.dp2px(240));
|
||||||
|
ImageView image = new ImageView(Init.context());
|
||||||
|
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
||||||
|
image.setImageBitmap(QRCode.getBitmap(data.getString("url"), 240, 2));
|
||||||
|
FrameLayout frame = new FrameLayout(Init.context());
|
||||||
|
params.gravity = Gravity.CENTER;
|
||||||
|
frame.addView(image, params);
|
||||||
|
dialog = new AlertDialog.Builder(Init.getActivity()).setView(frame).setOnDismissListener(this::dismiss).show();
|
||||||
|
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
||||||
|
Init.execute(() -> startService(data));
|
||||||
|
Init.show("請使用 BiliBili App 掃描二維碼");
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startService(JSONObject data) {
|
||||||
service = Executors.newScheduledThreadPool(1);
|
service = Executors.newScheduledThreadPool(1);
|
||||||
service.scheduleAtFixedRate(() -> {
|
service.scheduleAtFixedRate(() -> {
|
||||||
try {
|
try {
|
||||||
String qr = data.getJSONObject("data").getString("qrcode_key");
|
String qr = data.getString("qrcode_key");
|
||||||
String url = "https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=" + qr + "&source=main_mini";
|
String url = "https://passport.bilibili.com/x/passport-login/web/qrcode/poll?qrcode_key=" + qr + "&source=main_mini";
|
||||||
String content = OkHttp.string(url, getHeaders());
|
String json = OkHttp.string(url, getHeaders());
|
||||||
finalUrl = new JSONObject(content).getJSONObject("data").getString("url");
|
url = new JSONObject(json).getJSONObject("data").getString("url");
|
||||||
} catch (JSONException e) {
|
if (!TextUtils.isEmpty(url)) setCookie(url);
|
||||||
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
if (!finalUrl.isEmpty()) setCookie(finalUrl);
|
|
||||||
}, 1, 1, TimeUnit.SECONDS);
|
}, 1, 1, TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setCookie(String value) {
|
private void setCookie(String url) {
|
||||||
finalUrl = value;
|
Init.show("請重新進入播放頁");
|
||||||
loadWebView();
|
loadWebView(url);
|
||||||
Init.show("请重新进入播放页");
|
stopService();
|
||||||
checkService();
|
}
|
||||||
|
|
||||||
|
private void stopService() {
|
||||||
|
if (service != null) service.shutdownNow();
|
||||||
|
Init.run(this::dismiss);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dismiss(DialogInterface dialog) {
|
||||||
|
stopService();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dismiss() {
|
||||||
|
try {
|
||||||
|
if (dialog != null) dialog.dismiss();
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||||
private void loadWebView() {
|
private void loadWebView(String url) {
|
||||||
CookieManager cookieManager = CookieManager.getInstance();
|
CookieManager cookieManager = CookieManager.getInstance();
|
||||||
cookieManager.flush();
|
cookieManager.flush();
|
||||||
cookieManager.removeAllCookies(null);
|
cookieManager.removeAllCookies(null);
|
||||||
cookieManager.removeSessionCookies(null);
|
cookieManager.removeSessionCookies(null);
|
||||||
HashMap<String, List<String>> respHeaderMap = new HashMap<>();
|
HashMap<String, List<String>> respHeader = new HashMap<>();
|
||||||
OkHttp.stringNoRedirect(finalUrl, getHeaders(), respHeaderMap);
|
OkHttp.stringNoRedirect(url, getHeaders(), respHeader);
|
||||||
cookie = "";
|
setCookie(respHeader);
|
||||||
for (String kk : Objects.requireNonNull(respHeaderMap.get("set-cookie"))) {
|
|
||||||
cookie = cookie.concat(kk.split(";")[0] + ";");
|
|
||||||
}
|
|
||||||
Prefers.put("BiliCookie", cookie);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showCode(String text) {
|
private void setCookie(HashMap<String, List<String>> respHeaderMap) {
|
||||||
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
|
StringBuilder sb = new StringBuilder();
|
||||||
params.gravity = Gravity.CENTER;
|
for (String value : Objects.requireNonNull(respHeaderMap.get("set-cookie"))) sb.append(value.split(";")[0]).append(";");
|
||||||
Utils.addView(view = create(text), params);
|
Prefers.put("BiliCookie", cookie = sb.toString());
|
||||||
Init.show("请使用 BiliBili App 扫描二维码");
|
|
||||||
}
|
|
||||||
|
|
||||||
private ImageView create(String value) {
|
|
||||||
ImageView view = new ImageView(Init.context());
|
|
||||||
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
|
|
||||||
view.setImageBitmap(QRCode.getBitmap(value, 250, 2));
|
|
||||||
return view;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue