Last active
August 29, 2015 14:20
-
-
Save mattneary/b67b8cc55f64c5f7822c to your computer and use it in GitHub Desktop.
GroupMe API scraper
This file contains hidden or 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 http = require('http'); | |
var https = require('https'); | |
var GROUP = '10237173'; // Get from another API call | |
var record = []; | |
function getMessages(token, res, offset) { | |
https.get("https://api.groupme.com/v3/groups/" + GROUP + '/messages?limit=100&token=' + token + (offset ? '&before_id=' + offset : ''), function (apiRes) { | |
var buffer = []; | |
apiRes.on('data', function (chunk) { | |
buffer.push(chunk); | |
}); | |
apiRes.on('end', function () { | |
var data = JSON.parse(buffer.join('')); | |
var ms = data.response.messages; | |
record = record.concat(ms); | |
if (record.length < data.response.count) { | |
getMessages(token, res, ms[ms.length - 1].id); | |
} else { | |
res.write(JSON.stringify(record)); | |
res.end(); | |
} | |
}); | |
}); | |
} | |
// Make an app through GroupMe developer which points to localhost:3000 | |
http.createServer(function (req, res) { | |
if (req.url.indexOf('access_token') != -1) { | |
var token = req.url.split('=')[1]; | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
getMessages(token, res); | |
} else { | |
res.end(); | |
} | |
}).listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment