Last active
August 29, 2015 14:21
-
-
Save coolicer/435664065d7bf3391b6b to your computer and use it in GitHub Desktop.
callback vs async
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'); | |
var cheerio = require('cheerio'); | |
var async = require('async'); | |
var jar = request.jar(); | |
var form; | |
async.waterfall([ | |
function(callback){ | |
request({ | |
url : 'https://github.com/login', | |
method : 'GET', | |
jar : jar | |
}, function(err, res, body) { | |
console.log('get login page first') | |
callback(err, res, body); | |
}) | |
}, | |
function(res, body, callback){ | |
console.log('then get authenticity_token') | |
var $ = cheerio.load(res.body); | |
if(res.statusCode === 200) { | |
form = { | |
utf8 : true, | |
authenticity_token : $('input[name="authenticity_token"]').val() | |
}; | |
form.login = 'YOUR_EMAIL_ADRESS'; | |
form.password = 'YOUR_PASSWORD'; | |
request({ | |
url : 'https://github.com/session', | |
method : 'POST', | |
jar : jar, | |
form : form | |
}, function(err, res, body) { | |
callback(err, res, body); | |
}) | |
} | |
}, | |
function(res, body, callback){ | |
console.log('success login and redirect to /login') | |
if(res.statusCode === 302) { | |
request({ | |
url : 'https://github.com/login', | |
method : 'GET', | |
jar : jar | |
}, function(err, res, body) { | |
callback(err, res, body) | |
}) | |
} | |
} | |
], | |
function(err, res, body){ | |
if (res.statusCode === 200) { | |
var $ = cheerio.load(res.body); | |
console.log('user: ',$('span.css-truncate-target').eq(0).text()); | |
} | |
}); |
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'); | |
var cheerio = require('cheerio'); | |
var jar = request.jar(); | |
var form; | |
console.log('get login page first') | |
request({ | |
url : 'https://github.com/login', | |
method : 'GET', | |
jar : jar | |
}, function(err, res, body) { | |
console.log('then get authenticity_token') | |
if (res.statusCode === 200) { | |
var $ = cheerio.load(res.body); | |
form = { | |
utf8 : true, | |
authenticity_token : $('input[name="authenticity_token"]').val() | |
}; | |
form.login = 'YOUR_EMAIL_ADRESS'; | |
form.password = 'YOUR_PASSWORD'; | |
request({ | |
url : 'https://github.com/session', | |
method : 'POST', | |
form : form, | |
jar : jar | |
}, function(err, res, body) { | |
console.log('success login and redirect to /login') | |
if (res.statusCode === 302) { | |
request({ | |
url : 'https://github.com/login', | |
method : 'GET', | |
jar : jar | |
}, function(err, res, body) { | |
if (res.statusCode === 200) { | |
var $ = cheerio.load(res.body); | |
console.log('user: ',$('span.css-truncate-target').eq(0).text()); | |
} | |
}); | |
} | |
}) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment