| | |
| | | * @param {*} book |
| | | */ |
| | | async function getBookDetailPageUrl(book) { |
| | | const url = `https://libgen.vg/index.php?req=${book.title}&columns%5B%5D=t&topics%5B%5D=f&res=25&filesuns=all`; |
| | | const url = `https://libgen.rs/fiction/?q=${book.title.replace(/ /g,'+')}&criteria=title&language=&format=`; |
| | | return await retry(async () => { |
| | | const resp = await myAxios.get(url, { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36' } }) |
| | | const group = /.*href="(edition.php\?id=\d+)".*/g.exec(resp.data); |
| | | // const html = cheerio.load(resp.data); |
| | | // const url = html('body > table > tbody > tr:nth-child(1) > td:nth-child(6) > ul > li:nth-child(1) > a')?.attr('href') ?? ''; |
| | | // return url; |
| | | const group = /.*href="(http:\/\/library.lol\/fiction\/[0-9a-zA-Z]+)".*/g.exec(resp.data); |
| | | if (group) { |
| | | return `https://libgen.vg/${group[1]}`; |
| | | return `${group[1]}`; |
| | | } else { |
| | | return '' |
| | | } |
| | |
| | | async function getDownloadUrl(book, url) { |
| | | return await retry(async () => { |
| | | const resp = await myAxios.get(url, { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36' } }); |
| | | const group = /.*href="(get.php\?md5=[0-9a-f]+.*)".*/g.exec(resp.data); |
| | | const group = /.*href="(\S+)".*>GET<.*/g.exec(resp.data); |
| | | if (group) { |
| | | return `https://libgen.vg/${group[1]}`; |
| | | return `${group[1]}`; |
| | | } else { |
| | | return ''; |
| | | } |
| | |
| | | let ext = response.headers['content-disposition'].split('filename=')[1].split('.').pop() ?? ''; |
| | | ext = ext.substring(0, ext.length - 1); |
| | | |
| | | const filepath = `./downloads/${book.id} ${book.isbn}.${ext}`; |
| | | const filepath = `./downloads/${book.id}.${ext}`; |
| | | book.url = url; |
| | | if (fs.existsSync(filepath)) { |
| | | book.state = `下载完成`; |
| | |
| | | return; |
| | | } |
| | | const stream = response.data; |
| | | const _filepath = `./downloads/${book.id} ${book.isbn}.${ext}`; |
| | | const _filepath = `./downloads/${book.id}.${ext}`; |
| | | const out = fs.createWriteStream(_filepath); |
| | | stream.pipe(out); |
| | | stream.on("end", async () => { |
| | |
| | | book.format = ext; |
| | | book.file = filepath; |
| | | book.url = url; |
| | | book.pages = await getPdfPages(filepath).catch(e => 0); |
| | | // book.pages = await getPdfPages(filepath).catch(e => 0); |
| | | resolve(true); |
| | | }); |
| | | stream.on("error", (err) => { |
| | |
| | | // 等一段时间再打开详情页 |
| | | sleep(getRandomNumber(500, 1000)); |
| | | // 打开详情页,并获取下载链接 |
| | | const filePageUrl = await openBookDetailPage(book, detailPageUrl); |
| | | if (!filePageUrl) { |
| | | console.log(`没有文件: ${book.id} ${book.title}`); |
| | | continue; |
| | | } |
| | | const url = await getDownloadUrl(book, filePageUrl); |
| | | // const filePageUrl = await openBookDetailPage(book, detailPageUrl); |
| | | // if (!filePageUrl) { |
| | | // console.log(`没有文件: ${book.id} ${book.title}`); |
| | | // continue; |
| | | // } |
| | | const url = await getDownloadUrl(book, detailPageUrl); |
| | | if (!url) { |
| | | console.log(`没有文件: ${book.id} ${book.title}`); |
| | | continue; |