Thursday, April 5, 2007

Script That Makes Comments in All Posts in Reddit

Hello there!

Below is the PHP script that drop comments in all REDDIT posts starting from the first page.

This script is just for educational purposes!
DO NOT RUN IT!



<?php
/*
* This software is for educational purposes only
* Do not run it in the internet or use it for automated comments posting
*
* READ ME:
* 1. Put correct reddit username and password
* 2. Run it from command line:
*
* php reddit_commenter.php
*/


define("USER", "reddit_username");
define("PASS", "reddit_password");
define("USE_PROXY", false);
define("HTTP_PROXY_SERVER", "127.0.0.1:3128");

/*
DO NOT CHANGE THE LINES BELOW
*/


define("HOSTNAME", "http://reddit.com");

$default_header[] = "Accept: */*";
$default_header[] = "Accept-Language: uk";
$default_header[] = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
$default_header[] = "Host: reddit.com";
$default_header[] = "Proxy-Connection: Keep-Alive";
$default_header[] = "Pragma: no-cache";
$default_header[] = "x-prototype-version: 1.3.1";
$default_header[] = "x-requested-with: XMLHttpRequest";

$DEFAULT_HEADER = $default_header;

$login_header[] = "Accept: */*";
$login_header[] = "Accept-Language: uk";
$login_header[] = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
$login_header[] = "Host: reddit.com";
$login_header[] = "Proxy-Connection: Keep-Alive";
$login_header[] = "Pragma: no-cache";
$login_header[] = "x-prototype-version: 1.3.1";
$login_header[] = "Referer: http://reddit.com";
$login_header[] = "x-requested-with: XMLHttpRequest";

$LOGIN_HEADER = $login_header;


$post_header[] = "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-icq, */*";
$post_header[] = "Accept-Language: uk";
$post_header[] = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)";
$post_header[] = "Host: reddit.com";
$post_header[] = "Proxy-Connection: Keep-Alive";
$post_header[] = "Pragma: no-cache";
$post_header[] = "x-prototype-version: 1.3.1";
$post_header[] = "x-requested-with: XMLHttpRequest";

$POST_HEADER = $post_header;

define("COOKIE_FILENAME", "C:/cookie.txt");

function GetCurlPage ($pageSpec, $header, $follow=true) {
$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if ($follow) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if (USE_PROXY) curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_SERVER);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILENAME);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILENAME);
$tmp = curl_exec ($ch);
curl_close ($ch);
return $tmp;
}


function PostCurlPage ($pageSpec, $data, $header, $follow=false) {
$ch = curl_init($pageSpec);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if ($follow) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if (USE_PROXY) curl_setopt($ch, CURLOPT_PROXY, HTTP_PROXY_SERVER);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILENAME);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILENAME);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$tmp = curl_exec ($ch);
curl_close ($ch);
return $tmp;
}

function startpage($link){
global $DEFAULT_HEADER;
$header = $DEFAULT_HEADER;
return GetCurlPage($link, $header);
}

function login(){
global $LOGIN_HEADER;
$login_data = "ajax=1&op=login&user=".USER."&passwd=".PASS."&rem=on&_=";
$login_length = strlen($login_data);
$header = $LOGIN_HEADER;
$header[] = "Content-Type: application/x-www-form-urlencoded";
$header[] = "Content-Length: $login_length";
$header[] = "Referer: ".HOSTNAME;
return PostCurlPage("http://reddit.com/login", $login_data, $header);
}

function get_comment_page($post_link){
global $DEFAULT_HEADER;
$header = $DEFAULT_HEADER;
return GetCurlPage($post_link, $header);
}


function create_post_data($url, $parent_id, $comment){
return "url=".urlencode($url)."&parentid=".$parent_id."&comment=".urlencode($comment);
}


function post_comment($url, $parent_id, $comment){
global $DEFAULT_HEADER;
$post_data = create_post_data($url, $parent_id, $comment);
$login_length = strlen($post_data);
$header = $DEFAULT_HEADER;
$header[] = "Content-Type: application/x-www-form-urlencoded";
$header[] = "Content-Length: $login_length";
$header[] = "Referer: ".HOSTNAME."$url";
$post = PostCurlPage("http://reddit.com/comment", $post_data, $header);
}

function get_comment_links($html){
$pattern = '/href="(\/info\/[^\/]+\/comments)"\sclass="bylink"/';
preg_match_all($pattern, $html, $matches);
return $matches[1];
}

function get_parent_id ($html) {
$pattern = '/<input\stype="hidden"\sname="parentid"\svalue="(\d+)?">/';
preg_match_all($pattern, $html, $matches);
return $matches[1][0];
}

$post_body = "This is automated comment from [REDDIT automated commenter PHP script](http://redditscripts.blogspot.com/2007/04/script-that-makes-comments-in-all-posts.html)";

for($i=0; $i<500; $i+=25){
if (login()=='0'){
echo "Processing http://reddit.com/?offset=$i\n";
$start_page = startpage("http://reddit.com/?offset=$i");
$comment_links = get_comment_links($start_page);
foreach ($comment_links as $comment_link){
echo "\tget comment link $comment_link\n";
$comment_page = get_comment_page(HOSTNAME.$comment_link);
$parent_id = get_parent_id($comment_page);
post_comment($comment_link, $parent_id, $post_body);
echo "\tPost done\n";
}
} else {
echo "Cannot login [$login]";
}
}
?>