Skip to content

Commit

Permalink
refactor: remove direct dependent on xml2js (DIYgod#12817)
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL authored and nczitzk committed Aug 26, 2023
1 parent 5c69090 commit 26631e7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
57 changes: 28 additions & 29 deletions lib/v2/wikinews/index.js
Original file line number Diff line number Diff line change
@@ -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,
};
};
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 26631e7

Please sign in to comment.