mirror of
https://gh-proxy.org/https://github.com/tiajinsha/JKVideo
synced 2026-07-08 15:33:51 +08:00
1
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import axios from 'axios';
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { Platform } from 'react-native';
|
||||
import type { VideoItem, Comment, PlayUrlResponse, QRCodeInfo } from './types';
|
||||
|
||||
const BASE = 'https://api.bilibili.com';
|
||||
const PASSPORT = 'https://passport.bilibili.com';
|
||||
const isWeb = Platform.OS === 'web';
|
||||
const BASE = isWeb ? 'http://localhost:3001/bilibili-api' : 'https://api.bilibili.com';
|
||||
const PASSPORT = isWeb ? 'http://localhost:3001/bilibili-passport' : 'https://passport.bilibili.com';
|
||||
|
||||
function generateBuvid3(): string {
|
||||
const h = () => Math.floor(Math.random() * 16).toString(16);
|
||||
@@ -23,11 +25,14 @@ async function getBuvid3(): Promise<string> {
|
||||
const api = axios.create({
|
||||
baseURL: BASE,
|
||||
timeout: 10000,
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Referer': 'https://www.bilibili.com',
|
||||
'Origin': 'https://www.bilibili.com',
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
headers: isWeb ? {
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9',
|
||||
} : {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Referer': 'https://www.bilibili.com',
|
||||
'Origin': 'https://www.bilibili.com',
|
||||
'Accept': 'application/json, text/plain, */*',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9',
|
||||
},
|
||||
});
|
||||
@@ -37,9 +42,15 @@ api.interceptors.request.use(async (config) => {
|
||||
AsyncStorage.getItem('SESSDATA'),
|
||||
getBuvid3(),
|
||||
]);
|
||||
const cookies: string[] = [`buvid3=${buvid3}`];
|
||||
if (sessdata) cookies.push(`SESSDATA=${sessdata}`);
|
||||
config.headers['Cookie'] = cookies.join('; ');
|
||||
if (isWeb) {
|
||||
// Browsers block Cookie/Referer/Origin headers; relay via custom headers to proxy
|
||||
if (buvid3) config.headers['X-Buvid3'] = buvid3;
|
||||
if (sessdata) config.headers['X-Sessdata'] = sessdata;
|
||||
} else {
|
||||
const cookies: string[] = [`buvid3=${buvid3}`];
|
||||
if (sessdata) cookies.push(`SESSDATA=${sessdata}`);
|
||||
config.headers['Cookie'] = cookies.join('; ');
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
@@ -53,13 +64,20 @@ export async function getVideoDetail(bvid: string): Promise<VideoItem> {
|
||||
return res.data.data as VideoItem;
|
||||
}
|
||||
|
||||
export async function getPlayUrl(bvid: string, cid: number): Promise<PlayUrlResponse> {
|
||||
export async function getPlayUrl(bvid: string, cid: number, qn = 64): Promise<PlayUrlResponse> {
|
||||
const fnval = qn >= 80 ? 16 : 0;
|
||||
const res = await api.get('/x/player/playurl', {
|
||||
params: { bvid, cid, qn: 64, fnval: 0, platform: 'html5' },
|
||||
params: { bvid, cid, qn, fnval, platform: 'html5', fourk: 1 },
|
||||
});
|
||||
return res.data.data as PlayUrlResponse;
|
||||
}
|
||||
|
||||
export async function getUserInfo(): Promise<{ face: string; uname: string; mid: number }> {
|
||||
const res = await api.get('/x/web-interface/nav');
|
||||
const { face, uname, mid } = res.data.data;
|
||||
return { face: face ?? '', uname: uname ?? '', mid: mid ?? 0 };
|
||||
}
|
||||
|
||||
export async function getComments(aid: number, pn = 1): Promise<Comment[]> {
|
||||
const res = await api.get('/x/v2/reply', {
|
||||
params: { oid: aid, type: 1, pn, ps: 20, sort: 2 },
|
||||
@@ -68,24 +86,33 @@ export async function getComments(aid: number, pn = 1): Promise<Comment[]> {
|
||||
}
|
||||
|
||||
export async function generateQRCode(): Promise<QRCodeInfo> {
|
||||
const res = await axios.get(`${PASSPORT}/x/passport-login/web/qrcode/generate`, {
|
||||
headers: { 'Referer': 'https://www.bilibili.com' },
|
||||
});
|
||||
const headers = isWeb
|
||||
? {}
|
||||
: { 'Referer': 'https://www.bilibili.com' };
|
||||
const res = await axios.get(`${PASSPORT}/x/passport-login/web/qrcode/generate`, { headers });
|
||||
return res.data.data as QRCodeInfo;
|
||||
}
|
||||
|
||||
export async function pollQRCode(qrcode_key: string): Promise<{ code: number; cookie?: string }> {
|
||||
const headers = isWeb
|
||||
? {}
|
||||
: { 'Referer': 'https://www.bilibili.com' };
|
||||
const res = await axios.get(`${PASSPORT}/x/passport-login/web/qrcode/poll`, {
|
||||
params: { qrcode_key },
|
||||
headers: { 'Referer': 'https://www.bilibili.com' },
|
||||
headers,
|
||||
});
|
||||
const { code } = res.data.data;
|
||||
let cookie: string | undefined;
|
||||
if (code === 0) {
|
||||
const setCookie = res.headers['set-cookie'];
|
||||
const match = setCookie?.find((c: string) => c.includes('SESSDATA'));
|
||||
if (match) {
|
||||
cookie = match.split(';')[0].replace('SESSDATA=', '');
|
||||
if (isWeb) {
|
||||
// Proxy relays SESSDATA via custom response header
|
||||
cookie = res.headers['x-sessdata'] as string | undefined;
|
||||
} else {
|
||||
const setCookie = res.headers['set-cookie'];
|
||||
const match = setCookie?.find((c: string) => c.includes('SESSDATA'));
|
||||
if (match) {
|
||||
cookie = match.split(';')[0].replace('SESSDATA=', '');
|
||||
}
|
||||
}
|
||||
}
|
||||
return { code, cookie };
|
||||
|
||||
@@ -35,12 +35,18 @@ export interface Comment {
|
||||
}
|
||||
|
||||
export interface PlayUrlResponse {
|
||||
durl: Array<{
|
||||
durl?: Array<{
|
||||
url: string;
|
||||
length: number;
|
||||
size: number;
|
||||
}>;
|
||||
dash?: {
|
||||
video: Array<{ id: number; baseUrl: string; codecs: string; bandwidth: number }>;
|
||||
audio: Array<{ id: number; baseUrl: string; codecs: string; bandwidth: number }>;
|
||||
};
|
||||
quality: number;
|
||||
accept_quality: number[];
|
||||
accept_description: string[];
|
||||
}
|
||||
|
||||
export interface QRCodeInfo {
|
||||
|
||||
Reference in New Issue
Block a user