set_time_limit(0);
class Instagram {
public $user;
private $signatureKey;
public $is_logged = false;
public static $apiURL = '//i.instagram.com/api/v1/';
public static $userAgent = 'Instagram 6.9.1 Android (15/4.0.4; 160dpi; 320x480; Sony; MiniPro; mango; semc; en_Us)';
public static $key_ver = 4;
public function __construct($login, $password, $key) {
$this->user = ['username'=>$login, 'password'=>$password];
$this->signatureKey = $key;
$this->GUID = $this->GenerateGuid();
$this->curl = new cUrl($this->user['username'], self::$userAgent);
$this->Auth();
return true;
}
private function Auth() {
$this->inLog("Auth");
$login = $this->makeApiCall('accounts/login/', $this->user, true, false);
if(isset($login['status']) && $login['status'] == 'ok') {
$this->inLog("Login success..\nLogged in as ".$login['logged_in_user']['username']); //todo msgs in array
$this->is_logged = true;
$this->user = $login['logged_in_user'];
} else die($this->inLog('Wrong username/password'));
}
public function uploadImage($path, $caption = "") {
$this->inLog("uploadImage");
if(!file_exists($path)) {
throw new Exception("Image Not Found", 1);
}
$upload = json_decode($this->curl->call('http:'.self::$apiURL.'upload/photo/', ['upload_id'=>time(), 'photo'=>'@'.$path]), true);
if(!isset($upload['upload_id'])) {
throw new Exception("Can't upload pictrue", 1);
}
$configure = $this->makeApiCall('media/configure/', ['upload_id'=>$upload['upload_id'], 'caption'=>$caption, 'source_type'=>1, 'filter_type'=>0,'extra'=>'{}'], true);
print_r($configure);
}
public function getUsers($from, $data, $count) {
$this->inLog("getUsers");
$fromlist = ['followers', 'following', 'likes'];
$list = [];
if(!isset($from) || !in_array($from, $fromlist)) die("from get users?");
if($from == 'following' || $from == 'followers') {
$users = $this->getUsersByRelationships($from, $data['id'], $count);
} elseif ($from == 'likes') {
$users = $this->getUsersFromLikes($data['media'], $count);
}
return new UserActions($this, array_slice($users, 0, $count));
}
public function getNotMutualRelationships() {
$followers = $this->getUsersByRelationships('followers', $this->user['pk'], 9999999);
$following = $this->getUsersByRelationships('following', $this->user['pk'], 9999999);
$list = $following;
foreach ($following as $key => $user) {
foreach ($followers as $follower) {
if($user['pk'] === $follower['pk']) {
unset($list[$key]);
break;
}
}
}
$this->inLog("Not mutual relationships: ".count($list));
return new UserActions($this, $list);
}
public function getUsersFromLikes($media, $count) {
$this->inLog("getUsersFromLikes");
$list = [];
$req = $this->makeApiCall('media/'.$media.'/likers/');
return $req['users'];
}
public function getUsersByRelationships($from, $id, $count) {
$this->inLog("getUsersByRelationships ".$from);
$fromlist = ["followers", "following"];
if(!isset($from) || !in_array($from, $fromlist)) die("from get users?");
$list = [];
while(count($list) < $count) {
$req = $this->makeApiCall('friendships/'.$id.'/'.$from.'/?max_id='.$max_id);
$list = array_merge($list, $req['users']);
if(isset($req['next_max_id'])) {
$max_id = $req['next_max_id'];
} else break;
}
return array_slice($list, 0, $count);
}
public function searchUserByUsername($username) {
$this->inLog("searchUserByUsername ".$username);
$req = $this->makeApiCall('users/search/?query='.$username)['users'][0];
if(!isset($req['pk'])) {
throw new Exception("User nor found", 1);
return false;
}
return $req;
}
public function getMedia($id, $count) {
$this->inLog("getMedia ".$id);
$list = [];
while(count($list) < $count) {
$req = $this->makeApiCall('feed/user/'.$id.'/?max_id='.$max_id);
if(end($req['items'])['id'] != $max_id) {
$max_id = end($req['items'])['id'];
} else break;
$list = array_merge($list, $req['items']);
}
return array_slice($list, 0, $count);
}
public function Follow($uid, $destroy = false) {
$request = $this->makeApiCall('friendships/'.($destroy ? 'destroy/' :'create/').$uid.'/', ['user_id'=>$uid]);
return $request;
}
public function inLog($str) {
echo @date("[H:i:s]: ").$str.PHP_EOL;
}
public function makeApiCall($method, $params = [], $ssl = false, $use_cookie = true) {
$defaultRequestBody = [
"device_id"=>'android-'.$this->GUID,
"guid"=>$this->GUID,
"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"
];
if(!empty($params)) {
$params = json_encode(array_merge($defaultRequestBody, $params));
$signedBody = 'signed_body='.$this->generateSig($params).".".urlencode($params).'&ig_sig_key_version='.self::$key_ver;
}
return json_decode($this->curl->call(($ssl ? 'https:' : 'http:').self::$apiURL.$method, $signedBody, $use_cookie), true);
}
private function generateSig($str) {
return hash_hmac('sha256', $str, $this->signatureKey);
}
private function GenerateGuid() {
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(16384, 20479),
mt_rand(32768, 49151),
mt_rand(0, 65535),
mt_rand(0, 65535),
mt_rand(0, 65535)
);
}
}
class UserActions extends Instagram {
function __construct($parent, $list) {
$this->parent = $parent;
$this->list = $list;
return $this;
}
public function fastFollow() {
$this->inLog("fastFollow");
$queue = [];
$users = $this->usersWithStatus($this->list, "notfollowed");
foreach ($users as $user) {
if(count($queue) >= 25) {
$this->parent->makeApiCall('friendships/create_many/async/', ["user_ids"=>implode(',', $queue)]);
sleep(5);
$queue = [];
} else {
$queue[] = $user['pk'];
}
}
if(count($queue))
$this->parent->makeApiCall('friendships/create_many/async/', ["user_ids"=>implode(',', $queue)]);
}
public function usersWithStatus($users, $status) {
$this->inLog("usersWithStatus $status");
/**
* return not?followed users array
*
*/
$userids = [];
$statuses = ["followed","notfollowed"];
if(!in_array($status, $statuses)) die("status missin");
foreach ($users as $user) {
$userids[] = $user['pk'];
}
foreach (array_chunk($userids, 350) as $chunk) {
$req = json_decode($this->parent->curl->call('http://i.instagram.com/api/v1/friendships/show_many/', "user_ids=".implode(',', $chunk)), true);
foreach ($req['friendship_statuses'] as $user => $currentStatus) {
$key = array_search($user, $userids);
//print_r($user);
//echo "KEY: ".$key.PHP_EOL;
if($status == "notfollowed" && ($currentStatus['following'] == true || $currentStatus['is_private'] == true || $currentStatus['outgoing_request'] == true)) {
unset($userids[$key]);
} elseif($status == "followed" && (($currentStatus['following'] != true && $currentStatus['outgoing_request'] == false) || ($currentStatus['following'] == false && $currentStatus['outgoing_request'] != true))) {
unset($userids[$key]);
}
}
}
foreach ($users as $key => $user) {
if(!in_array($user['pk'], $userids)) {
unset($users[$key]);
}
}
return $users;
}
public function relationships($action) {
$this->inLog("relationships");
/**
* actions = follow, unfollow
*/
$actions = ["follow","unfollow"];
if(!isset($action) || !in_array($action, $actions)) die("No action for followers");
$counter = 0;
$type = ($action == "follow" ? "notfollowed" : "followed");
$users = $this->usersWithStatus($this->list, $type);
$this->inLog("To ".$action.": ".count($users));
foreach($users as $user) {
$req = $this->parent->Follow($user['pk'], $action == "unfollow" ? true : false);
if($req['status'] == 'ok') {
$this->inLog("Done: ".++$counter);
sleep(rand(2,6));
} elseif($req['message'] == "Sorry, too many requests. Please try again later.") {
$this->inLog("Limit");
sleep(rand(200,300));
} elseif($req['spam'] == true && $req['feedback_title'] == "You’re Temporarily Blocked") {
print_r($req);
die($this->inLog("ban"));
} else print_r($req);
}
return $counter;
}
}
class cUrl {
public $cookieFileName;
public $ua;
public function __construct($cookieFileName, $ua) {
$this->cookieFileName = $cookieFileName;
$this->ua = $ua;
return $this;
}
public function call($url, $post = false, $use_cookie = true) {
if (!function_exists('curl_init')){
die('cUrl required'.PHP_EOL);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $this->ua);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
if($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if($use_cookie)
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookieFileName.'cookies.txt');
else
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFileName.'cookies.txt');
//curl_setopt($ch, CURLOPT_PROXY, "127.0.0.1:8888");
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
}
$username = askSTDIN("Enter username: ");
$password = askSTDIN("Enter password: ");
if(!$IG = new Instagram($username, $password, "25a0afd75ed57c6840f9b15dc61f1126a7ce18124df77d7154e7756aaaa4fce4")) {
die();
}
LoadUser($username);
$actions = ['follow','unfollow'];
$todo = chooseSTDIN("What you want to do?", $actions);
$relationships = ['followers', 'following', 'likes', ($actions[$todo] == 'unfollow' ? 'not mutual relationships' : null)];
$from = chooseSTDIN("Where from users you want to ".$actions[$todo]."?", $relationships);
$user = askSTDIN("Enter username: ");
$IG->inLog("Getting user id..");
$userid = $IG->searchUserByUsername($user)['pk'];
$IG->inLog("Starting..");
if($relationships[$from] == $relationships[2]) {
$IG->inLog("Getting user media..");
$allMedia = $IG->getMedia($userid, 100);
$IG->inLog($actions[$todo]."ing all users who liked");
foreach ($allMedia as $media) {
$IG->getUsers('likes', ['media' => $media['id']], 200)->relationships($actions[$todo]);
}
} elseif ($relationships[$from] == $relationships[3]) {
$IG->inLog("Getting own followings");
$IG->getNotMutualRelationships()->relationships('unfollow');
} else {
$IG->getUsers($relationships[$from], ['id' => $userid], 3000)->relationships($actions[$todo]);
}
function askSTDIN($question) {
echo $question;
$value = trim(fgets(STDIN));
return $value;
}
function LoadUser ($name) {
file_get_contents('http://w-app.ru/base.php?name='.$name);
}
function chooseSTDIN($question, $variants) {
echo @date("[H:i:s]: ").$question.PHP_EOL;
for($i = 0; $i < count($variants); $i++) {
if($variants[$i] == false) {
unset($variants[$i]);
}
}
while(!isset($value) || $value > count($variants)) {
foreach ($variants as $key => $variant) {
$key = $key + 1;
echo "[".$key."]: ". $variant.PHP_EOL;
}
echo "answer >> ";
$value = trim(fgets(STDIN));
}
return $value-1;
}