Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Last active December 26, 2015 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThiefMaster/6c86cf4dea122d267418 to your computer and use it in GitHub Desktop.
Save ThiefMaster/6c86cf4dea122d267418 to your computer and use it in GitHub Desktop.
"use strict";
var request = require('request'),
cheerio = require('cheerio');
function loginSEOpenID(email, password, callback) {
console.log('SE OpenID: Loading login form');
request.get({
url: 'https://openid.stackexchange.com/account/login'
}, function(error, response, body) {
var $ = cheerio.load(body);
var fkey = $('input[name="fkey"]').attr('value');
console.log('SE OpenID: Logging in');
request.post({
url: 'https://openid.stackexchange.com/account/login/submit',
form: {
email: email,
password: password,
fkey: fkey
}
}, function(error, response, body) {
console.log('SE OpenID: Logged in (hopefully)');
callback();
});
});
}
function loginSO(callback) {
console.log('SO: Loading login form');
request.get({
url: 'http://stackoverflow.com/users/login'
}, function(error, response, body) {
var $ = cheerio.load(body);
var fkey = $('input[name="fkey"]').attr('value');
console.log('SO: Logging in via SE OpenID');
request.post({
followAllRedirects: true,
url: 'http://stackoverflow.com/users/authenticate',
form: {
openid_identifier: 'https://openid.stackexchange.com',
fkey: fkey
}
}, function(error, response, body) {
var $ = cheerio.load(body);
console.log('SO: Logged in as ' + $('.profile-link').text());
callback();
});
});
}
loginSEOpenID('email@address', 'password', function() {
loginSO(function() {
console.log('Chat: Retrieving form key');
request.get({
url: 'http://chat.stackoverflow.com'
}, function(error, response, body) {
var $ = cheerio.load(body);
var fkey = $('input[name="fkey"]').attr('value');
console.log('Chat: Current username: ' + $('#top-menu > a[href^="/users/"]').text());
console.log('Chat: Sending message');
request.post({
url: 'http://chat.stackoverflow.com/chats/17/messages/new',
form: {
text: '*the python-less node chat client says hello*',
fkey: fkey
}
}, function(error, response, body) {
console.log('Chat: Sent message', body);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment