Day_Tripper

Sub Reddit auto mod bot

Oct 23rd, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.65 KB | None | 0 0
  1. <?php
  2. require_once('inc/httpful.phar');
  3.  
  4. class reddit_api
  5. {
  6.     static $url = 'https://www.reddit.com/';
  7.     private $modhash=null;
  8.     private $cookie=null;
  9.    
  10.     function doCommand($url,$data=null)
  11.     {
  12.         if (substr($url,0,4)=='api/')
  13.             $request = \Httpful\Request::post(reddit_api::$url.$url);
  14.         else
  15.             $request = \Httpful\Request::get(reddit_api::$url.$url);
  16.         $request -> addHeader('User-Agent','atpeace_bot 0.1');
  17.         if ($this->modhash!==null)
  18.             $request -> addHeader('X-Modhash',$this->modhash);
  19.         if ($this->cookie!==null)
  20.             $request -> addHeader('Cookie','reddit_session='.$this->cookie);
  21.         if ($data!==null)
  22.         {
  23.             $data['api_type'] = 'json';
  24.             $request -> body(http_build_query($data));
  25.         }
  26.         $request -> expectsJson();
  27.         $response = null;
  28.         try
  29.         {
  30.             $response = $request -> send();
  31.         }  catch (Exception $e) {
  32.             echo 'Error '.$e;
  33.             die();
  34.         }
  35.        
  36.         if (isset($response->body->json->data->modhash))
  37.             $this->modhash = $response->body->json->data->modhash;
  38.         if (isset($response->body->json->data->cookie))
  39.             $this->cookie = $response->body->json->data->cookie;
  40.        
  41.         //echo $this->modhash;
  42.         if (isset($response->body->json->data))
  43.             return $response->body->json->data;
  44.         else
  45.             return $response->body;
  46.     }
  47. }
  48.  
  49. class reddit_bot
  50. {
  51.     private $api;
  52.  
  53.     private $username;
  54.     private $password;
  55.     public $rules = array();
  56.    
  57.     public $removed = 0;
  58.     public $checked = 0;
  59.    
  60.     private $auth=false;
  61.    
  62.     function __construct($username,$password)
  63.     {
  64.         $this->username = $username;
  65.         $this->password = $password;
  66.         $this->api = new reddit_api();
  67.     }
  68.    
  69.     function login()
  70.     {
  71.         $reply = $this->api->doCommand('api/login',array('user'=>$this->username,'passwd'=>$this->password));
  72.         $this->auth=true;
  73.     }
  74.    
  75.     function logout()
  76.     {
  77.         $reply = $this->api->doCommand('logout');
  78.     }
  79.    
  80.     function process_rules()
  81.     {
  82.         $page=null;
  83.         foreach ($this->rules as $rule)
  84.         {
  85.             if ($rule['page']=='reports')
  86.                 $page=$this->api->doCommand('r/'.$rule['sub'].'/about/reports.json');
  87.             else
  88.                 $page=$this->api->doCommand('r/'.$rule['sub'].'.json');
  89.             $rule_eval = new rule($rule['remove']);
  90.             foreach ($page->data->children as $post)
  91.             {
  92.                 $fail = $rule_eval->match($post);
  93.                 if ($fail)
  94.                 {
  95.                     $this->api->doCommand('api/remove',array('id'=>$post->data->name,'spam'=>0));
  96.                     $this->removed++;
  97.                 }
  98.                 $this->checked++;
  99.             }
  100.         }
  101.     }
  102.    
  103. }
  104.  
  105. class rule
  106. {
  107.     private $rule_string;
  108.     private $eval = '';
  109.    
  110.     function __construct($rule)
  111.     {
  112.         $this->rule_string=$rule;
  113.         $this->eval='return '.$rule.';';
  114.         $this->eval=str_replace('num_reports','$post->data->num_reports',$this->eval);
  115.         $this->eval=str_replace('score','$post->data->score',$this->eval);
  116.     }
  117.  
  118.     function match($post)
  119.     {
  120.         return eval($this->eval);
  121.     }
  122.  
  123. }
  124.  
  125. $my_bot = new reddit_bot('XXXaccountnameXXX','XXXpasswordXXXX');
  126. $my_bot->rules[]=array('sub'=>'XXXsubredditnameXXX','page'=>'reports','remove'=>'num_reports>5&&score<1');
  127. $my_bot->login();
  128. $my_bot->process_rules();
  129. $my_bot->logout();
  130. echo 'Removed ('.$my_bot->removed.'/'.$my_bot->checked.')';
  131. ?>
Add Comment
Please, Sign In to add comment