Last active
September 6, 2017 16:35
-
-
Save fabsrc/73a57322c5521ea6c6f9cc122dde335b to your computer and use it in GitHub Desktop.
List and save all messages from the Facebook ticker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Nightmare = require('nightmare') | |
const Datastore = require('nedb') | |
const DB = new Datastore({ filename: 'ticks.db', autoload: true }) | |
const INTERVAL = process.env.INTERVAL || 15000 | |
var nightmare = new Nightmare({ | |
show: true, | |
webPreferences: { | |
partition: 'nopersist' | |
} | |
}) | |
nightmare | |
.on('console', (type, log) => { | |
if (log.type && log.type === 'tickerFeedMessage') { | |
DB.find(log, (err, ticks) => { | |
if (err) console.error(err) | |
if (ticks.length === 0) { | |
console.log(`${log.user}${log.text}`) | |
DB.insert(log) | |
} | |
}) | |
} | |
}) | |
.goto('https://facebook.com') | |
.viewport(1600, 800) | |
.type('input#email', process.env.EMAIL) | |
.type('input#pass', process.env.PASSWORD) | |
.click('#loginbutton input') | |
.then(getMessages) | |
.catch(console.error) | |
function getMessages () { | |
setTimeout(() => { | |
return nightmare | |
.goto('https://facebook.com') | |
.wait('.tickerFeedMessage') | |
.evaluate(function () { | |
let feedTickerStories = document.getElementsByClassName('fbFeedTickerStory') | |
Array.from(feedTickerStories).reverse().forEach(a => { | |
let feedMessage = a.getElementsByClassName('tickerFeedMessage')[0] | |
let storyLink = a.getElementsByClassName('tickerStoryLink')[0] | |
let link = storyLink && storyLink.href | |
let user = feedMessage.lastChild.previousSibling.innerText | |
let text = feedMessage.lastChild.textContent | |
console.log({ type: 'tickerFeedMessage', user, text, link }) | |
}) | |
}) | |
.then(getMessages) | |
}, INTERVAL) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment