fix fetch wechat bug

This commit is contained in:
evnydd0sf
2022-10-04 14:03:40 +08:00
parent 59548be382
commit ccac4be518
2 changed files with 30 additions and 20 deletions

View File

@@ -1,5 +1,3 @@
// const got = require('@/utils/got');
// const cheerio = require('cheerio');
const util = require('./utils');
module.exports = async (ctx) => {

View File

@@ -1,21 +1,32 @@
/* eslint-disable no-undef */
/* eslint-disable no-case-declarations */
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
const { finishArticleItem } = require('@/utils/wechat-mp');
const { fetchArticle } = require('@/utils/wechat-mp');
const pageType = (href) => {
if (!href.startsWith('http')) {
return 'nua';
return 'in-nua';
}
const url = new URL(href);
if (url.hostname === 'mp.weixin.qq.com') {
return 'wechat-mp';
} else if (url.hostname === 'www.nua.edu.cn') {
return 'nua';
} else {
return 'unkonwn';
return 'unknown';
}
};
function arti_link(text, href) {
a = '<a href="' + href + '">' + text + '</a>';
a.textContent = text;
a.href = href;
return a;
}
async function ProcessList(newsUrl, baseUrl, listName, listDate, webPageName) {
const result = await got(newsUrl, {
https: {
@@ -34,7 +45,7 @@ async function ProcessList(newsUrl, baseUrl, listName, listDate, webPageName) {
const type = pageType(href);
return {
link: type === 'nua' ? baseUrl + href : href,
link: type === 'in-nua' ? baseUrl + href : href,
title: item.find('a').attr('title'),
pubDate: timezone(parseDate(item.find(listDate).first().text(), 'YYYY-MM-DD'), +8),
type,
@@ -46,25 +57,26 @@ async function ProcessList(newsUrl, baseUrl, listName, listDate, webPageName) {
const ProcessFeed = async (items, artiContent, ctx) =>
await Promise.all(
items.map((item) => {
switch (item.type) {
case 'nua':
return ctx.cache.tryGet(item.link, async () => {
items.map((item) =>
ctx.cache.tryGet(item.link, async () => {
switch (item.type) {
case 'in-nua' || 'nua':
const result = await got({ url: item.link, https: { rejectUnauthorized: false } });
const $ = cheerio.load(result.data);
item.author = $('.arti_publisher').text() + ' ' + $('.arti_views').text();
item.description = $(artiContent).html();
return item;
});
case 'wechat-mp':
return finishArticleItem(ctx, items[0]);
case 'unkonwn':
item.description = `暂不支持解析该文章内容,请点击 ${'阅读原文'.link(item.link)}`;
return item;
default:
return item;
}
})
case 'wechat-mp':
return fetchArticle(ctx, item.link);
case 'unknown':
item.description = `暂不支持解析该内容,请点击 ${arti_link('原文', item.link)}`;
return item;
default:
item.description = `default`;
return item;
}
})
)
);
module.exports = {