Skip to content

Commit

Permalink
Cache mixin key
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobby233Liu committed Sep 16, 2024
1 parent 97c347e commit 4df3286
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/utils_bilibili.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ export async function getVideoData(info, getVideoURL, dropCobaltErrs) {
);
const idType = id.startsWith("BV") ? "bvid" : "aid";
requestURL.searchParams.set(idType, id.slice("BV".length));
wbiSignURLSearchParams(requestURL, info.wbiKeys.img, info.wbiKeys.sub);
wbiSignURLSearchParams(requestURL, info.wbiKeys.mixin);

/** @type {import("fetch-cookie").FetchCookieImpl} */
const fetchCookie = info.fetchCookie;
Expand Down
27 changes: 14 additions & 13 deletions src/utils_bilibili_crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as crypto from "node:crypto";
/**
* @param {import("fetch-cookie").FetchCookieImpl} fetchCookie
*/
// FIXME: We'd want to cache this somehow
// FIXME: We'd want to cache this for a day (in BJT) long somehow
export async function wbiGetKeys(fetchCookie, referer) {
referer = referer instanceof URL ? referer : new URL(referer);
const response = await fetchCookie(
Expand Down Expand Up @@ -74,14 +74,14 @@ export async function wbiGetKeys(fetchCookie, referer) {
return path;
}

return {
img: getKeyFromFakeBfsPath(
const img = getKeyFromFakeBfsPath(
new URL(responseData.data.wbi_img.img_url).pathname,
),
sub: getKeyFromFakeBfsPath(
sub = getKeyFromFakeBfsPath(
new URL(responseData.data.wbi_img.sub_url).pathname,
),
};
mixin = wbiGenMixinKey(img, sub);
return { img, sub, mixin };
}

const WBI_MIXIN_KEY_SHUFFLE_ORDER = [
Expand All @@ -91,19 +91,20 @@ const WBI_MIXIN_KEY_SHUFFLE_ORDER = [
36, 20, 34, 44, 52,
];

function wbiGenMixinKey(img, sub) {
const fullKey = img + sub;
return WBI_MIXIN_KEY_SHUFFLE_ORDER.map((i) => fullKey.charAt(i))
.join("")
.slice(0, 32);
}

const WBI_SIGN_CHAR_FILTER_REGEX = /[!'()*]/g;

/**
* @param {URL} url
*/
export function wbiSignURLSearchParams(url, img, sub) {
const fullKey = img + sub;
const mixinKey = WBI_MIXIN_KEY_SHUFFLE_ORDER.map((i) => fullKey.charAt(i))
.join("")
.slice(0, 32);
const timestamp = Math.round(Date.now() / 1000);

url.searchParams.set("wts", timestamp);
export function wbiSignURLSearchParams(url, mixinKey) {
url.searchParams.set("wts", Math.round(Date.now() / 1000));
url.searchParams.sort();
url.searchParams.forEach((value, key) => {
url.searchParams.set(
Expand Down

0 comments on commit 4df3286

Please sign in to comment.