Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('inc/httpful.phar');
- class reddit_api
- {
- static $url = 'https://www.reddit.com/';
- private $modhash=null;
- private $cookie=null;
- function doCommand($url,$data=null)
- {
- if (substr($url,0,4)=='api/')
- $request = \Httpful\Request::post(reddit_api::$url.$url);
- else
- $request = \Httpful\Request::get(reddit_api::$url.$url);
- $request -> addHeader('User-Agent','atpeace_bot 0.1');
- if ($this->modhash!==null)
- $request -> addHeader('X-Modhash',$this->modhash);
- if ($this->cookie!==null)
- $request -> addHeader('Cookie','reddit_session='.$this->cookie);
- if ($data!==null)
- {
- $data['api_type'] = 'json';
- $request -> body(http_build_query($data));
- }
- $request -> expectsJson();
- $response = null;
- try
- {
- $response = $request -> send();
- } catch (Exception $e) {
- echo 'Error '.$e;
- die();
- }
- if (isset($response->body->json->data->modhash))
- $this->modhash = $response->body->json->data->modhash;
- if (isset($response->body->json->data->cookie))
- $this->cookie = $response->body->json->data->cookie;
- //echo $this->modhash;
- if (isset($response->body->json->data))
- return $response->body->json->data;
- else
- return $response->body;
- }
- }
- class reddit_bot
- {
- private $api;
- private $username;
- private $password;
- public $rules = array();
- public $removed = 0;
- public $checked = 0;
- private $auth=false;
- function __construct($username,$password)
- {
- $this->username = $username;
- $this->password = $password;
- $this->api = new reddit_api();
- }
- function login()
- {
- $reply = $this->api->doCommand('api/login',array('user'=>$this->username,'passwd'=>$this->password));
- $this->auth=true;
- }
- function logout()
- {
- $reply = $this->api->doCommand('logout');
- }
- function process_rules()
- {
- $page=null;
- foreach ($this->rules as $rule)
- {
- if ($rule['page']=='reports')
- $page=$this->api->doCommand('r/'.$rule['sub'].'/about/reports.json');
- else
- $page=$this->api->doCommand('r/'.$rule['sub'].'.json');
- $rule_eval = new rule($rule['remove']);
- foreach ($page->data->children as $post)
- {
- $fail = $rule_eval->match($post);
- if ($fail)
- {
- $this->api->doCommand('api/remove',array('id'=>$post->data->name,'spam'=>0));
- $this->removed++;
- }
- $this->checked++;
- }
- }
- }
- }
- class rule
- {
- private $rule_string;
- private $eval = '';
- function __construct($rule)
- {
- $this->rule_string=$rule;
- $this->eval='return '.$rule.';';
- $this->eval=str_replace('num_reports','$post->data->num_reports',$this->eval);
- $this->eval=str_replace('score','$post->data->score',$this->eval);
- }
- function match($post)
- {
- return eval($this->eval);
- }
- }
- $my_bot = new reddit_bot('XXXaccountnameXXX','XXXpasswordXXXX');
- $my_bot->rules[]=array('sub'=>'XXXsubredditnameXXX','page'=>'reports','remove'=>'num_reports>5&&score<1');
- $my_bot->login();
- $my_bot->process_rules();
- $my_bot->logout();
- echo 'Removed ('.$my_bot->removed.'/'.$my_bot->checked.')';
- ?>
Add Comment
Please, Sign In to add comment