Update bilibili
This commit is contained in:
parent
d0baac0f6e
commit
3574b0ef56
|
|
@ -1,15 +1,12 @@
|
|||
package com.github.catvod.spider;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.view.Gravity;
|
||||
import android.webkit.CookieManager;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
|
||||
|
|
@ -24,7 +21,6 @@ import com.github.catvod.utils.Trans;
|
|||
import com.github.catvod.utils.Utils;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.jsoup.Jsoup;
|
||||
|
||||
|
|
@ -32,6 +28,7 @@ import java.net.URLEncoder;
|
|||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
|
@ -42,42 +39,38 @@ import java.util.concurrent.TimeUnit;
|
|||
*/
|
||||
public class Bili extends Spider {
|
||||
|
||||
private final String url = "https://www.bilibili.com";
|
||||
private ScheduledExecutorService service;
|
||||
private HashMap<String, String> header;
|
||||
private AlertDialog dialog;
|
||||
private JSONObject ext;
|
||||
private String cookie;
|
||||
private String extend;
|
||||
private boolean login;
|
||||
|
||||
private void getCookie() {
|
||||
cookie = Prefers.getString("BiliCookie");
|
||||
HashMap<String, List<String>> respHeader = new HashMap<>();
|
||||
OkHttp.string(url, getHeaders(), respHeader);
|
||||
setCookie(respHeader);
|
||||
private String getCookie(String cookie) {
|
||||
String cache = Prefers.getString("BiliCookie");
|
||||
if (!TextUtils.isEmpty(cache)) return cache;
|
||||
if (cookie.startsWith("http")) return OkHttp.string(cookie).replace("\n", "").trim();
|
||||
return cookie.isEmpty() ? "buvid3=84B0395D-C9F2-C490-E92E-A09AB48FE26E71636infoc" : cookie;
|
||||
}
|
||||
|
||||
private HashMap<String, String> getHeaders() {
|
||||
HashMap<String, String> headers = new HashMap<>();
|
||||
if (!cookie.isEmpty()) headers.put("cookie", cookie);
|
||||
headers.put("User-Agent", Utils.CHROME);
|
||||
headers.put("Referer", url);
|
||||
return headers;
|
||||
private void setHeader() {
|
||||
header.put("cookie", getCookie(ext.optString("cookie")));
|
||||
header.put("Referer", "https://www.bilibili.com");
|
||||
header.put("User-Agent", Utils.CHROME);
|
||||
}
|
||||
|
||||
private void fetchRule() throws Exception {
|
||||
if (login && !TextUtils.isEmpty(cookie)) return;
|
||||
if (header.containsKey("cookie") && header.get("cookie").length() > 0) 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;
|
||||
setHeader();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Context context, String extend) {
|
||||
try {
|
||||
this.extend = extend;
|
||||
this.header = new HashMap<>();
|
||||
fetchRule();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
@ -101,7 +94,7 @@ public class Bili extends Spider {
|
|||
String duration = extend.containsKey("duration") ? extend.get("duration") : "0";
|
||||
if (extend.containsKey("tid")) tid = tid + " " + extend.get("tid");
|
||||
String url = "https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword=" + URLEncoder.encode(tid) + "&duration=" + duration + "&page=" + pg;
|
||||
JSONObject resp = new JSONObject(OkHttp.string(url, getHeaders()));
|
||||
JSONObject resp = new JSONObject(OkHttp.string(url, header));
|
||||
JSONArray result = resp.getJSONObject("data").getJSONArray("result");
|
||||
List<Vod> list = new ArrayList<>();
|
||||
for (int i = 0; i < result.length(); ++i) {
|
||||
|
|
@ -119,12 +112,13 @@ public class Bili extends Spider {
|
|||
|
||||
@Override
|
||||
public String detailContent(List<String> ids) throws Exception {
|
||||
if (!login) checkLogin();
|
||||
String bvid = ids.get(0);
|
||||
String bvid2aidUrl = "https://api.bilibili.com/x/web-interface/archive/stat?bvid=" + bvid;
|
||||
JSONObject bvid2aidResp = new JSONObject(OkHttp.string(bvid2aidUrl, getHeaders()));
|
||||
JSONObject bvid2aidResp = new JSONObject(OkHttp.string(bvid2aidUrl, header));
|
||||
String aid = bvid2aidResp.getJSONObject("data").getLong("aid") + "";
|
||||
String detailUrl = "https://api.bilibili.com/x/web-interface/view?aid=" + aid;
|
||||
JSONObject detailResp = new JSONObject(OkHttp.string(detailUrl, getHeaders()));
|
||||
JSONObject detailResp = new JSONObject(OkHttp.string(detailUrl, header));
|
||||
JSONObject detailData = detailResp.getJSONObject("data");
|
||||
List<String> playlist = new ArrayList<>();
|
||||
JSONArray pages = detailData.getJSONArray("pages");
|
||||
|
|
@ -152,29 +146,28 @@ public class Bili extends Spider {
|
|||
|
||||
@Override
|
||||
public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
|
||||
checkLogin();
|
||||
String[] ids = id.split("\\+");
|
||||
String aid = ids[0];
|
||||
String cid = ids[1];
|
||||
String url = "https://api.bilibili.com/x/player/playurl?avid=" + aid + "&cid=" + cid + "&qn=120&fourk=1";
|
||||
JSONObject resp = new JSONObject(OkHttp.string(url, getHeaders()));
|
||||
JSONObject resp = new JSONObject(OkHttp.string(url, header));
|
||||
url = resp.getJSONObject("data").getJSONArray("durl").getJSONObject(0).getString("url");
|
||||
return Result.get().url(url).header(getHeaders()).string();
|
||||
return Result.get().url(url).header(header).string();
|
||||
}
|
||||
|
||||
private void checkLogin() throws Exception {
|
||||
if (login) return;
|
||||
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();
|
||||
login = new JSONObject(OkHttp.string("https://api.bilibili.com/x/web-interface/nav", header)).getJSONObject("data").getBoolean("isLogin");
|
||||
boolean qrCode = Prefers.getBoolean("BiliQRCode", true);
|
||||
if (!login && qrCode) getQRCode();
|
||||
}
|
||||
|
||||
private void getQRCode() throws JSONException {
|
||||
private void getQRCode() {
|
||||
try {
|
||||
String json = OkHttp.string("https://passport.bilibili.com/x/passport-login/web/qrcode/generate?source=main-mini", null);
|
||||
JSONObject data = new JSONObject(json).getJSONObject("data");
|
||||
Init.run(() -> showQRCode(data));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
private void showQRCode(JSONObject data) {
|
||||
|
|
@ -200,18 +193,22 @@ public class Bili extends Spider {
|
|||
try {
|
||||
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 json = OkHttp.string(url, getHeaders());
|
||||
String json = OkHttp.string(url, header);
|
||||
url = new JSONObject(json).getJSONObject("data").getString("url");
|
||||
if (!TextUtils.isEmpty(url)) setCookie(url);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}, 1, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
private void setCookie(String url) {
|
||||
Map<String, List<String>> respHeader = new HashMap<>();
|
||||
OkHttp.stringNoRedirect(url, header, respHeader);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String value : Objects.requireNonNull(respHeader.get("set-cookie"))) sb.append(value.split(";")[0]).append(";");
|
||||
Init.run(() -> Prefers.put("BiliQRCode", true), 5000);
|
||||
Prefers.put("BiliCookie", sb.toString());
|
||||
Init.show("請重新進入播放頁");
|
||||
loadWebView(url);
|
||||
stopService();
|
||||
}
|
||||
|
||||
|
|
@ -221,6 +218,7 @@ public class Bili extends Spider {
|
|||
}
|
||||
|
||||
private void dismiss(DialogInterface dialog) {
|
||||
Prefers.put("BiliQRCode", false);
|
||||
stopService();
|
||||
}
|
||||
|
||||
|
|
@ -230,21 +228,4 @@ public class Bili extends Spider {
|
|||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||
private void loadWebView(String url) {
|
||||
CookieManager cookieManager = CookieManager.getInstance();
|
||||
cookieManager.flush();
|
||||
cookieManager.removeAllCookies(null);
|
||||
cookieManager.removeSessionCookies(null);
|
||||
HashMap<String, List<String>> respHeader = new HashMap<>();
|
||||
OkHttp.stringNoRedirect(url, getHeaders(), respHeader);
|
||||
setCookie(respHeader);
|
||||
}
|
||||
|
||||
private void setCookie(HashMap<String, List<String>> respHeaderMap) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String value : Objects.requireNonNull(respHeaderMap.get("set-cookie"))) sb.append(value.split(";")[0]).append(";");
|
||||
Prefers.put("BiliCookie", cookie = sb.toString());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -1 +1 @@
|
|||
9f2b1ab7fabf1ef8fdb0199df87aef0d
|
||||
be4d70b1bec4f2c930972aa6685ffe19
|
||||
|
|
|
|||
Loading…
Reference in New Issue