fix: 修复多维榜单 (#3842)

* feat: 添加多维经济

* feat: 修复多维榜单
This commit is contained in:
Henry Wang
2020-02-01 09:25:01 +00:00
committed by GitHub
parent 514149aef0
commit 68e063e235
5 changed files with 89 additions and 152 deletions

View File

@@ -178,15 +178,15 @@ Category 列表:
<Route author="HenryQW" example="/dwnews/yaowen/global" path="/dwnews/yaowen/:region?" :paramsDesc="['要闻地区,默认`全部`,可选地区如下']"> <Route author="HenryQW" example="/dwnews/yaowen/global" path="/dwnews/yaowen/:region?" :paramsDesc="['要闻地区,默认`全部`,可选地区如下']">
| 全部 | 国际 | 中国 | 香港 | 台湾 | | 全部 | 国际 | 中国 | 香港 | 台湾 | 经济 |
| ------ | ------ | ----- | -------- | ------ | | ------ | ------ | ----- | -------- | ------ | ------ |
| yaowen | global | china | hongkong | taiwan | | yaowen | global | china | hongkong | taiwan | jingji |
</Route> </Route>
### 新闻排行榜 ### 24 小时新闻排行榜
<Route author="HenryQW" example="/dwnews/rank/photo/7" path="/dwnews/rank/:type/:range" :paramsDesc="['榜单类型,`news`为普通新闻,`photo`为图集新闻','榜单范围(天),`1``7`']"/> <Route author="HenryQW" example="/dwnews/rank" path="/dwnews/rank"/>
## 华尔街见闻 ## 华尔街见闻

View File

@@ -823,7 +823,7 @@ router.get('/guanzhi', require('./routes/guanzhi/guanzhi'));
// 多维新闻网 // 多维新闻网
router.get('/dwnews/yaowen/:region?', require('./routes/dwnews/yaowen')); router.get('/dwnews/yaowen/:region?', require('./routes/dwnews/yaowen'));
router.get('/dwnews/rank/:type/:range', require('./routes/dwnews/rank')); router.get('/dwnews/rank/:type?/:range?', require('./routes/dwnews/rank'));
// 知晓程序 // 知晓程序
router.get('/miniapp/article/:category', require('./routes/miniapp/article')); router.get('/miniapp/article/:category', require('./routes/miniapp/article'));

View File

@@ -3,60 +3,38 @@ const cheerio = require('cheerio');
const utils = require('./utils'); const utils = require('./utils');
module.exports = async (ctx) => { module.exports = async (ctx) => {
const response = await got.get('http://www.dwnews.com/html/share/24-7topnews.js'); const link = 'https://www.dwnews.com/';
const api = 'https://prod-site-api.dwnews.com/v2/articles';
const response = await got.get(link);
const data = JSON.parse(response.data.replace('var rankingData = ', '')); const $ = cheerio.load(response.data, { xmlMode: true });
const list = JSON.parse($('#__NEXT_DATA__')[0].children[0].data).props.pageProps.initialState.home.page.sections.find((s) => s.name === '新闻排行榜 24h').items;
let list, title;
switch (ctx.params.type) {
case 'news':
if (ctx.params.range === '1') {
list = data.newsRanklist['24H'];
title = '新闻 24 小时排行';
} else {
list = data.newsRanklist['7D'];
title = '新闻 7 日排行';
}
break;
case 'photo':
if (ctx.params.range === '1') {
list = data.photoRanklist['24H'];
title = '图集 24 小时排行';
} else {
list = data.photoRanklist['7D'];
title = '图集 7 日排行';
}
break;
default:
break;
}
const out = await Promise.all( const out = await Promise.all(
list.slice(0, 10).map(async (item) => { list.map(async (item) => {
const cache = await ctx.cache.get(item.url); const url = `${api}/${item.id}`;
const cache = await ctx.cache.get(url);
if (cache) { if (cache) {
return Promise.resolve(JSON.parse(cache)); return Promise.resolve(JSON.parse(cache));
} }
const response = await got.get(item.url.replace('.html', '_all.html')); const response = await got.get(url);
const $ = cheerio.load(response.data);
const result = utils.ProcessRank($, ctx.params.type); const single = utils.ProcessFeed(response.data);
const single = { ctx.cache.set(url, JSON.stringify(single));
title: item.title,
link: item.url,
author: result.author,
description: result.description,
pubDate: result.pubDate,
};
ctx.cache.set(item.url, JSON.stringify(single));
return Promise.resolve(single); return Promise.resolve(single);
}) })
); );
if (ctx.params.type || ctx.params.range) {
out.push({
title: '由于源站改版,该源地址及参数已更新,请参考文档。',
pubDate: new Date(),
});
}
ctx.state.data = { ctx.state.data = {
title: `多维新闻网 - ${title}`, title: `多维新闻网 - 24 小时新闻排行榜`,
description: '多维新闻网—记住世界的轨迹 更需多维的视线,海外华人首选的中文门户新闻网站,及时全面的向全球海外华人更新世界各地时事政治、经济、科技、人文历史、图片、视频等新闻内容,是海外华人必上的新闻门户网站。', description: '多维新闻网—记住世界的轨迹 更需多维的视线,海外华人首选的中文门户新闻网站,及时全面的向全球海外华人更新世界各地时事政治、经济、科技、人文历史、图片、视频等新闻内容,是海外华人必上的新闻门户网站。',
link: 'http://www.dwnews.com/', link: 'http://www.dwnews.com/',
item: out, item: out,

View File

@@ -1,101 +1,65 @@
const cheerio = require('cheerio'); const ProcessFeed = (data) => {
let description = '';
const ProcessFeed = ($, item) => { const genFigure = (source) => `<figure>
let content; <img src="${source.cdnUrl}" alt="${source.caption}">
if (item.filetype === 'TJ') { <figcaption>${source.caption}</figcaption>
const images = $('div#slider_component_4_0 > img'); </figure><br>`;
$('div.captions > p').each((i, e) => { let hasBold = false;
$(images[i]).insertBefore(e);
});
content = $('div.captions'); data.blocks.forEach((b) => {
} else { switch (b.blockType) {
content = $('div.dia-lead-one'); case 'summary':
} description += `<blockquote>${b.summary.join('<br>')}</blockquote>`;
break;
case 'text':
b.htmlTokens.forEach((t) => {
t.forEach((tt) => {
switch (tt.type) {
case 'text':
if (hasBold) {
hasBold = false;
description += `${tt.content}</p>`;
} else {
description += `<p>${tt.content}</p>`;
}
break;
case 'boldText':
description = description.slice(0, -4);
description += `<b>${tt.content}</b>`;
hasBold = true;
break;
ProcessQuote($, content); default:
ProcessImage($, content); break;
Clean($, content); }
});
return content.html();
};
const ProcessRank = ($, type) => {
let content, author;
const pubDate = new Date($('div.time').text());
pubDate.setHours(pubDate.getHours() - 8);
content = $('div.dia-lead-one');
if (type === 'news') {
if (content.length === 0) {
content = $('div.container');
author = $('div.author').text();
} else {
author = $('div.nw').text();
}
ProcessQuote($, content);
ProcessImage($, content);
Clean($, content);
} else {
let images = $('div#slider_component_4_0 > img');
if ($('div#slider_component_4_0 > img').length > 0) {
$('div.captions > p').each((i, e) => {
$(images[i]).insertBefore(e);
});
content = $('div.captions');
} else {
images = $('div#component_4_0 .sd-page > img');
content = cheerio.load('<div></div>');
content = content('div');
$('.sqbtnbox > .tzbox')
.slice(1)
.each((i, e) => {
$(images[i]).appendTo(content);
$(e).appendTo(content);
}); });
} break;
} case 'image':
description += genFigure(b.image);
break;
case 'gallery':
b.images.forEach((t) => {
description += genFigure(t);
});
break;
default:
break;
}
});
return { return {
description: content.html(), title: data.title,
author, link: data.publishUrl,
pubDate, author: data.authors[0].publishName,
description,
pubDate: new Date(data.publishTime).toUTCString(),
}; };
}; };
const ProcessQuote = ($, content) => {
content.find('div.ed-top').each((i, e) => {
const quote = $(e).siblings('p');
if (quote.length > 0) {
$(`<blockquote>${quote.text()}</blockquote>`).insertBefore(e);
$(quote).remove();
$(e).remove();
}
});
};
const ProcessImage = ($, content) => {
content.find('a[href="javascript:;"]').each((i, e) => {
const img = $(e).find('img');
$(img).insertBefore(e);
$(e).remove();
});
};
const Clean = ($, content) => {
content.find('div.clear, div.badoo, div.hu-bqsm, div.sign, div.xyy, div#component_14_0').each((i, e) => {
$(e).remove();
});
};
module.exports = { module.exports = {
ProcessFeed, ProcessFeed,
ProcessRank,
}; };

View File

@@ -1,8 +1,9 @@
const got = require('@/utils/got'); const got = require('@/utils/got');
const cheerio = require('cheerio'); const utils = require('./utils');
module.exports = async (ctx) => { module.exports = async (ctx) => {
let link = 'https://www.dwnews.com'; let link = 'https://www.dwnews.com';
const api = 'https://prod-site-api.dwnews.com/v2/articles';
let region, let region,
regionid, regionid,
host, host,
@@ -27,6 +28,10 @@ module.exports = async (ctx) => {
title = `台湾${title}`; title = `台湾${title}`;
regionid = '10000119'; regionid = '10000119';
break; break;
case 'jingji':
title = `经济${title}`;
regionid = '10000123';
break;
default: default:
break; break;
} }
@@ -40,27 +45,17 @@ module.exports = async (ctx) => {
const out = await Promise.all( const out = await Promise.all(
list.data.items.map(async (item) => { list.data.items.map(async (item) => {
const cache = await ctx.cache.get(item.data.publishUrl); const url = `${api}/${item.id}`;
const cache = await ctx.cache.get(url);
if (cache) { if (cache) {
return Promise.resolve(JSON.parse(cache)); return Promise.resolve(JSON.parse(cache));
} }
const response = await got.get(encodeURI(item.data.publishUrl)); const response = await got.get(url);
const $ = cheerio.load(response.data);
const pubDate = new Date(item.data.publishTime * 1000); const single = utils.ProcessFeed(response.data);
pubDate.setHours(pubDate.getHours() - 8);
const description = $('article').html() || item.data.description; ctx.cache.set(url, JSON.stringify(single));
const single = {
title: item.data.title,
link: item.data.publishUrl,
author: item.data.authors[0].publishName,
description,
pubDate: pubDate.toUTCString(),
};
ctx.cache.set(item.data.publishUrl, JSON.stringify(single));
return Promise.resolve(single); return Promise.resolve(single);
}) })
); );
@@ -68,7 +63,7 @@ module.exports = async (ctx) => {
ctx.state.data = { ctx.state.data = {
title: `多维新闻网 - ${title}`, title: `多维新闻网 - ${title}`,
description: '多维新闻网—记住世界的轨迹 更需多维的视线,海外华人首选的中文门户新闻网站,及时全面的向全球海外华人更新世界各地时事政治、经济、科技、人文历史、图片、视频等新闻内容,是海外华人必上的新闻门户网站。', description: '多维新闻网—记住世界的轨迹 更需多维的视线,海外华人首选的中文门户新闻网站,及时全面的向全球海外华人更新世界各地时事政治、经济、科技、人文历史、图片、视频等新闻内容,是海外华人必上的新闻门户网站。',
link: link, link,
item: out, item: out,
}; };
}; };