Skip to content

Commit

Permalink
feat: isResponseAvailability()
Browse files Browse the repository at this point in the history
Create isResponseAvailability.mjs
Update request.dev.js
  • Loading branch information
VirgilClyne committed Oct 30, 2024
1 parent 63039ad commit 678d4c2
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 98 deletions.
107 changes: 107 additions & 0 deletions src/function/isResponseAvailability.mjs
Original file line number Diff line number Diff line change
@@ -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;
}
99 changes: 1 addition & 98 deletions src/request.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 678d4c2

Please sign in to comment.