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