diff --git a/lib/routes/novel/biquge.js b/lib/routes/novel/biquge.js index da6e01de5f0021..4a5f9d6b4021d0 100644 --- a/lib/routes/novel/biquge.js +++ b/lib/routes/novel/biquge.js @@ -2,6 +2,7 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const iconv = require('iconv-lite'); const asyncPool = require('tiny-async-pool'); +const { parseDate } = require('@/utils/parse-date'); const baseUrl = 'http://www.biquge5200.com/'; // 获取小说的最新章节列表 @@ -25,36 +26,36 @@ module.exports = async (ctx) => { .map((_, e) => ({ title: e.children[0].data, link: e.attribs.href, - pubDate: new Date((parseInt(e.attribs.href.split('/').pop().replace('.html', '')) - 15015015) * 10000).toUTCString(), + pubDate: parseDate((parseInt(e.attribs.href.split('/').pop().replace('.html', '')) - 15015015) * 10000), })) .get(); - const items = await asyncPool(3, chapter_item, async (item) => { - const cache = await ctx.cache.get(item.link); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } + const items = []; + for await (const item of asyncPool(3, chapter_item, (item) => + ctx.cache.tryGet(item, async () => { + const response = await got({ + method: 'get', + url: item.link, + responseType: 'buffer', + }); - const response = await got({ - method: 'get', - url: item.link, - responseType: 'buffer', - }); + const responseHtml = iconv.decode(response.data, 'GBK'); + const $ = cheerio.load(responseHtml); - const responseHtml = iconv.decode(response.data, 'GBK'); - const $ = cheerio.load(responseHtml); + const description = $('#content').html(); - const description = $('#content').html(); + const single = { + title: item.title, + description, + link: item.link, + pubDate: item.pubDate, + }; + return single; + }) + )) { + items.push(item); + } - const single = { - title: item.title, - description, - link: item.link, - pubDate: item.pubDate, - }; - ctx.cache.set(item.link, JSON.stringify(single)); - return Promise.resolve(single); - }); ctx.state.data = { title: `笔趣阁 ${title}`, link: `${baseUrl}${id}/`, diff --git a/lib/routes/novel/biqugeinfo.js b/lib/routes/novel/biqugeinfo.js index 805d47296104b3..16180923d92e0f 100644 --- a/lib/routes/novel/biqugeinfo.js +++ b/lib/routes/novel/biqugeinfo.js @@ -1,15 +1,15 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); const iconv = require('iconv-lite'); -const url = require('url'); const asyncPool = require('tiny-async-pool'); +const { parseDate } = require('@/utils/parse-date'); const firstDay = new Date('2020-01-01'); const baseUrl = 'http://www.biquge.info'; // 获取小说的最新章节列表 module.exports = async (ctx) => { const { id, limit = 10 } = ctx.params; // 小说id - const pageUrl = url.resolve(baseUrl, `/${id}/`); + const pageUrl = new URL(`/${id}/`, baseUrl); const response = await got({ method: 'get', @@ -28,37 +28,37 @@ module.exports = async (ctx) => { .find('a') .map((i, e) => ({ title: e.children[0].data, - link: url.resolve(pageUrl, e.attribs.href), - pubDate: new Date(firstDay.getTime() + (nItems - parseInt(limit) + i) * 1000 * 60 * 60 * 24), + link: new URL(e.attribs.href, pageUrl), + pubDate: parseDate(firstDay.getTime() + (nItems - parseInt(limit) + i) * 1000 * 60 * 60 * 24), })) .get(); - const items = await asyncPool(3, chapter_item, async (item) => { - const cache = await ctx.cache.get(item.link); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } + const items = []; + for await (const item of asyncPool(3, chapter_item, (item) => + ctx.cache.tryGet(item, async () => { + const response = await got({ + method: 'get', + url: item.link, + responseType: 'buffer', + }); - const response = await got({ - method: 'get', - url: item.link, - responseType: 'buffer', - }); + const responseHtml = iconv.decode(response.data, 'utf-8'); + const $ = cheerio.load(responseHtml); - const responseHtml = iconv.decode(response.data, 'utf-8'); - const $ = cheerio.load(responseHtml); + const description = $('#content').html(); - const description = $('#content').html(); + const single = { + title: item.title, + description, + link: item.link, + pubDate: item.pubDate, + }; + return single; + }) + )) { + items.push(item); + } - const single = { - title: item.title, - description, - link: item.link, - pubDate: item.pubDate, - }; - ctx.cache.set(item.link, JSON.stringify(single)); - return Promise.resolve(single); - }); ctx.state.data = { title: `笔趣阁 ${title}`, link: pageUrl, diff --git a/package.json b/package.json index c9f2b3183083c1..4ef1430f3a38d5 100644 --- a/package.json +++ b/package.json @@ -126,7 +126,7 @@ "simplecc-wasm": "0.1.4", "socks-proxy-agent": "6.1.1", "string-similarity": "4.0.4", - "tiny-async-pool": "1.2.0", + "tiny-async-pool": "2.0.1", "torrent-search-api": "2.1.4", "tough-cookie": "4.0.0", "tunnel": "0.0.6", diff --git a/yarn.lock b/yarn.lock index e7a08114ac4c9a..ca053d2685407d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13014,13 +13014,10 @@ timsort@^0.3.0: resolved "https://registry.yarnpkg.com/timsort/-/timsort-0.3.0.tgz#405411a8e7e6339fe64db9a234de11dc31e02bd4" integrity sha1-QFQRqOfmM5/mTbmiNN4R3DHgK9Q= -tiny-async-pool@1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/tiny-async-pool/-/tiny-async-pool-1.2.0.tgz#22132957e18f8b6020a94b390d07718fd519cc71" - integrity sha512-PY/OiSenYGBU3c1nTuP1HLKRkhKFDXsAibYI5GeHbHw2WVpt6OFzAPIRP94dGnS66Jhrkheim2CHAXUNI4XwMg== - dependencies: - semver "^5.5.0" - yaassertion "^1.0.0" +tiny-async-pool@2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/tiny-async-pool/-/tiny-async-pool-2.0.1.tgz#b91a5e9fa539f2d89b2201cd843a7341fe2710a7" + integrity sha512-QIXBQdZdFWb7sLsuS5R8+23YdUXypkplXG+C3AMkEVzUlAZ/2EWrQzfAey0l4/r8ih+F7KgbJdPWHrfOce908A== tlds@1.224.0: version "1.224.0" @@ -14482,11 +14479,6 @@ y18n@^5.0.5: resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.8.tgz#7f4934d0f7ca8c56f95314939ddcd2dd91ce1d55" integrity sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA== -yaassertion@^1.0.0: - version "1.0.2" - resolved "https://registry.yarnpkg.com/yaassertion/-/yaassertion-1.0.2.tgz#f1a90166e1cc4ad44dbb71487009ebca017e9874" - integrity sha512-sBoJBg5vTr3lOpRX0yFD+tz7wv/l2UPMFthag4HGTMPrypBRKerjjS8jiEnNMjcAEtPXjbHiKE0UwRR1W1GXBg== - yallist@^2.1.2: version "2.1.2" resolved "https://registry.yarnpkg.com/yallist/-/yallist-2.1.2.tgz#1c11f9218f076089a47dd512f93c6699a6a81d52"