forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove direct dependent on xml2js (DIYgod#12817)
- Loading branch information
Showing
3 changed files
with
29 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.