Skip to content

Commit

Permalink
feat: Add sources from different types
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovler-Young committed Jun 15, 2024
1 parent 3e07bf3 commit 5f2c520
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/lib/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,24 @@ export class BiliArchiver {
return None
}
}

async add_from_source(source_type: string, source_id: string): Promise<Array<string>> {
const url = new URL(`/archive/${source_type}/${source_id}`, this.endpoint);
console.info(`POST ${url.toString()}`);
try {
const res = await fetch(url.toString(), {
method: "POST",
});
const data = await res.json();
console.info(data);
if (data?.success) {
return data?.bvids || [];
} else {
return [];
}
} catch (e) {
console.error(e);
return [];
}
}
}
80 changes: 80 additions & 0 deletions src/lib/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ const handleBiliLink = async (ctx: Context) => {
console.info("Resolved", text);
const matches = /BV[a-zA-Z0-9]+/.exec(text);
if (!matches) {
// user space
const uidmatches = /space\.bilibili\.com\/(\d+)/.exec(text);
if (uidmatches) {
console.info("UID matches", uidmatches[1]);
handle_source(ctx, "user", uidmatches[1]);
return;
}
return;
}
console.info("Regex matches", matches[0]);
Expand Down Expand Up @@ -95,4 +102,77 @@ const handleBiliLink = async (ctx: Context) => {
})();
};

const handle_source = async (ctx: Context, source_type: string, source_id: string) => {
if (!ctx.message) {
return;
}
if (!ctx.message.text) {
return;
}
if (!ctx.chat) {
return;
}
let pending;
try {
pending = await ctx.reply("正在发送请求……", {
reply_to_message_id: ctx.message.message_id,
});
} catch (e) {
return;
}
const bvids = await api.add_from_source(source_type, source_id);
const reply_markup =
ctx.chat.type === "private" ? MARKUP.MINIAPP_PRIVATE : MARKUP.MINIAPP;
(async () => {
const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
for (let i = 0; i < 30; i++) {
await sleep(28000 + 4500 * i);
for (let bvid of bvids) {
const result = await api.check(new Bvid(bvid));
if (result.isSome()) {
try {
const url = result.unwrap().toString();
await ctx.reply(
`\u{1F389} Archive of ${bvid} was done, item uploaded to\n${url}`,
{
reply_markup: {
inline_keyboard: [
[
{
text: "View archived",
url,
},
],
],
},
}
);
} catch (e) {}
return;
}
}
}
})();
(async () => {
try {
ctx.deleteMessages([pending.message_id]);
if (bvids.length) {
await ctx.reply(
`Archive request of ${bvids.length} items was successfully sent.`,
{
reply_markup,
}
);
} else {
await ctx.reply(`Archive request failed.`, {
reply_markup,
});
}
} catch (e) {
return;
}
})();
};

export { handleBiliLink };

0 comments on commit 5f2c520

Please sign in to comment.