fix: 澎湃 DOM 更新 (#4992)

This commit is contained in:
Henry Wang 2020-06-15 04:38:30 +01:00 committed by GitHub
parent be3d878eb1
commit 9ff227f471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 16 deletions

View File

@ -2,7 +2,7 @@ const utils = require('./utils');
module.exports = async (ctx) => {
const { id } = ctx.params;
const link = `https://www.thepaper.cn/channel_${id}`;
const link = `https://m.thepaper.cn/channel_${id}`;
const items = await utils.ProcessFeed(link, ctx);
ctx.state.data = {

View File

@ -1,5 +1,5 @@
const cheerio = require('cheerio');
const dayjs = require('dayjs');
const date = require('@/utils/date');
const got = require('@/utils/got');
module.exports = {
@ -7,32 +7,29 @@ module.exports = {
const res = await got.get(link);
const $ = cheerio.load(res.data);
const list = $('#mainContent .news_li').slice(0, 10).get();
const list = $('.t_news').slice(0, 10).get();
return await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const itemUrlID = $(item).find('.news_tu a').attr('href');
const itemUrl = `https://www.thepaper.cn/${itemUrlID}`;
const itemUrlID = $(item).find('.news_tit02 a').attr('href');
const itemUrl = `https://m.thepaper.cn/${itemUrlID}`;
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const res = await got.get(itemUrl);
const content = cheerio.load(res.data);
const serverOffset = new Date().getTimezoneOffset() / 60;
const author = content('.newscontent .news_about p').slice(0, 1).text();
content('.newscontent .news_about p span').remove();
const pubDateStr = content('.newscontent .news_about p').slice(1, 2).text().trim();
content('.contheight').replaceWith('<br>');
const single = {
title: content('.newscontent .news_title').text(),
guid: itemUrl,
title: content('h1.t_newsinfo').text(),
link: itemUrl,
description: content('.newscontent .news_txt').html(),
pubDate: dayjs(pubDateStr)
.add(-8 - serverOffset, 'hour')
.toISOString(),
author: author,
description: $(content('.news_part_limit div')[0]).html(),
pubDate: date($(content('.about_news')[1]).text().trim()),
author: $(content('.about_news')[0]).text(),
};
return Promise.resolve(single);
})