From ef7e2a395c35909549c4b0670ca2212a4122c20f Mon Sep 17 00:00:00 2001 From: maple Date: Mon, 10 Dec 2018 11:04:54 +0800 Subject: [PATCH] Add facebook page (#1245) --- docs/README.md | 4 ++++ router.js | 3 +++ routes/facebook/article.js | 15 +++++++++++++++ routes/facebook/page.js | 31 +++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 routes/facebook/article.js create mode 100644 routes/facebook/page.js diff --git a/docs/README.md b/docs/README.md index 21f08a3ba..6bd41ff17 100644 --- a/docs/README.md +++ b/docs/README.md @@ -563,6 +563,10 @@ RSSHub 提供下列 API 接口: +### Facebook + + + ## 编程 ### 掘金 diff --git a/router.js b/router.js index 45d103d6d..751ee9e1e 100644 --- a/router.js +++ b/router.js @@ -904,4 +904,7 @@ router.get('/steam/news/:appids', require('./routes/steam/news')); // 扇贝 router.get('/shanbay/checkin/:id', require('./routes/shanbay/checkin')); +// Facebook +router.get('/facebook/page/:id', require('./routes/facebook/page')); + module.exports = router; diff --git a/routes/facebook/article.js b/routes/facebook/article.js new file mode 100644 index 000000000..75bb394be --- /dev/null +++ b/routes/facebook/article.js @@ -0,0 +1,15 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); + +module.exports = async (url) => { + const { data: html } = await axios.get(url); + const $ = cheerio.load(html); + const $ct = $($('#m_story_permalink_view').get(0)).find('div>div>div>div>p'); + $ct.find('br').replaceWith('\n'); + const content = $ct + .map((i, p) => $(p).text()) + .toArray() + .join('\n'); + const { searchParams: q } = new URL(url); + return { url: `https://www.facebook.com/story.php?story_fbid=${q.get('story_fbid')}&id=${q.get('id')}`, html, title: $($('h3 strong a').get(0)).text(), content }; +}; diff --git a/routes/facebook/page.js b/routes/facebook/page.js new file mode 100644 index 000000000..e805fc718 --- /dev/null +++ b/routes/facebook/page.js @@ -0,0 +1,31 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const getArticle = require('./article'); + +module.exports = async (ctx) => { + const { id } = ctx.params; + const { data: html } = await axios.get(`https://mbasic.facebook.com/${encodeURIComponent(id)}/`); + const $ = cheerio.load(html); + ctx.state.data = { + title: $('#m-timeline-cover-section h1 span').text(), + link: `https://www.facebook.com/${encodeURIComponent(id)}/`, + description: $('#sub_profile_pic_content>div>div:nth-child(3) div>span') + .find('br') + .replaceWith('\n') + .text(), + item: await Promise.all( + $('div[role=article]>div:nth-child(2)>div:nth-child(2)>span+a') + .toArray() + .map((x) => $(x).attr('href')) + .map(async (url) => { + const d = await getArticle('https://mbasic.facebook.com' + url); + return { + title: d.title, + description: d.content.replace(/\n/g, '
'), + link: d.url, + }; + }) + ), + }; + return $; +};