Skip to content

Commit

Permalink
feat(src/search-app/hono-app): add: 4399 用户名查询,哔哩哔哩用户查询
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyungent committed Apr 12, 2024
1 parent 289439f commit d835420
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/search-app/hono-app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ dist
package-lock.json
yarn.lock
pnpm-lock.yaml
bun.lockb
bun.lockb



1 change: 1 addition & 0 deletions src/search-app/hono-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"deploy": "wrangler deploy --minify src/index.ts"
},
"dependencies": {
"@hono/swagger-ui": "^0.2.1",
"hono": "^4.2.3"
},
"devDependencies": {
Expand Down
68 changes: 62 additions & 6 deletions src/search-app/hono-app/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
import { Hono } from 'hono'
import { Hono } from "hono";
import { cors } from "hono/cors";
import { env } from "hono/adapter";
import { swaggerUI } from "@hono/swagger-ui";
import game4399account from "./plugins/game4399account";
import bilibiliUser from "./plugins/bilibili-user";

const app = new Hono()
const app = new Hono();
// https://hono.dev/middleware/builtin/cors
app.use("/*", cors());
// https://github.com/honojs/middleware/tree/main/packages/swagger-ui
// Use the middleware to serve Swagger UI at /ui
// app.get('/ui', swaggerUI({ url: '/doc' }))

app.get('/', (c) => {
return c.text('Hello Hono!')
})
app.get("/", (c) => {
return c.json({
name: "搜索 API",
version: "1.0.0",
plugins: [
{
name: "4399 用户名查询",
url: "/api/plugins/game4399account?q={q}",
test: "/api/plugins/game4399account?q=21434",
},
{
name: "哔哩哔哩用户查询",
url: "/api/plugins/bilibili-user?q={q}",
test: "/api/plugins/bilibili-user?q=测试用户",
},
],
});
});

export default app
app.get("/api/plugins/game4399account", async (c) => {
const q = c.req.query("q");
if (!q) {
return c.json({
code: -1,
message: "参数错误",
data: [],
});
}
const res = await game4399account(q);

return c.json(res);
});

app.get("/api/plugins/bilibili-user", async (c) => {
const q = c.req.query("q");
if (!q) {
return c.json({
code: -1,
message: "参数错误",
data: [],
});
}
const { PLUGINS_BILIBILI_USER_COOKIE } = env<{
PLUGINS_BILIBILI_USER_COOKIE: string;
}>(c);
const res = await bilibiliUser(q, PLUGINS_BILIBILI_USER_COOKIE);

return c.json(res);
});

export default app;
121 changes: 121 additions & 0 deletions src/search-app/hono-app/src/plugins/bilibili-user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import type { BaseResponseModel } from "../types/baseReponseModel";


interface BilibiliUserDataItemModel {
/**
* 用户名
*/
uname: string;
/**
* 头像
*/
upic: string;

/**
* 签名
*/
usign: string;

/**
* 投稿视频个数
*/
videos: number;

/**
* 粉丝个数
*/
fans: number;

/**
* 等级
*/
level: number;

/**
* https://space.bilibili.com/25057459
*/
url: string;
}

export default async function query(
query: string,
cookie: string,
): Promise<BaseResponseModel<BilibiliUserDataItemModel>> {
const responseModel: BaseResponseModel<BilibiliUserDataItemModel> = {
code: -1,
message: "失败",
data: [],
};
try {
const res = await fetch(
`https://api.bilibili.com/x/web-interface/wbi/search/type?` +
new URLSearchParams({
category_id: "",
search_type: "bili_user",
ad_resource: "5646",
__refresh__: "true",
page: "1",
page_size: "36",
platform: "pc",
highlight: "1",
single_column: "0",
keyword: query.trim(),
qv_id: "drhuMbLFmLakkilY2NVwvZlCArwuxzso",
source_tag: "3",
order_sort: "0",
user_type: "0",
dynamic_offset: "0",
web_location: "1430654",
w_rid: "c69cf3c4599ba1fcd0429dcfad83f2e8",
wts: "1712952282",
}),
{
method: "GET",
headers: {
"Content-Type": "application/json",
"User-Agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36`,
Referer:
`https://search.bilibili.com/upuser?` +
new URLSearchParams({
keyword: query.trim(),
search_source: "5",
}),
Accept: "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
Origin: "https://search.bilibili.com",
"Sec-Ch-Ua": `"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"`,
Cookie: `${cookie}`
},
}
);

console.log("res", res);

if (res.ok) {
responseModel.code = 1;
responseModel.message = "成功";
const resJson: any = await res.json();
if (
resJson.code === 0 &&
resJson.data?.result &&
resJson.data?.result.length > 0
) {
resJson.data.result.forEach((item: any) => {
responseModel.data.push({
uname: item.uname,
upic: `${item.upic.replace("//", "http://")}`,
usign: item.usign,
videos: item.videos,
fans: item.fans,
level: item.level,
url: `https://space.bilibili.com/${item.mid}`,
});
});
}
}
} catch (error) {
console.log("error", error);
}

return responseModel;
}
89 changes: 89 additions & 0 deletions src/search-app/hono-app/src/plugins/game4399account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { BaseResponseModel } from "../types/baseReponseModel";

interface Gamme4399AccountDataItemModel {
/**
* 4399账号
*/
account: string;
/**
* 此账号在4399平台不存在
* 此账号在4399平台已存在
*/
message: string;

/**
* https://www.4399.com
*/
url: string;
}

/**
* https://u.4399.com/anquan/pwd/?from=u&sfrom=
* @param query
* @returns
*/
export default async function query(
query: string
): Promise<BaseResponseModel<Gamme4399AccountDataItemModel>> {
const responseModel: BaseResponseModel<Gamme4399AccountDataItemModel> = {
code: -1,
message: "失败",
data: [],
};
try {
// 创建一个新的 Date 对象
const date = new Date();
// 获得以毫秒为单位的时间戳
const timeStamp = date.getTime();
const res = await fetch(
`https://u.4399.com/anquan/pwd/?` +
new URLSearchParams({
_c: "verify",
_a: "userAllowFind",
userName: query.trim(),
jsoncallback: "jQuery110206197225005545752_1712947018019",
_: `${timeStamp}`,
}),
{
method: "GET",
headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
Referer: "https://u.4399.com/anquan/pwd/?from=u&sfrom=",
"Content-Type": "application/x-www-form-urlencoded",
Accept: `text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01`,
"X-Requested-With": "XMLHttpRequest",
"Sec-Ch-Ua-Platform": `"Windows"`,
},
}
);

if (res.ok) {
responseModel.code = 1;
responseModel.message = "成功";
const text = await res.text();
// decoded: jQuery110206197225005545752_1712947018007({status: false, msg: "此账号在4399平台不存在"})
// jQuery110206197225005545752_1712947018007({"status":false,"msg":"\u6b64\u8d26\u53f7\u57284399\u5e73\u53f0\u4e0d\u5b58\u5728"})
// jQuery110206197225005545752_1712947018007({"status":true})
if (text.indexOf(`"status":true`) > 0) {
responseModel.message = "成功";
responseModel.data = [
{
account: query.trim(),
message: "此账号在4399平台已存在",
url: "https://www.4399.com",
},
];
} else if (text.indexOf(`"status":false`) > 0) {
responseModel.message = `成功: 此账号 (${query.trim()}) 在4399平台不存在`;
// 不存在就不需要展示列表了
responseModel.data = [];
} else {
responseModel.code = -1;
responseModel.message = "未知错误";
}
}
} catch (error) {}

return responseModel;
}
7 changes: 7 additions & 0 deletions src/search-app/hono-app/src/types/baseReponseModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export interface BaseResponseModel<TDataItem> {
code: number
message: string
data: TDataItem[]
}

5 changes: 4 additions & 1 deletion src/search-app/hono-app/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ compatibility_date = "2023-12-01"

# [vars]
# MY_VAR = "my-variable"
PLUGINS_BILIBILI_USER_COOKIE = "buvid4=98FCD548-69A9-1455-69CF-6A64B36BC4A000607-024031218-HyYFmR0Ivh2P1Xfp2yjdqg%3D%3D; buvid3=C63D509D-FF05-8A4E-8574-3343C6A9FBD216132infoc; b_nut=1712955316"


# [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
Expand All @@ -18,4 +20,5 @@ compatibility_date = "2023-12-01"
# database_id = ""

# [ai]
# binding = "AI"
# binding = "AI"

0 comments on commit d835420

Please sign in to comment.