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
var request = require('request'), | |
$ = require('cheerio'); | |
var roomid = process.argv[2]; | |
if (!roomid) { | |
throw new Error('you need to pass a roomid and a output file'); | |
} | |
var outputfile = process.argv[3] || roomid; | |
var found = []; | |
var page = 1; | |
var start = Date.now(); | |
var siteRoot = 'http://chat.stackoverflow.com/rooms/info/' + roomid + '/'; | |
console.log('scraping room#', roomid); | |
scrape(siteRoot + '?tab=stars'); | |
function scrape(url) { | |
console.log('scraping page ', page); | |
request(url, function(err, res, html) { | |
var root = $(html); | |
var newUrl = root.find('a[rel="next"]').attr('href'); | |
var matches = []; | |
root.find('.monologue').each(function(i, row) { | |
var messageId = $(row).find('.message a').attr('name'); | |
var messageStarCount = $(row).find('.flash .stars .times').text(); | |
var user = $(row).find('.username').text(); | |
var userid = $(row).find('.username a').attr('href'); | |
matches.push({ | |
id: messageId, | |
count: messageStarCount, | |
username: user, | |
userid: userid | |
}); | |
}); | |
found = found.concat(matches); | |
if (newUrl) { | |
page++; | |
console.log('total time: ', (Date.now() - start) / 1000, 'seconds'); | |
scrape(siteRoot + newUrl); | |
} else { | |
finish(); | |
} | |
}); | |
} | |
function finish() { | |
var ws = require('fs').createWriteStream(outputfile); | |
ws.on('error', function(err) { | |
console.error(err); | |
}); | |
ws.write(JSON.stringify(found)); | |
ws.end(); | |
console.log('found stars written to ', outputfile); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment