refactor: remove direct dependent on xml2js (#12817)

This commit is contained in:
Tony 2023-07-17 04:38:54 -09:00 committed by GitHub
parent 79d848f656
commit 1c680ae5c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 34 deletions

View File

@ -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,
};
};

View File

@ -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",

View File

@ -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':