Fix futunn parser issue (#8828)

Co-authored-by: Tony <TonyRL@users.noreply.github.com>
This commit is contained in:
Jankin Wei
2022-01-22 19:56:50 +00:00
committed by GitHub
co-authored by Tony
parent 6193097708
commit 9eebd964f4
+36 -34
View File
@@ -1,56 +1,58 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const date_util = require('@/utils/date');
const timezone = require('@/utils/timezone');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const link = `https://news.futunn.com/main`;
const response = await got.get(link);
const $ = cheerio.load(response.data);
// extract json from 'window._params = {...};'
const params = $('script:not([src])').html().trim().slice(17, -1);
const contents = $('script:not([src]):contains("window._params")');
const params = contents.html().trim().slice(17, -1);
const newslist = JSON.parse(params).preList;
const out = await Promise.all(
newslist.map(async (info) => {
const title = info.title;
const itemUrl = info.url;
newslist
.filter((info) => {
const url = new URL(info.url);
if (url.hostname !== 'news.futunn.com') {
// ignore videos.futunn.com
return false;
} else if (url.searchParams.get('src') !== '3') {
// ignore src=12 redirecting to mp.weixin.qq.com
return false;
}
return true;
})
.map((info) =>
ctx.cache.tryGet(info.url, async () => {
const response = await got({
url: info.url,
method: 'get',
headers: {
Referer: link,
},
});
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const $ = cheerio.load(response.data);
const description = $('div.newsDetailBox div.main div.inner').html();
const date = $('div.timeBar').children().remove().end().text().trim();
const response = await got({
url: itemUrl,
method: 'get',
headers: {
Referer: link,
},
});
info.link = info.url;
info.description = description;
info.pubDate = timezone(parseDate(date, 'YYYY/MM/DD HH:mm'), +8);
const $ = cheerio.load(response.data);
const description = $('div.newsDetailBox div.main div.inner').html() || '原文已被删除';
const date = $('div.timeBar').text();
const single = {
title,
link: itemUrl,
description,
pubDate: date_util(date, 8),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
})
return info;
})
)
);
const outfilter = out.filter((t) => t.description !== '原文已被删除');
ctx.state.data = {
title: `富途牛牛要闻`,
link,
item: outfilter,
item: out,
};
};