From 1c680ae5c3e4eced63a52638175f8ec4ba0c0589 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 17 Jul 2023 04:38:54 -0900 Subject: [PATCH] refactor: remove direct dependent on xml2js (#12817) --- lib/v2/wikinews/index.js | 57 ++++++++++++++++++++-------------------- package.json | 3 +-- pnpm-lock.yaml | 3 --- 3 files changed, 29 insertions(+), 34 deletions(-) diff --git a/lib/v2/wikinews/index.js b/lib/v2/wikinews/index.js index 12c390134..323c06c22 100644 --- a/lib/v2/wikinews/index.js +++ b/lib/v2/wikinews/index.js @@ -1,43 +1,42 @@ const got = require('@/utils/got'); -const { parseString } = require('xml2js'); const cheerio = require('cheerio'); const currentURL = 'https://zh.wikinews.org/wiki/Special:%E6%96%B0%E9%97%BB%E8%AE%A2%E9%98%85'; const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const resp = await got(currentURL); - const { - urlset: { url: urls }, - } = await new Promise((resolve, reject) => { - parseString(resp.data, (err, result) => { - if (err) { - reject(err); - return; - } - resolve(result); + const $ = cheerio.load(resp.data); + const urls = $('url') + .toArray() + .map((item) => { + item = $(item); + return { + title: item.find('news\\:title').text(), + pubDate: parseDate(item.find('news\\:publication_date').text()), + category: item + .find('news\\:keywords') + .text() + .split(',') + .map((item) => item.trim()), + link: item.find('loc').text(), + }; }); - }); + + const items = await Promise.all( + urls.map((item) => + ctx.cache.tryGet(item.link, async () => { + const resp = await got(item.link); + const $ = cheerio.load(resp.data); + item.description = $('#bodyContent').html(); + + return item; + }) + ) + ); ctx.state.data = { title: '最新新闻 - 维基新闻', link: currentURL, - item: await Promise.all( - urls.map(async (url) => { - const loc = url.loc[0]; - const news = url['news:news'][0]; - const description = await ctx.cache.tryGet(loc, async () => { - const resp = await got(loc); - const $ = cheerio.load(resp.data); - return $('#bodyContent').html(); - }); - return { - title: news['news:title'][0], - pubDate: parseDate(news['news:publication_date'][0]), - category: news['news:keywords'][0].split(',').map((item) => item.trim()), - link: loc, - description, - }; - }) - ), + item: items, }; }; diff --git a/package.json b/package.json index c6b429e37..4a5dc3030 100644 --- a/package.json +++ b/package.json @@ -137,8 +137,7 @@ "tiny-async-pool": "2.1.0", "tough-cookie": "4.1.3", "twitter-api-v2": "1.15.0", - "winston": "3.9.0", - "xml2js": "0.5.0" + "winston": "3.9.0" }, "devDependencies": { "@microsoft/eslint-formatter-sarif": "3.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d4c467eaf..24c0b8ca8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -190,9 +190,6 @@ dependencies: winston: specifier: 3.9.0 version: 3.9.0 - xml2js: - specifier: 0.5.0 - version: 0.5.0 devDependencies: '@microsoft/eslint-formatter-sarif':