diff --git a/src/function/isResponseAvailability.mjs b/src/function/isResponseAvailability.mjs new file mode 100644 index 0000000..aac7d2e --- /dev/null +++ b/src/function/isResponseAvailability.mjs @@ -0,0 +1,107 @@ +import { log } from "@nsnanocat/util"; + +/** + * Determine Response Availability + * @author VirgilClyne + * @param {Object} response - Original Response Content + * @return {Boolean} is Available + */ +export default function isResponseAvailability(response = {}) { + log("☑️ Determine Response Availability", ""); + log(`statusCode: ${response.statusCode}`, `headers: ${JSON.stringify(response.headers)}`, ""); + const FORMAT = (response?.headers?.["Content-Type"] ?? response?.headers?.["content-type"])?.split(";")?.[0]; + log("🚧 Determine Response Availability", `FORMAT: ${FORMAT}`, ""); + let isAvailable = true; + switch (response?.statusCode) { + case 200: + switch (FORMAT) { + case "application/grpc": + case "application/grpc+proto": + switch (response?.headers?.["Grpc-Message"] ?? response?.headers?.["grpc-message"]) { + case "0": + isAvailable = true; + break; + case undefined: + case "": // 无内容 + switch (response?.headers?.["content-length"] ?? response?.headers?.["content-length"]) { + case undefined: + isAvailable = true; + break; + default: + if (Number.parseInt(response?.headers?.["content-length"] ?? response?.headers?.["Content-Length"]) < 1400) isAvailable = false; + break; + } + break; + case "-404": + default: + isAvailable = false; + break; + } + break; + case "text/json": + case "application/json": + switch (response?.headers?.["bili-status-code"]) { + case "0": + case undefined: { + const data = JSON.parse(response?.body).data; + switch (response?.headers?.idc) { + case "sgp001": + case "sgp002": + switch (data?.limit) { + case "": + case undefined: + isAvailable = true; + break; + default: + isAvailable = false; + break; + } + break; + case "shjd": + case undefined: + default: + switch (data?.video_info?.code) { + case 0: + default: + isAvailable = true; + break; + case undefined: + isAvailable = false; + break; + } + switch (data?.dialog?.code) { + case undefined: + isAvailable = true; + break; + case 6010001: + default: + isAvailable = false; + break; + } + break; + } + break; + } + case "-404": // 啥都木有 + case "-10403": + case "10015001": // 版权地区受限 + default: + isAvailable = false; + break; + } + break; + case "text/html": + isAvailable = true; + break; + } + break; + case 403: + case 404: + case 415: + default: + isAvailable = false; + break; + } + log("✅ Determine Response Availability", `isAvailable:${isAvailable}`, ""); + return isAvailable; +} diff --git a/src/request.dev.js b/src/request.dev.js index 8c35ae0..66b3a0b 100644 --- a/src/request.dev.js +++ b/src/request.dev.js @@ -2,6 +2,7 @@ import { $platform, Lodash as _, Storage, fetch, notification, log, logError, wa import { gRPC } from "@nsnanocat/util"; import database from "./function/database.mjs"; import setENV from "./function/setENV.mjs"; +import isResponseAvailability from "./function/isResponseAvailability.mjs"; import { WireType, UnknownFieldHandler, reflectionMergePartial, MESSAGE_TYPE, MessageType, BinaryReader, isJsonObject, typeofJsonValue, jsonWriteOptions } from "@protobuf-ts/runtime"; import { ViewReq } from "./protobuf/bilibili/app/viewunite/v1/viewunite.js"; import { PlayViewUniteReq } from "./protobuf/bilibili/app/playerunite/v1/playerunite.js"; @@ -450,104 +451,6 @@ log(`⚠ FORMAT: ${FORMAT}`, ""); }); /***************** Function *****************/ -/** - * Determine Response Availability - * @author VirgilClyne - * @param {Object} response - Original Response Content - * @return {Boolean} is Available - */ -function isResponseAvailability(response = {}) { - log("☑️ Determine Response Availability", ""); - log(`statusCode: ${response.statusCode}`, `headers: ${JSON.stringify(response.headers)}`, ""); - const FORMAT = (response?.headers?.["Content-Type"] ?? response?.headers?.["content-type"])?.split(";")?.[0]; - log("🚧 Determine Response Availability", `FORMAT: ${FORMAT}`, ""); - let isAvailable = true; - switch (response?.statusCode) { - case 200: - switch (FORMAT) { - case "application/grpc": - case "application/grpc+proto": - switch (response?.headers?.["Grpc-Message"] ?? response?.headers?.["grpc-message"]) { - case "0": - isAvailable = true; - break; - case undefined: - if (Number.parseInt(response?.headers?.["content-length"] ?? response?.headers?.["Content-Length"]) < 1200) isAvailable = false; - else isAvailable = true; - break; - case "-404": - default: - isAvailable = false; - break; - } - break; - case "text/json": - case "application/json": - switch (response?.headers?.["bili-status-code"]) { - case "0": - case undefined: { - const data = JSON.parse(response?.body).data; - switch (response?.headers?.idc) { - case "sgp001": - case "sgp002": - switch (data?.limit) { - case "": - case undefined: - isAvailable = true; - break; - default: - isAvailable = false; - break; - } - break; - case "shjd": - case undefined: - default: - switch (data?.video_info?.code) { - case 0: - default: - isAvailable = true; - break; - case undefined: - isAvailable = false; - break; - } - switch (data?.dialog?.code) { - case undefined: - isAvailable = true; - break; - case 6010001: - default: - isAvailable = false; - break; - } - break; - } - break; - } - case "-404": // 啥都木有 - case "-10403": - case "10015001": // 版权地区受限 - default: - isAvailable = false; - break; - } - break; - case "text/html": - isAvailable = true; - break; - } - break; - case 403: - case 404: - case 415: - default: - isAvailable = false; - break; - } - log("✅ Determine Response Availability", `isAvailable:${isAvailable}`, ""); - return isAvailable; -} /** * Fetch