From 2a9b9ccd670c7f70d4dc4d463c913897b898e7d1 Mon Sep 17 00:00:00 2001 From: Akari <60416767+MotooriKashin@users.noreply.github.com> Date: Sun, 4 Aug 2024 17:34:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20access=5Fkey=20=E9=89=B4?= =?UTF-8?q?=E6=9D=83=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../x/passport-tv-login/h5/qrcode/confirm.ts | 25 ++++++++++ .../x/passport-tv-login/qrcode/auth_code.ts | 24 ++++++++++ .../x/passport-tv-login/qrcode/poll.ts | 46 +++++++++++++++++++ src/io/sign/index.ts | 24 ++++++---- src/main/bilibili/desc/index.ts | 2 +- src/main/bilibili/home/style/video-info.css | 2 +- 6 files changed, 112 insertions(+), 11 deletions(-) create mode 100644 src/io/com/bilibili/passport/x/passport-tv-login/h5/qrcode/confirm.ts create mode 100644 src/io/com/bilibili/passport/x/passport-tv-login/qrcode/auth_code.ts create mode 100644 src/io/com/bilibili/passport/x/passport-tv-login/qrcode/poll.ts diff --git a/src/io/com/bilibili/passport/x/passport-tv-login/h5/qrcode/confirm.ts b/src/io/com/bilibili/passport/x/passport-tv-login/h5/qrcode/confirm.ts new file mode 100644 index 0000000..543b008 --- /dev/null +++ b/src/io/com/bilibili/passport/x/passport-tv-login/h5/qrcode/confirm.ts @@ -0,0 +1,25 @@ +import { Passport } from "../../../.."; + +/** + * 扫码登陆确认 + * + * @param auth_code 扫码登录秘钥 + * @param csrf cookie密钥,来自 cookie.bili_jct + * @returns 重定向url + */ +export async function confirm( + auth_code: string, + csrf: string, +) { + const response = await fetch(Passport + '/x/passport-tv-login/h5/qrcode/confirm', { + method: 'POST', + headers: new Headers({ + 'Content-type': 'application/x-www-form-urlencoded' + }), + body: new URLSearchParams({ + auth_code, + csrf, + }), + }); + return (await response.json()).data.gourl; +} \ No newline at end of file diff --git a/src/io/com/bilibili/passport/x/passport-tv-login/qrcode/auth_code.ts b/src/io/com/bilibili/passport/x/passport-tv-login/qrcode/auth_code.ts new file mode 100644 index 0000000..3e5f977 --- /dev/null +++ b/src/io/com/bilibili/passport/x/passport-tv-login/qrcode/auth_code.ts @@ -0,0 +1,24 @@ +import { Passport } from "../../.."; +import { sign } from "../../../../../../sign"; + +/** TV端扫码登录 */ +export async function auth_code() { + const response = await fetch(Passport + '/x/passport-tv-login/qrcode/auth_code', { + method: 'POST', + headers: new Headers({ + 'Content-type': 'application/x-www-form-urlencoded' + }), + body: sign(new URLSearchParams({ + local_id: 0, + ts: (Date.now() / 1000).toFixed(0), + }), '27eb53fc9058f8c3'), + }); + return (await response.json()).data; +} + +interface IAuthCode { + /** 二维码内容 url */ + url: string; + /** 扫码登录秘钥 */ + auth_code: string; +} \ No newline at end of file diff --git a/src/io/com/bilibili/passport/x/passport-tv-login/qrcode/poll.ts b/src/io/com/bilibili/passport/x/passport-tv-login/qrcode/poll.ts new file mode 100644 index 0000000..b6988a7 --- /dev/null +++ b/src/io/com/bilibili/passport/x/passport-tv-login/qrcode/poll.ts @@ -0,0 +1,46 @@ +import { Passport } from "../../.."; +import { sign } from "../../../../../../sign"; + +/** 扫码登录结果查询 */ +export async function poll( + auth_code: string, +) { + const response = await fetch(Passport + '/x/passport-tv-login/qrcode/poll', { + method: 'POST', + headers: new Headers({ + 'Content-type': 'application/x-www-form-urlencoded' + }), + body: sign(new URLSearchParams({ + auth_code, + local_id: 0, + ts: (Date.now() / 1000).toFixed(0), + }), '27eb53fc9058f8c3'), + }); + return (await response.json()); +} + +interface IPoll { + /** + * 返回值 + * | 0 | -3 | -400 | -404 | 86038 | 86039 | 86090 | + * | :-: | :-: | :-: | :-: | :-: | :-: | :-: | + * | 成功 | API校验密匙错误 | 请求错误 | 啥都木有 | 二维码已失效 | 二维码尚未确认 | 二维码已扫码未确认 | + */ + code: number; + /** 错误信息 */ + message: string; + ttl: number; + /** 当且仅当 code===0 时登录成功 */ + data?: IPollData; +} + +interface IPollData { + /** 登录用户mid */ + mid: number; + /** APP登录Token */ + access_token: string; + /** APP刷新Token */ + refresh_token: string; + /** 有效时间(单位:秒) */ + expires_in: number; +} \ No newline at end of file diff --git a/src/io/sign/index.ts b/src/io/sign/index.ts index f2c88f9..6faf316 100644 --- a/src/io/sign/index.ts +++ b/src/io/sign/index.ts @@ -8,14 +8,20 @@ import { APP_KEY } from "./key_secret"; * @param appkey 签名所属平台的appkey,参看{@link APP_KEY} * @returns 已签名的url */ -export function sign( - url: string | URL, +export function sign( + url: T, appkey: keyof typeof APP_KEY -) { - const n_url = new URL(url); - n_url.searchParams.set('appkey', appkey); - n_url.searchParams.delete('sign'); - n_url.searchParams.sort(); - n_url.searchParams.set('sign', crypto.md5.toHex(n_url.searchParams.toString() + APP_KEY[appkey])); - return n_url; +): T { + if (url instanceof URLSearchParams) { + url.set('appkey', appkey); + url.delete('sign'); + url.sort(); + url.set('sign', crypto.md5.toHex(url.toString() + APP_KEY[appkey])); + return url; + } + url.searchParams.set('appkey', appkey); + url.searchParams.delete('sign'); + url.searchParams.sort(); + url.searchParams.set('sign', crypto.md5.toHex(url.searchParams.toString() + APP_KEY[appkey])); + return url; } \ No newline at end of file diff --git a/src/main/bilibili/desc/index.ts b/src/main/bilibili/desc/index.ts index 298db65..50d58c1 100644 --- a/src/main/bilibili/desc/index.ts +++ b/src/main/bilibili/desc/index.ts @@ -170,7 +170,7 @@ export class Desc extends HTMLDivElement { } }); this.bangumi(season, this.#epid).finally(async () => { - if (this.#ssid) { + if (this.#ssid && !this.#epid) { const d = await pgcSection(this.#ssid); const eps = d.main_section.episodes.concat(...d.section.map(d => d.episodes)); const ep = this.#epid ? eps.find(d => d.id === this.#epid) : eps[0]; diff --git a/src/main/bilibili/home/style/video-info.css b/src/main/bilibili/home/style/video-info.css index 25196da..bdc7f07 100644 --- a/src/main/bilibili/home/style/video-info.css +++ b/src/main/bilibili/home/style/video-info.css @@ -66,7 +66,7 @@ .v-data { display: flex; - column-gap: 2em; + justify-content: space-between; >span { white-space: nowrap;