|
| 1 | +import { Dict } from "koishi"; |
| 2 | + |
| 3 | +import { logger, runtime } from "../.."; |
| 4 | +import { BiliAPI } from "."; |
| 5 | + |
| 6 | +interface BGMType { |
| 7 | + type: string; |
| 8 | + id: string; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * 解析 ID 类型 |
| 13 | + * @param id 番剧集 ID |
| 14 | + * @returns type: ID 类型, id: 番剧集 ID |
| 15 | + */ |
| 16 | +function bgm_type_parse(id: string): BGMType { |
| 17 | + const idRegex = [ |
| 18 | + { pattern: /ep([0-9]+)/i, type: "ep" }, |
| 19 | + { pattern: /ss([0-9]+)/i, type: "ss" }, |
| 20 | + ]; |
| 21 | + |
| 22 | + let ret: BGMType; |
| 23 | + |
| 24 | + for (const rule of idRegex) { |
| 25 | + const match = id.match(rule.pattern); |
| 26 | + if (match) { |
| 27 | + ret = { |
| 28 | + type: rule.type, |
| 29 | + id: match[1], |
| 30 | + }; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return ret; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * 获取番剧集基本信息 |
| 39 | + * @param id 番剧集 ID |
| 40 | + * @returns API 内容 |
| 41 | + */ |
| 42 | +export async function fetch_web_api(id: string): Promise<BiliAPI<Dict>> { |
| 43 | + let url: string; |
| 44 | + const bgm = bgm_type_parse(id); |
| 45 | + |
| 46 | + switch (bgm.type) { |
| 47 | + case "ep": |
| 48 | + url = `https://api.bilibili.com/pgc/view/web/season?ep_id=${bgm.id}`; |
| 49 | + break; |
| 50 | + case "ss": |
| 51 | + url = `https://api.bilibili.com/pgc/view/web/season?season_id=${bgm.id}`; |
| 52 | + break; |
| 53 | + default: |
| 54 | + throw new Error(`No such bangumi type: ${bgm.type}`); |
| 55 | + } |
| 56 | + |
| 57 | + logger.debug(url); |
| 58 | + const ret = await runtime.ctx.http.get<BiliAPI<Dict>>(url, { |
| 59 | + headers: { |
| 60 | + Host: "api.bilibili.com", |
| 61 | + "User-Agent": runtime.config.userAgent, |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + ret.data = ret.result; |
| 66 | + return ret; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * 获取番剧集基本信息 |
| 71 | + * @param id 番剧集 ID |
| 72 | + * @returns API 内容 |
| 73 | + */ |
| 74 | +export async function fetch_mdid_api(id: string): Promise<BiliAPI<Dict>> { |
| 75 | + const mdInfo: BiliAPI<Dict> = await runtime.ctx.http.get<BiliAPI<Dict>>( |
| 76 | + `https://api.bilibili.com/pgc/review/user?media_id=${id.replace(/^md/, "")}`, |
| 77 | + { |
| 78 | + headers: { |
| 79 | + Host: "api.bilibili.com", |
| 80 | + "User-Agent": runtime.config.userAgent, |
| 81 | + }, |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + if (!mdInfo.result) { |
| 86 | + throw new Error("Fetch bangumi infomation via mdid failed!"); |
| 87 | + } |
| 88 | + |
| 89 | + const ret: BiliAPI<Dict> = await runtime.ctx.http.get<BiliAPI<Dict>>( |
| 90 | + `https://api.bilibili.com/pgc/view/web/season?season_id=${mdInfo.result.media.season_id}`, |
| 91 | + { |
| 92 | + headers: { |
| 93 | + Host: "api.bilibili.com", |
| 94 | + "User-Agent": runtime.config.userAgent, |
| 95 | + }, |
| 96 | + }, |
| 97 | + ); |
| 98 | + ret.data = ret.result; |
| 99 | + return ret; |
| 100 | +} |
0 commit comments