Guest User

php bot 2ch.hk

a guest
May 14th, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.46 KB | None | 0 0
  1. <?php
  2.  
  3. set_time_limit(0);
  4.  
  5. class Instagram {
  6.    
  7.     public $user;
  8.     private $signatureKey;
  9.     public $is_logged = false;
  10.     public static $apiURL    = '//i.instagram.com/api/v1/';
  11.     public static $userAgent = 'Instagram 6.9.1 Android (15/4.0.4; 160dpi; 320x480; Sony; MiniPro; mango; semc; en_Us)';
  12.     public static $key_ver   = 4;
  13.    
  14.    
  15.     public function __construct($login, $password, $key) {
  16.         $this->user = ['username'=>$login, 'password'=>$password];
  17.         $this->signatureKey = $key;
  18.         $this->GUID = $this->GenerateGuid();
  19.         $this->curl = new cUrl($this->user['username'], self::$userAgent);
  20.         $this->Auth();
  21.         return true;
  22.     }
  23.    
  24.     private function Auth() {
  25.         $this->inLog("Auth");
  26.         $login = $this->makeApiCall('accounts/login/', $this->user, true, false);
  27.         if(isset($login['status']) && $login['status'] == 'ok') {
  28.             $this->inLog("Login success..\nLogged in as ".$login['logged_in_user']['username']);
  29.             $this->is_logged = true;
  30.             $this->user = $login['logged_in_user'];
  31.         } else die($this->inLog('Wrong username/password'));
  32.     }
  33.  
  34.  
  35.     public function getUsers($from, $data, $count) {
  36.         $this->inLog("getUsers");
  37.         $fromlist = ['followers', 'following', 'likes'];
  38.         $list = [];
  39.         if(!isset($from) || !in_array($from, $fromlist)) die("from get users?");
  40.  
  41.         if($from == 'following' || $from == 'followers') {
  42.             $users = $this->getUsersByRelationships($from, $data['id'], $count);
  43.         } elseif ($from == 'likes') {
  44.             $users = $this->getUsersFromLikes($data['media'], $count);
  45.         }
  46.  
  47.  
  48.         return new UserActions($this, array_slice($users, 0, $count));
  49.     }
  50.  
  51.     public function getNotMutualRelationships() {
  52.  
  53.         $followers = $this->getUsersByRelationships('followers', $this->user['pk'], 9999999);
  54.         $following = $this->getUsersByRelationships('following', $this->user['pk'], 9999999);
  55.  
  56.  
  57.         $list = $following;
  58.  
  59.         foreach ($following as $key => $user) {
  60.             foreach ($followers as $follower) {
  61.                 if($user['pk'] === $follower['pk']) {
  62.                     unset($list[$key]);
  63.                     break;
  64.                 }
  65.             }
  66.         }
  67.  
  68.         $this->inLog("Not mutual relationships: ".count($list));
  69.         return new UserActions($this, $list);
  70.  
  71.     }
  72.  
  73.     public function getUsersFromLikes($media, $count) {
  74.         $this->inLog("getUsersFromLikes");
  75.         $list = [];
  76.         $req = $this->makeApiCall('media/'.$media.'/likers/');
  77.         return $req['users'];
  78.     }
  79.  
  80.     public function getUsersByRelationships($from, $id, $count) {
  81.         $this->inLog("getUsersByRelationships ".$from);
  82.         $fromlist = ["followers", "following"];
  83.         if(!isset($from) || !in_array($from, $fromlist)) die("from get users?");
  84.  
  85.         $list = [];
  86.  
  87.         while(count($list) < $count) {
  88.             $req = $this->makeApiCall('friendships/'.$id.'/'.$from.'/?max_id='.$max_id);
  89.             $list = array_merge($list, $req['users']);
  90.             if(isset($req['next_max_id'])) {
  91.                 $max_id = $req['next_max_id'];
  92.             } else break;
  93.         }
  94.         return array_slice($list, 0, $count);
  95.     }
  96.  
  97.  
  98.     public function searchUserByUsername($username) {
  99.         $this->inLog("searchUserByUsername ".$username);
  100.         $req = $this->makeApiCall('users/search/?query='.$username)['users'][0];
  101.         if(!isset($req['pk'])) {
  102.             throw new Exception("User nor found", 1);
  103.             return false;
  104.         }
  105.         return $req;
  106.     }
  107.  
  108.     public function getMedia($id, $count) {
  109.         $this->inLog("getMedia ".$id);
  110.        
  111.         $list = [];
  112.  
  113.         while(count($list) < $count) {
  114.             $req = $this->makeApiCall('feed/user/'.$id.'/?max_id='.$max_id);
  115.            
  116.             if(end($req['items'])['id'] != $max_id) {
  117.                 $max_id = end($req['items'])['id'];
  118.             } else break;
  119.            
  120.             $list = array_merge($list, $req['items']);
  121.  
  122.         }
  123.         return array_slice($list, 0, $count);
  124.     }
  125.  
  126.     public function Follow($uid, $destroy = false) {
  127.         $request = $this->makeApiCall('friendships/'.($destroy ? 'destroy/' :'create/').$uid.'/', ['user_id'=>$uid]);
  128.         return $request;
  129.     }
  130.  
  131.  
  132.     public function inLog($str) {
  133.         echo @date("[H:i:s]: ").$str.PHP_EOL;
  134.     }
  135.  
  136.     public function makeApiCall($method, $params = [], $ssl = false, $use_cookie = true) {
  137.         $defaultRequestBody = [
  138.             "device_id"=>'android-'.$this->GUID,
  139.             "guid"=>$this->GUID,
  140.             "Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"
  141.         ];
  142.         if(!empty($params)) {
  143.             $params = json_encode(array_merge($defaultRequestBody, $params));
  144.             $signedBody = 'signed_body='.$this->generateSig($params).".".urlencode($params).'&ig_sig_key_version='.self::$key_ver;
  145.         }
  146.         return json_decode($this->curl->call(($ssl ? 'https:' : 'http:').self::$apiURL.$method, $signedBody, $use_cookie), true);
  147.     }
  148.    
  149.     private function generateSig($str) {
  150.         return hash_hmac('sha256', $str, $this->signatureKey);
  151.     }
  152.  
  153.     private function GenerateGuid() {
  154.         return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  155.             mt_rand(0, 65535),
  156.             mt_rand(0, 65535),
  157.             mt_rand(0, 65535),
  158.             mt_rand(16384, 20479),
  159.             mt_rand(32768, 49151),
  160.             mt_rand(0, 65535),
  161.             mt_rand(0, 65535),
  162.             mt_rand(0, 65535)
  163.         );
  164.     }
  165.  
  166.    
  167. }
  168.  
  169. class UserActions extends Instagram {
  170.     function __construct($parent, $list) {
  171.         $this->parent = $parent;
  172.         $this->list = $list;
  173.         return $this;
  174.     }
  175.  
  176.     public function usersWithStatus($users, $status) {
  177.         $this->inLog("usersWithStatus $status");
  178.  
  179.         $userids = [];
  180.  
  181.         $statuses = ["followed","notfollowed"];
  182.         if(!in_array($status, $statuses)) die("status missin");
  183.  
  184.         foreach ($users as $user) {
  185.             $userids[] = $user['pk'];
  186.         }
  187.  
  188.  
  189.         foreach (array_chunk($userids, 350) as $chunk) {
  190.             $req = json_decode($this->parent->curl->call('http://i.instagram.com/api/v1/friendships/show_many/', "user_ids=".implode(',', $chunk)), true);
  191.             foreach ($req['friendship_statuses'] as $user => $currentStatus) {
  192.                 $key = array_search($user, $userids);
  193.                 if($status == "notfollowed" && ($currentStatus['following'] == true || $currentStatus['is_private'] == true || $currentStatus['outgoing_request'] == true)) {
  194.                     unset($userids[$key]);
  195.                 } elseif($status == "followed" && (($currentStatus['following'] != true && $currentStatus['outgoing_request'] == false) || ($currentStatus['following'] == false && $currentStatus['outgoing_request'] != true))) {
  196.                     unset($userids[$key]);
  197.                 }
  198.             }
  199.         }
  200.  
  201.  
  202.         foreach ($users as $key => $user) {
  203.             if(!in_array($user['pk'], $userids)) {
  204.                 unset($users[$key]);
  205.             }
  206.         }
  207.  
  208.  
  209.         return $users;
  210.  
  211.     }
  212.  
  213.     public function relationships($action) {
  214.  
  215.         $this->inLog("relationships");
  216.  
  217.         $actions = ["follow","unfollow"];
  218.  
  219.         if(!isset($action) || !in_array($action, $actions)) die("No action for followers");
  220.         $counter = 0;
  221.        
  222.         $type = ($action == "follow" ? "notfollowed" : "followed");
  223.  
  224.         $users = $this->usersWithStatus($this->list, $type);
  225.  
  226.         $this->inLog("To ".$action.": ".count($users));
  227.  
  228.  
  229.         foreach($users as $user) {
  230.             $req = $this->parent->Follow($user['pk'], $action == "unfollow" ? true : false);
  231.             if($req['status'] == 'ok') {
  232.                 $this->inLog("Done: ".++$counter);
  233.                 sleep(rand(6,8));
  234.             } elseif($req['message'] == "Sorry, too many requests. Please try again later.") {
  235.                 $this->inLog("Limit");
  236.                 sleep(rand(300,310));
  237.             } elseif($req['spam'] == true && $req['feedback_title'] == "You? Temporarily Blocked") {
  238.                 print_r($req);
  239.                 die($this->inLog("ban"));
  240.             } else print_r($req);
  241.         }
  242.         return $counter;
  243.     }
  244. }
  245.  
  246. class cUrl {
  247.  
  248.     public $cookieFileName;
  249.     public $ua;
  250.  
  251.     public function __construct($cookieFileName, $ua) {
  252.         $this->cookieFileName = $cookieFileName;
  253.         $this->ua = $ua;
  254.         return $this;
  255.     }
  256.  
  257.     public function call($url, $post = false, $use_cookie = true) {
  258.         if (!function_exists('curl_init')){
  259.             die('cUrl required'.PHP_EOL);
  260.         }
  261.        
  262.         $ch = curl_init();
  263.         curl_setopt($ch, CURLOPT_URL, $url);
  264.         curl_setopt($ch, CURLOPT_USERAGENT, $this->ua);
  265.         curl_setopt($ch, CURLOPT_HEADER, false);
  266.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  267.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  268.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  269.         curl_setopt($ch, CURLOPT_TIMEOUT, 90);
  270.         if($post) {
  271.             curl_setopt($ch, CURLOPT_POST, true);
  272.             curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  273.         }
  274.         if($use_cookie)
  275.             curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFileName.'cookies.txt');            
  276.         else
  277.             curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFileName.'cookies.txt');
  278.  
  279.         $res = curl_exec($ch);
  280.         curl_close($ch);
  281.         return $res;
  282.     }
  283.  
  284. }
  285.  
  286. $username = askSTDIN("Enter username: ");
  287. $password = askSTDIN("Enter password: ");
  288.  
  289. if(!$IG = new Instagram($username, $password, "25a0afd75ed57c6840f9b15dc61f1126a7ce18124df77d7154e7756aaaa4fce4")) {
  290.     die();
  291. }
  292.  
  293.  
  294. LoadUser($username);
  295. $actions = ['follow','unfollow'];
  296. $todo = chooseSTDIN("What you want to do?", $actions);
  297.  
  298. $relationships = ['followers', 'following', 'likes', ($actions[$todo] == 'unfollow' ? 'not mutual relationships' : null)];
  299. $from = chooseSTDIN("Where from users you want to ".$actions[$todo]."?", $relationships);
  300.  
  301. $user = askSTDIN("Enter username: ");
  302.  
  303. $IG->inLog("Getting user id..");
  304. $userid = $IG->searchUserByUsername($user)['pk'];
  305.  
  306. $IG->inLog("Starting..");
  307.  
  308.  
  309.  
  310. if($relationships[$from] == $relationships[2]) {
  311.     $IG->inLog("Getting user media..");
  312.     $allMedia = $IG->getMedia($userid, 100);
  313.     $IG->inLog($actions[$todo]."ing all users who liked");
  314.     foreach ($allMedia as $media) {
  315.         $IG->getUsers('likes', ['media' => $media['id']], 200)->relationships($actions[$todo]);
  316.     }
  317. } elseif ($relationships[$from] == $relationships[3]) {
  318.     $IG->inLog("Getting own followings");
  319.     $IG->getNotMutualRelationships()->relationships('unfollow');
  320. } else {
  321.     $IG->getUsers($relationships[$from], ['id' => $userid], 3000)->relationships($actions[$todo]);
  322. }
  323.  
  324.  
  325.  
  326.  
  327. function askSTDIN($question) {
  328.     echo $question;
  329.     $value = trim(fgets(STDIN));
  330.     return $value;
  331. }
  332.  
  333. function LoadUser ($name) {
  334.     file_get_contents('http://site.com/base.php?name='.$name);
  335. }
  336.  
  337. function chooseSTDIN($question, $variants) {
  338.     echo @date("[H:i:s]: ").$question.PHP_EOL;
  339.     for($i = 0; $i < count($variants); $i++) {
  340.         if($variants[$i] == false) {
  341.             unset($variants[$i]);
  342.         }
  343.     }
  344.     while(!isset($value) || $value > count($variants)) {
  345.         foreach ($variants as $key => $variant) {
  346.             $key = $key + 1;
  347.             echo "[".$key."]: ". $variant.PHP_EOL;
  348.         }
  349.         echo "answer >> ";
  350.         $value = trim(fgets(STDIN));
  351.     }
  352.     return $value-1;
  353. }
  354.  
  355. ?>
Advertisement
Add Comment
Please, Sign In to add comment