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