Created
March 5, 2011 22:53
-
-
Save NuckChorris/856802 to your computer and use it in GitHub Desktop.
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 http = require('http'); | |
var https = require('https'); | |
/** | |
* @name deviant.js | |
* @author Peter Lejeck <peter.lejeck@gmail.com> | |
* @description deviantJS, handles the process of logging in. | |
* @version 0.1 | |
*/ | |
exports.A = function ( e ){ | |
var Events = e; | |
var username, cookie, authtoken; | |
var server = { | |
login: { | |
transport: 'https://', | |
host: 'www.deviantart.com', | |
file: '/users/login', | |
port: 443 | |
}, | |
auth: { | |
transport: 'http://', | |
host: 'chat.deviantart.com', | |
file: '/chat/botdom', | |
port: 80 | |
}, | |
chat: { | |
host: 'chat.deviantart.com', | |
version: '0.3', | |
port: 3900 | |
} | |
}; | |
var agent = 'deviantJS/0.1'; | |
/** | |
* Logs in to deviantART, and grabs the authtoken | |
**/ | |
var login = function(username, password){ | |
this.username = username; | |
var postdata = {}, headers = {}, options = {}; | |
postdata = 'ref=' + encodeURI( server.login.transport + server.login.host + server.login.file ); | |
postdata += '&username=' + encodeURI( username ); | |
postdata += '&password=' + encodeURI( password ); | |
postdata += '&reusetoken=1'; | |
headers['Host'] = server.login.host; | |
headers['User-Agent'] = agent; | |
headers['Accept'] = "text/html"; | |
headers['Cookie'] = "skipintro=1"; | |
headers['Content-Type'] = "application/x-www-form-urlencoded"; | |
headers['Content-Length'] = postdata.length; | |
options.port = server.login.port; | |
options.host = server.login.host; | |
options.path = server.login.file; | |
options.method = 'POST'; | |
options.headers = headers; | |
// Login and get the cookie | |
var request = https.request( options, function( response ) { | |
var headers = {}, options = {}; | |
Events.emit( 'deviant.cookie', response.headers["set-cookie"] ); | |
headers['Host'] = server.auth.host; | |
headers['User-Agent'] = agent; | |
headers['Accept'] = "text/html"; | |
headers['Cookie'] = Array.isArray( response.headers["set-cookie"] ) ? response.headers["set-cookie"].join(';') : response.headers["set-cookie"]; | |
options.port = server.auth.port; | |
options.host = server.auth.host; | |
options.path = server.auth.file; | |
options.method = 'GET'; | |
options.headers = headers; | |
// Get authtoken | |
var req = http.request( options, function( resp ) { | |
resp.setEncoding('utf8'); | |
resp.on('data', function (chunk) { | |
if ( chunk.toString().indexOf( 'dAmn_Login(' ) !== -1 ) { | |
this.authtoken = /dAmn_Login\(\s*\".*\",\s*\"([a-f0-9]{32})\"\s*\)/.exec( chunk.toString() )[1]; | |
Events.emit( 'deviant.authtoken', this.authtoken ); | |
req.abort(); | |
} | |
}.bind(this) ); | |
}.bind(this) ); | |
req.end(); | |
}.bind(this) ); | |
request.write(postdata); | |
request.end(); | |
}; | |
/** | |
* Public stuff | |
**/ | |
return { | |
login: login | |
}; | |
}; |
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 EventEmitter = require('events').EventEmitter; | |
var e = new EventEmitter(); | |
var deviant = require('./deviant.js').A(e); | |
e.on('deviant.authtoken',function(auth){ | |
console.log(auth); | |
}); | |
deviant.login('username','password'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment