mtfosbot/libs/facebook-pageparse/index.js

66 lines
1.5 KiB
JavaScript
Raw Normal View History

const request = require('request')
const cheerio = require('cheerio')
const getLastPost = async (pageid = '') => {
if (typeof pageid !== 'string' || pageid.trim().length === 0) return null
pageid = pageid.trim()
let page = await new Promise((resolve) => {
request({
baseUrl: 'https://facebook.com',
url: `/${pageid}/posts`,
method: 'get',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0'
}
}, (err, res, body) => {
if (err) {
resolve(null)
return
}
if (body && typeof body !== 'string' && !(body instanceof String)) {
resolve(null)
return
}
resolve(body)
})
})
if (page === null) return null
let $ = cheerio.load(page, {
lowerCaseTags: true,
lowerCaseAttributeNames: true
})
let posts = []
$('div.userContentWrapper').each((i, el) => {
let t = cheerio.load(el)
let timeEl = t('abbr')
let time = timeEl.attr('data-utime')
let link = timeEl.parent().attr('href')
let p = t('div.userContent div.text_exposed_root')
let txt = p.text()
let id = p.attr('id')
if (!time || !link || !txt || !id) return
let tmp = {
txt,
id,
link,
time
}
posts.push(tmp)
})
if (posts.length === 0) return
posts.sort((a, b) => {
return b.time - a.time
})
let post = posts[0]
post.link = `https://facebook.com/${post.link.replace(/^\//, '')}`
return post
}
module.exports = {
getLastPost
}