Files
ClassContent/历届学生/王凯/XiaoShuo/xiaoshuo-backend/controller/class/index.js
2024-09-27 02:06:13 +08:00

71 lines
1.9 KiB
JavaScript
Executable File

const { http, utils, db, config,router } = require("../../_");
// http://127.0.0.1:3000/api/json/1.0/class?class=1&page=1
router.get('/class', async function(req,res,next){
// 请求数据 and 返回dom数据
let $ = await utils.AnalyticalData({ class:req.query.class, page: req.query.page } ),
left = [],
father = null;
/**
* 存在 action 爬取每个分类下面的更多小说
*/
if(req.query.hasOwnProperty("action")){
// http://127.0.0.1:3000/api/json/1.0/class?class=2&page=1&action=more
father = $("#newscontent .l ul li")
father.each(function(){
left.push({
title: $(this).find(".s2 a").text(),
url: $(this).find(".s2 a").attr("href")
})
})
let newList = []
left.forEach(async (item) => {
let $ = await utils.AnalyticalData({ siteUrl: item.url } )
newList.push({
title: $('#maininfo #info').find('h1').text(),
imgURL: $('#sidebar #fmimg').find('img').attr('src'),
introduce: $('#maininfo #intro').children().last().text(),
author: ($('#maininfo #info').find('h1+p').text()).substring(7),
url: item.url
})
if(newList.length == 30) {
return res.json(newList)
}
})
/**
* 不存在 action 爬取每个分类小面的推荐小说
*/
} else {
// http://127.0.0.1:3000/api/json/1.0/class?class=2
father = $("#hotcontent .ll .item")
father.each(async function() {
left.push({
title: $(this).find("dl dt a").text(),
imgURL: $(this).find(".image img").attr("src"),
author: $(this).find("dl dt span").text(),
introduce: $(this).find("dl dd").text(),
url: $(this).find(".image a").attr("href")
})
})
return res.json(left)
}
})
module.exports = router