更新书籍章节列表时,先判断数据是否已经存在

This commit is contained in:
zhl 2019-04-19 18:58:35 +08:00
parent 29df061517
commit 027fbd0c3c
2 changed files with 30 additions and 13 deletions

View File

@ -8,3 +8,4 @@ sites/hoh8.js
## 20190403 全民小说抓取 ## 20190403 全民小说抓取
sites/book.js sites/book.js
sites/bookChapter.js

View File

@ -158,22 +158,33 @@ const parseOneBook = async (book) => {
// 有些源无法获取到章节列表, 所以挨个获取,直到获取到数据 // 有些源无法获取到章节列表, 所以挨个获取,直到获取到数据
if (book.data.sites && book.data.sites.length > 0) { if (book.data.sites && book.data.sites.length > 0) {
for(let site of book.data.sites) { for(let site of book.data.sites) {
let chapterListData = await bookChapterList(bookId, site.site); try {
if (chapterListData.code === 1) { let chapterListData = await bookChapterList(bookId, site.site);
hasChapter = true; if (chapterListData.code === 1) {
chapterListData.book_id = bookId; hasChapter = true;
chapterListData.site = site.site; chapterListData.book_id = bookId;
let chapterRecord = new BookChapter({ chapterListData.site = site.site;
data: chapterListData, const chapterExist = await BookChapter.findOne({'data.book_id': bookId, 'data.site': site.site});
type: 'book_chapter' if (!chapterExist) {
}); let chapterRecord = new BookChapter({
await chapterRecord.save(); data: chapterListData,
type: 'book_chapter'
});
await chapterRecord.save();
}
}
} catch (err) {
console.log(err);
} }
} }
} }
if (hasChapter) { if (hasChapter) {
book.status = 2; try {
await book.save(); book.status = 2;
await book.save();
} catch (err) {
console.log(err);
}
} }
// let bar = multi.newBar(' '+book.data.name+' [:bar] :percent :etas', { // let bar = multi.newBar(' '+book.data.name+' [:bar] :percent :etas', {
// complete: '=', // complete: '=',
@ -216,6 +227,11 @@ export default {
// 获取所有分类下的数据列表 // 获取所有分类下的数据列表
// await parseAllCategory(2, 0); // await parseAllCategory(2, 0);
// 获取所有数据的详情 // 获取所有数据的详情
await parseAllBookList(1, 0); for(let i = 0; i < 14; i++){
await parseAllBookList(1, i);
}
for(let i = 0; i < 9; i++){
await parseAllBookList(2, i);
}
} }
} }