Last active
July 29, 2016 16:30
-
-
Save Shiggiddie/4e6728568273c0a1d0509c94955a399b to your computer and use it in GitHub Desktop.
Reddit Comment Posting using only request-promise and session cookies
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
/* | |
* The purpose of this excercise was to understand how to preserve a session | |
* using only request-promise (https://www.npmjs.com/package/request-promise) | |
* | |
* USAGE: | |
* | |
* Grab dependencies: | |
* $ npm --save request | |
* $ npm --save request-promise | |
* | |
* Execute with proper command line args: | |
* $ node reddit_comment.js -u 'reddit-username' -p 'reddit-password' -t 'thing-id you wish to comment on' -c 'comment text' | |
*/ | |
var req = require('request-promise'); | |
var ui = process.argv.indexOf('-u'); | |
var pi = process.argv.indexOf('-p'); | |
var ti = process.argv.indexOf('-t'); | |
var ci = process.argv.indexOf('-c'); | |
if (ui === -1 || pi === -1 || ti === -1) { | |
throw TypeError('you must provide -u [username], -p [password], -t [comment ID], -c [comment text] command line args'); | |
} | |
var username = process.argv[ui+1]; | |
var password = process.argv[pi+1]; | |
var thingID = process.argv[ti+1]; | |
var comment = process.argv[ci+1]; | |
var j = req.jar(); | |
var url = 'http://www.reddit.com'; | |
console.log('ATTEMPTING LOGIN'); | |
req({ | |
uri: url + '/api/login/' + username, | |
method: 'POST', | |
jar: j, | |
form: { | |
op: 'login', | |
user: username, | |
passwd: password, | |
api_type: 'json' | |
}, | |
resolveWithFullResponse: true | |
}) | |
.then(function(args) { | |
console.log('LOGIN SUCCESS'); | |
for (var i = 0; i < args.headers['set-cookie'].length; i++) { | |
var cookie = req.cookie(args.headers['set-cookie'][i]); | |
j.setCookie(cookie, url); | |
} | |
console.log('VISITING HOMEPAGE POST-LOGGED-IN'); | |
// Reddit provides CSRF protection in the form of a "uh" value. | |
// go back to homepage in order to pick up "uh" relevant for session | |
return req({ | |
uri: url, | |
method: 'GET', | |
jar: j, | |
resolveWithFullResponse: true | |
}) | |
}) | |
.then(function(args) { | |
var uh = /name="uh" value="([^"]+)"/.exec(args.body)[1]; | |
console.log('"UH" VALUE FOUND, ATTEMPTING TO POST COMMENT'); | |
return req({ | |
uri: url + '/api/comment', | |
method: 'POST', | |
jar: j, | |
form: { | |
thing_id: thingID, | |
text: comment, | |
id: '#commentreply_' + thingID, | |
r: 'pics', | |
uh: uh, | |
renderstyle: 'html' | |
}, | |
resolveWithFullResponse: true, | |
followAllRedirects: true // NEEDED FOR POSTS ONLY | |
}) | |
}) | |
.then(function(args) { | |
console.log('COMMENT POST SUCCESS'); | |
}) | |
.catch(function(args) { | |
console.log('THERE WAS AN ERROR SOMEWHERE'); | |
Object.keys(args).forEach(function(arg) { console.log(arg); }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment