mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-13 08:48:59 +08:00
修复JSON重复parse等问题 (#641)
This commit is contained in:
+7
-13
@@ -1,17 +1,11 @@
|
||||
const axios = require('../../utils/axios');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const response = await axios({
|
||||
method: 'get',
|
||||
url: 'https://xkcd.com/info.0.json',
|
||||
headers: {
|
||||
Referer: 'https://www.xkcd.com',
|
||||
},
|
||||
});
|
||||
const response = await axios.get('https://xkcd.com/info.0.json');
|
||||
|
||||
const data = JSON.parse(response.data);
|
||||
const result = response.data;
|
||||
const postTime = new Date();
|
||||
postTime.setFullYear(parseInt(data.year), parseInt(data.month) - 1, parseInt(data.day));
|
||||
postTime.setFullYear(parseInt(result.year), parseInt(result.month) - 1, parseInt(result.day));
|
||||
postTime.setHours(0, 0, 0, 0); // 无法获取精确时间
|
||||
|
||||
ctx.state.data = {
|
||||
@@ -20,11 +14,11 @@ module.exports = async (ctx) => {
|
||||
description: 'A webcomic of romance, sarcasm, math, and language.',
|
||||
item: [
|
||||
{
|
||||
title: data.title,
|
||||
description: `<img src="${data.img}"><br />${data.alt}`,
|
||||
title: result.title,
|
||||
description: `<img src="${result.img}"><br />${result.alt}`,
|
||||
pubDate: postTime.toUTCString(),
|
||||
link: 'https://www.xkcd.com/' + data.num,
|
||||
guid: data.num,
|
||||
link: 'https://www.xkcd.com/' + result.num,
|
||||
guid: result.num,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
+19
-12
@@ -4,27 +4,34 @@ const cheerio = require('cheerio');
|
||||
module.exports = async (ctx) => {
|
||||
const response = await axios({
|
||||
method: 'get',
|
||||
url: 'https://what-if.xkcd.com/',
|
||||
url: 'https://what-if.xkcd.com/feed.atom',
|
||||
headers: {
|
||||
Referer: 'https://what-if.xkcd.com/',
|
||||
Referer: 'https://xkcd.com/',
|
||||
},
|
||||
});
|
||||
|
||||
const data = response.data;
|
||||
const $ = cheerio.load(data);
|
||||
const $ = cheerio.load(data, {
|
||||
xmlMode: true,
|
||||
});
|
||||
const list = $('entry');
|
||||
|
||||
ctx.state.data = {
|
||||
title: 'what-if',
|
||||
link: 'https://what-if.xkcd.com/',
|
||||
description: "xkcd' article",
|
||||
item: [
|
||||
{
|
||||
title: $('h1').text(),
|
||||
description: $('#question').text(),
|
||||
pubDate: '', // 发表日期无法获取
|
||||
guid: '',
|
||||
link: 'https:' + $('article a:first').attr('href'),
|
||||
},
|
||||
],
|
||||
item:
|
||||
list &&
|
||||
list
|
||||
.map((index, item) => {
|
||||
item = $(item);
|
||||
return {
|
||||
title: item.find('title').text(),
|
||||
description: item.find('content').text(),
|
||||
pubDate: item.find('updated').text(),
|
||||
link: item.find('id').text(),
|
||||
};
|
||||
})
|
||||
.get(),
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user