const Router = require("koa-router") const {serach, gen_douban} = require("../util/ptgen") const axios = require("axios") const cheerio = require("cheerio"); const {addSub, getSub, delSub} = require("../util/sql/video"); // HTML页面解析 const router = new Router({ prefix: "/videoInfo" }) /** * 豆瓣搜索 */ router.get("/searchVideo", async (ctx) => { let name = ctx.query.name ctx.body = await serach(name) }) /** * 获取豆瓣描述 */ router.get("/getVideoInfo", async (ctx) => { let id = ctx.query.id if (!id) { ctx.body = "请传递id" return } ctx.body = await gen_douban(id) }) /** * 添加订阅 */ router.post("/addSubscribe", async (ctx) => { // console.log(ctx.request.body) let data = ctx.request.body try { let r = await addSub(data) if (r) { ctx.body = "订阅已存在" } else { ctx.body = "添加成功" } } catch { ctx.body = "添加失败" } }) /** * 获取订阅列表 */ router.get("/getSubscribe", async (ctx) => { ctx.body = await getSub() }) /** * 从imdb获取影视英文名称 */ router.get("/getImdbName", async (ctx) => { let res = await axios.get(ctx.query.url, { proxy: false, headers: { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67" } }) // console.log(res.data) let $ = cheerio.load(res.data) ctx.body = $("h1").text() }) /** * 删除订阅任务 */ router.get("/delSubscribe", async (ctx) => { try { await delSub(ctx.query.id) ctx.body = { code: 0, msg: "删除成功" } } catch { ctx.body = { code: 0, msg: "删除失败" } } }) module.exports = router