获取电影详细信息
(url: string)
| 133 | "accept-language": "zh-CN,zh;q=0.9", |
| 134 | "user-agent": "Mozilla/5.0 TeleBoxBot", |
| 135 | }, |
| 136 | timeout: 15000, |
| 137 | }); |
| 138 | |
| 139 | // 解析 HTML |
| 140 | const $ = cheerio.load(html); |
| 141 | |
| 142 | // 定义解析工具函数 |
| 143 | const getPanelValue = (label: string) => |
| 144 | $(`.panel-block strong:contains("${label}")`).parent().find(".value").text().trim(); |
| 145 | const getPanelLinkValue = (label: string) => |
| 146 | $(`.panel-block strong:contains("${label}")`).parent().find(".value a").first().text().trim(); |
| 147 | |
| 148 | // 提取基本信息 |
| 149 | const detail: MovieDetail = {}; |
| 150 | const director = getPanelLinkValue("導演"); |
| 151 | const maker = getPanelLinkValue("片商"); |
| 152 | const series = getPanelLinkValue("系列"); |
| 153 | const duration = getPanelValue("時長"); |
| 154 | const releaseDate = getPanelValue("日期"); |
| 155 | if (director) detail.director = director; |
| 156 | if (maker) detail.maker = maker; |
| 157 | if (series) detail.series = series; |
| 158 | if (duration) detail.duration = duration; |
| 159 | if (releaseDate) detail.releaseDate = releaseDate; |
| 160 | |
| 161 | // 提取演员信息 |
| 162 | const actorsBlock = $(`.panel-block strong:contains("演員")`).parent().find(".value"); |
| 163 | const actors = actorsBlock.find("a").map((_, el) => { |
| 164 | const $el = $(el); |
| 165 | const gender: "male" | "female" = $el.next(".symbol").hasClass("female") ? "female" : "male"; |
| 166 | return { name: $el.text().trim(), gender }; |
| 167 | }).get(); |
| 168 | if (actors.length) detail.actors = actors; |
| 169 | |
| 170 | // 提取标签信息 |
| 171 | const tagsBlock = $(`.panel-block strong:contains("類別")`).parent().find(".value"); |
| 172 | const tags = tagsBlock.find("a").map((_, el) => $(el).text().trim()).get(); |
| 173 | if (tags.length) detail.tags = tags; |
| 174 | |
| 175 | // 提取评分 |
| 176 | const sc = $(".score .value").first().text().trim(); |
| 177 | if (sc) detail.score = sc; |
| 178 | |
| 179 | // 提取预览图 |
| 180 | const previewImages = $(".preview-images .tile-item.preview-images-item") |
| 181 | .map((_, el) => $(el).attr("href") || "") |
| 182 | .get(); |
| 183 | if (previewImages.length) detail.previewImages = previewImages; |
| 184 | |
| 185 | return detail; |
| 186 | } |
| 187 | |
| 188 | // ==================== 文本处理函数 ==================== |
| 189 | /** 生成评分星级显示 */ |
| 190 | function generateRating(text: string): string { |
| 191 | const m = text.match(/(\d+(?:\.\d+)?)/); |
| 192 | if (!m) return "暂无评分"; |
no test coverage detected