Skip to content

Commit

Permalink
chore: support favlist
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovler-Young committed Jun 15, 2024
1 parent 00d08c7 commit 8583bab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/lib/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ bot.command("help", (ctx) =>
bot.command("bili", async (ctx) => {
await handleBiliLink(ctx);
});
bot.hears(/(BV[a-zA-Z0-9]+)|(av\d+)|b23\.(tv|wtf)\/\S+|www\.bilibili\.com\/video\/\S+|space\.bilibili\.com\/\d+/i, async (ctx) => {
bot.hears(/(BV[a-zA-Z0-9]+)|(av\d+)|b23\.(tv|wtf)\/\S+|www\.bilibili\.com\/(video|medialist|list)\/\S+|space\.bilibili\.com\/\d+/i, async (ctx) => {
await handleBiliLink(ctx);
});

Expand Down
67 changes: 45 additions & 22 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) {
const listmatches = /\/(play|list)\/ml(\d+)|\/favlist\?fid=(\d+)/.exec(text);
if (listmatches) {
console.info("List matches", listmatches[2] || listmatches[3]);
handle_source(ctx, "favlist", listmatches[2] || listmatches[3]);
return;
}

const uidmatches = /space\.bilibili\.com\/(\d+)/.exec(text);
if (uidmatches) {
console.info("UID matches", uidmatches[1]);
Expand Down Expand Up @@ -113,20 +120,55 @@ const handle_source = async (ctx: Context, source_type: string, source_id: strin
}
let pending;
try {
pending = await ctx.reply("正在发送请求……", {
pending = await ctx.reply("Getting items from source……", {
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 () => {
try {
ctx.deleteMessages([pending.message_id]);
if (bvids.length) {
pending = await ctx.reply(
`Got ${bvids.length} items from source ${source_type} ${source_id}.\nSending archive requests……`,
{
reply_markup,
}
);
} else {
await ctx.reply(`Archive request failed.`, {
reply_markup,
});
return;
}
} catch (e) {
return;
}
})();

for (let bvid of bvids) {
await api.add(new Bvid(bvid));
}

const reply_markup =
ctx.chat.type === "private" ? MARKUP.MINIAPP_PRIVATE : MARKUP.MINIAPP;
(async () => {
try {
ctx.deleteMessages([pending.message_id]);
} catch (e) {
return;
} finally {
if (bvids.length) {
await ctx.reply(`All ${bvids.length} items from source ${source_type} ${source_id} were successfully sent.`, {
reply_markup,
});
}
}
})();

(async () => {
const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -158,25 +200,6 @@ const handle_source = async (ctx: Context, source_type: string, source_id: strin
}
}
})();
(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 8583bab

Please sign in to comment.