Skip to content

Commit

Permalink
feat: add 凤凰网-大风号 (DIYgod#3924)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamch authored Feb 9, 2020
1 parent a89893e commit 9ce3ead
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/new-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,16 @@ Supported sub-sites:

<Route author="emdoe" example="/plainlaw/archives" path="/plainlaw/archives"/>

## 凤凰网

### 大风号

<Route author="Jamch" example="/ifeng/feng/2583/doc" path="/ifeng/feng/:id/:type" :paramsDesc="['对应 id,可在 大风号作者页面 找到','类型,见下表']"/>

| 文章 | 视频 |
| ---- | ----- |
| doc | video |

## 观察者网-中国关怀 全球视野

### 观察者首页
Expand Down
3 changes: 3 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,9 @@ router.get('/lastfm/top/:country?', require('./routes/lastfm/top'));
router.get('/piapro/user/:pid', require('./routes/piapro/user'));
router.get('/piapro/public/:type/:tag?/:category?', require('./routes/piapro/public'));

// 凤凰网
router.get('/ifeng/feng/:id/:type', require('./routes/ifeng/feng'));

// 网易公开课
router.get('/open163/vip', require('./routes/netease/open/vip'));
router.get('/open163/latest', require('./routes/netease/open/latest'));
Expand Down
69 changes: 69 additions & 0 deletions lib/routes/ifeng/feng.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { JSDOM } = require('jsdom');
const utils = require('./utils');

module.exports = async (ctx) => {
const id = ctx.params.id;
const type = ctx.params.type;
const userResponse = await got({
method: 'get',
url: `https://shankapi.ifeng.com/winter/ishare/getAccountInfo/${id}`,
headers: {
Referer: `https://ishare.ifeng.com/mediaShare/home/${id}/do`,
},
});
const contentResponse = await got({
method: 'get',
url: `https://shankapi.ifeng.com/winter/ishare/getShareListData/${id}/${type}/1`,
headers: {
Referer: `https://ishare.ifeng.com/mediaShare/home/${id}/do`,
},
});

const userData = userResponse.data.data.data;
const contentData = contentResponse.data.data;

const mediaName = userData.weMediaName;
const items = await Promise.all(
(contentData || []).map(async (item) => {
const link = `https://feng.ifeng.com/c/${item.id}`;
const simple = {
title: item.title,
description: `<img src="${item.thumbnail}">${item.title}`,
pubDate: new Date(Date.parse(item.newsTime.replace(/-/g, '/'))),
author: mediaName,
link: link,
};

const details = await ctx.cache.tryGet(`ifeng:feng:${link}`, async () => {
const response = await got.get(link);
const $ = cheerio.load(response.data);
if (type === 'doc') {
const dom = new JSDOM(`<body><script>${$('#__INITIAL_STATE__').html()}</script></body>`, {
runScripts: 'dangerously',
});
const data = dom.window.allData;
return {
description: utils.extractDoc(data),
};
}
if (type === 'video') {
return {
description: `<img src="${item.thumbnail}"><br><video src="${$('meta[name=og\\:img_video]').attr('content')}">`,
};
}
return {
description: $('meta[name=description]').attr('content'),
};
});
return Promise.resolve(Object.assign({}, simple, details));
})
);

ctx.state.data = {
title: `大风号-${mediaName}-${type === 'doc' ? '文章' : '视频'}`,
link: `https://feng.ifeng.com/author/${id}`,
item: items,
};
};
26 changes: 26 additions & 0 deletions lib/routes/ifeng/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const extractDoc = (data) => {
if (data.docData.contentData.contentList) {
return extractContent(data.docData.contentData.contentList);
}
if (data.docData.contentData.image) {
return data.docData.contentData.image.map((item) => `<p><img src="${item.url}"><br>${item.description.replace(/\n/g, '<br>')}</p>`).join('');
}
};

const extractContent = (data) =>
(data || [])
.map((item) => {
const type = item.type;
if (type === 'video') {
return `<video src="${item.data.mobileUrl}">`;
}
if (type === 'text') {
return item.data;
}
return '';
})
.join('<br>');

module.exports = {
extractDoc,
};

0 comments on commit 9ce3ead

Please sign in to comment.