From ccac4be5180ff7c2d81763efa7c09cf0b752ddc4 Mon Sep 17 00:00:00 2001 From: evnydd0sf Date: Tue, 4 Oct 2022 14:03:40 +0800 Subject: [PATCH] fix fetch wechat bug --- lib/v2/nua/dc.js | 2 -- lib/v2/nua/utils.js | 48 ++++++++++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/lib/v2/nua/dc.js b/lib/v2/nua/dc.js index 8a04712240..d02a7126d4 100644 --- a/lib/v2/nua/dc.js +++ b/lib/v2/nua/dc.js @@ -1,5 +1,3 @@ -// const got = require('@/utils/got'); -// const cheerio = require('cheerio'); const util = require('./utils'); module.exports = async (ctx) => { diff --git a/lib/v2/nua/utils.js b/lib/v2/nua/utils.js index 35d84ae31d..9e4fc3c84b 100644 --- a/lib/v2/nua/utils.js +++ b/lib/v2/nua/utils.js @@ -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 = '' + text + ''; + 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 = {