From a06ac24ab76466b2b0da31fa8d6fb53f806c7056 Mon Sep 17 00:00:00 2001 From: Wenzheng Li Date: Sun, 1 Jan 2023 10:57:05 -0800 Subject: [PATCH] =?UTF-8?q?feat(route):=20=E6=96=B0=E8=AF=AD=E4=B8=9D?= =?UTF-8?q?=E6=96=B0=E5=88=B0=E8=B5=84=E6=96=99=20(#11526)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(route): 增加新语丝 * fix regex replace to remove unwanted / of a path * add comment * resolve PR comments * resolve comments --- docs/blog.md | 6 +++ lib/v2/xys/maintainer.js | 3 ++ lib/v2/xys/new.js | 74 +++++++++++++++++++++++++++++++++++ lib/v2/xys/radar.js | 13 ++++++ lib/v2/xys/router.js | 3 ++ lib/v2/xys/templates/desc.art | 3 ++ 6 files changed, 102 insertions(+) create mode 100644 lib/v2/xys/maintainer.js create mode 100644 lib/v2/xys/new.js create mode 100644 lib/v2/xys/radar.js create mode 100644 lib/v2/xys/router.js create mode 100644 lib/v2/xys/templates/desc.art diff --git a/docs/blog.md b/docs/blog.md index 6f20147b8..79370ed06 100644 --- a/docs/blog.md +++ b/docs/blog.md @@ -320,6 +320,12 @@ username 为博主用户名,而非`xxx.hashnode.dev`中`xxx`所代表的 blog +## 新语丝 + +### 新到资料 + + + ## 优步 ### 工程技术 diff --git a/lib/v2/xys/maintainer.js b/lib/v2/xys/maintainer.js new file mode 100644 index 000000000..c47ecdcea --- /dev/null +++ b/lib/v2/xys/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/new': ['wenzhenl'], +}; diff --git a/lib/v2/xys/new.js b/lib/v2/xys/new.js new file mode 100644 index 000000000..713333d23 --- /dev/null +++ b/lib/v2/xys/new.js @@ -0,0 +1,74 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const iconv = require('iconv-lite'); +const path = require('path'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); + +module.exports = async (ctx) => { + const rootUrl = 'http://www.xys.org'; + const currentUrl = `${rootUrl}/new.html`; + + const response = await got({ + method: 'get', + url: currentUrl, + responseType: 'buffer', + }); + + // 转码 + const data = iconv.decode(response.data, 'gb2312'); + + const $ = cheerio.load(data); + + let items = $('li a') + .slice(4, ctx.query.limit ? parseInt(ctx.query.limit) : 30) + .toArray() + .map((item) => { + item = $(item); + let link = item.attr('href'); + /^https?:\/\//.test(link) || (link = rootUrl + '/' + link.replace(/^\//, '')); + let date = item.parent().text().trim().slice(0, 8); + date = parseDate(date, 'YY.MM.DD'); + return { + title: item.text(), + link, + pubDate: date, + }; + }); + + items = await Promise.all( + items + .filter((item) => !item.link.endsWith('.zip')) + .map((item) => + ctx.cache.tryGet(item.link, async () => { + const youTube = /(?:https?:\/\/)?(?:www\.)?youtu\.?be(?:\.com)?\/?.*(?:watch|embed)?(?:.*v=|v\/|\/)([\w\-_]+)&?/g; + const matchYoutube = item.link.match(youTube); + + if (matchYoutube) { + item.description = art(path.join(__dirname, 'templates/desc.art'), { youTube: item.link.slice(32) }); + } else { + const detailResponse = await got({ + method: 'get', + url: item.link, + responseType: 'buffer', + }); + + // 转码 + const detailData = iconv.decode(detailResponse.data, 'gb2312'); + + const content = cheerio.load(detailData); + + item.description = content.text().replace(/\n/g, '
\n'); + } + + return item; + }) + ) + ); + + ctx.state.data = { + title: '新语丝 - 新到资料', + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/xys/radar.js b/lib/v2/xys/radar.js new file mode 100644 index 000000000..1781addca --- /dev/null +++ b/lib/v2/xys/radar.js @@ -0,0 +1,13 @@ +module.exports = { + 'xys.org': { + _name: '新语丝', + '.': [ + { + title: '新到资料', + docs: 'https://docs.rsshub.app/blog.html#xin-yu-si', + source: ['/', '/new.html'], + target: '/xys/new', + }, + ], + }, +}; diff --git a/lib/v2/xys/router.js b/lib/v2/xys/router.js new file mode 100644 index 000000000..cb06e7c51 --- /dev/null +++ b/lib/v2/xys/router.js @@ -0,0 +1,3 @@ +module.exports = function (router) { + router.get('/new', require('./new')); +}; diff --git a/lib/v2/xys/templates/desc.art b/lib/v2/xys/templates/desc.art new file mode 100644 index 000000000..061e26ca2 --- /dev/null +++ b/lib/v2/xys/templates/desc.art @@ -0,0 +1,3 @@ +{{ if youTube }} + +{{ /if }}