Guest User

Untitled

a guest
Oct 27th, 2017
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. include 'instagram.class.php';
  2.  
  3. $instaUsername = "xxxxx";
  4. $instaPassword = "xxxxx";
  5.  
  6. $folderLocation = "upload";
  7.  
  8. $attempts = 0;
  9. $loginSucces = false;
  10. $postImageSucces = false;
  11. $maxAttempts = 25;
  12.  
  13. // --------------------------------------------------------------------------------------------------------
  14. $captionOptions = ['caption 1', 'caption 2', 'caption 3', 'caption 4', 'caption 5'];
  15. $caption = $captionOptions[array_rand($captionOptions)];
  16. // --------------------------------------------------------------------------------------------------------
  17.  
  18. $files = glob($folderLocation.'/*.{jpg,png,gif,jpeg}', GLOB_BRACE);
  19. array_multisort(array_map('filemtime', $files), SORT_NUMERIC, SORT_DESC, $files);
  20.  
  21. if(empty($files)) {
  22. exit("No pictures found");
  23. }
  24.  
  25. $fullFileLocation = $files[0];
  26. $fileNameWithExtension = explode('/', $fullFileLocation)[1];
  27. $fileName = explode('.', $fileNameWithExtension)[0];
  28. $fileExtension = explode('.', $fileNameWithExtension)[1];
  29.  
  30. if(file_exists("$folderLocation/{$fileName}.txt")) {
  31. $caption = file_get_contents("$folderLocation/{$fileName}.txt");
  32. }
  33.  
  34.  
  35. // --------------------------------------------------------------------------------------------------------
  36. // login & upload
  37. $instaGram = new Instagram();
  38. do {
  39. if($attempts >= 15) {
  40. exit("15 times... error? ??");
  41.  
  42. }
  43.  
  44. if($loginSucces == false) {
  45. $response = $instaGram->Login($instaUsername, $instaPassword);
  46. if(strpos($response[1], "Sorry") || empty($response[1])) {
  47. // proxy fails, get a new one
  48. $instaGram->delProxy();
  49. } else {
  50. $loginSucces = true;
  51. sleep(5);
  52. }
  53. }
  54.  
  55.  
  56. if($loginSucces == true) {
  57. if($instaGram->Post($fullFileLocation, $caption)) {
  58. $postImageSucces = true;
  59. }
  60. }
  61.  
  62. $attempts++;
  63. } while($postImageSucces == false);
  64. // --------------------------------------------------------------------------------------------------------
  65.  
  66. // logn/upload succes
  67.  
  68.  
  69. // --------------------------------------------------------------------------------------------------------
  70. // rename files
  71.  
  72. rename("{$fullFileLocation}", "./gebruikt/{$fileNameWithExtension}");
  73.  
  74. // caption txt bestand
  75. if(file_exists("$folderLocation/{$fileName}.txt")) {
  76. rename("{$folderLocation}/{$fileName}.txt", "./gebruikt/{$fileName}.txt");
  77. }
  78. // --------------------------------------------------------------------------------------------------------
  79.  
  80. <?php
  81.  
  82. Class Instagram
  83. {
  84. public $username;
  85. public $password;
  86. private $guid;
  87. private $userAgent = 'Instagram 6.21.2 Android (19/4.4.2; 480dpi; 1152x1920; Meizu; MX4; mx4; mt6595; nl_BE)';
  88. private $instaSignature ='xxxxxxxxxxxxx';
  89. private $instagramUrl = 'https://i.instagram.com/api/v1/';
  90.  
  91. function __construct() {
  92. if (!extension_loaded('curl')) trigger_error('php_curl extension is not loaded', E_USER_ERROR);
  93. }
  94.  
  95. function __destruct() {
  96.  
  97. }
  98.  
  99. private function writeProxy($ip) {
  100. file_put_contents("./upload/proxy.txt", $ip);
  101. }
  102.  
  103. public function delProxy() {
  104. file_put_contents("./upload/proxy.txt", "");
  105. }
  106.  
  107. private function getProxyFromFile() {
  108. return file_get_contents("./upload/proxy.txt");
  109. }
  110.  
  111.  
  112.  
  113. private function getProxy() {
  114. $proxy = file_get_contents("./upload/proxy.txt");
  115. if(empty($proxy)) {
  116. $data = json_decode(file_get_contents('http://gimmeproxy.com/api/getProxy?country=NL&supportsHttps=true&maxCheckPeriod=15000&minSpeed=250'));
  117. $proxy = trim($data->ipPort);
  118. $this->writeProxy($proxy);
  119. } else {
  120. $proxy = $proxy;
  121. }
  122.  
  123. return $proxy;
  124. }
  125.  
  126. private function Request($url, $post, $post_data, $cookies) {
  127.  
  128. $ch = curl_init();
  129. curl_setopt($ch, CURLOPT_URL, $this->instagramUrl . $url);
  130. curl_setopt($ch, CURLOPT_PROXY, $this->getProxy());
  131. curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
  132. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  133. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  134. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  135. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  136. //curl_setopt($ch, CURLOPT_HEADER, 1);
  137. if($post) {
  138. curl_setopt($ch, CURLOPT_POST, 1);
  139. if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
  140. curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
  141. }
  142. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  143. }
  144.  
  145. if($cookies) {
  146. curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__). '/upload/cookies.txt');
  147. } else {
  148. curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__). '/upload/cookies.txt');
  149. }
  150. $response = curl_exec($ch);
  151. $http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  152. curl_close($ch);
  153. return array($http, $response);
  154. }
  155.  
  156. private function GenerateGuid() {
  157. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  158. mt_rand(0, 65535),
  159. mt_rand(0, 65535),
  160. mt_rand(0, 65535),
  161. mt_rand(16384, 20479),
  162. mt_rand(32768, 49151),
  163. mt_rand(0, 65535),
  164. mt_rand(0, 65535),
  165. mt_rand(0, 65535));
  166. }
  167.  
  168. private function GenerateSignature($data) {
  169. return hash_hmac('sha256', $data, $this->instaSignature);
  170. }
  171.  
  172.  
  173. private function GetPostData($path) {
  174. $post_data = array('device_timestamp' => time());
  175. if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
  176. $post_data['photo'] = new CURLFile(realpath($path));
  177. } else {
  178. $post_data['photo'] = "@".realpath($path);
  179. }
  180. return $post_data;
  181. }
  182.  
  183. public function Login($username, $password) {
  184. $this->username = $username;
  185. $this->password = $password;
  186. $this->guid = $this->GenerateGuid();
  187. $device_id = "android-" . $this->guid;
  188. $data = '{"device_id":"'.$device_id.'","guid":"'.$this->guid.'","username":"'. $this->username.'","password":"'.$this->password.'","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';
  189. $sig = $this->GenerateSignature($data);
  190. $data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
  191. return $this->Request('accounts/login/', true, $data, false);
  192. }
  193.  
  194. private function PostImage($photo) {
  195. $data = $this->GetPostData($photo);
  196. return $this->Request('media/upload/', true, $data, true);
  197. }
  198.  
  199. private function PostCaption($caption, $media_id) {
  200. $caption = preg_replace("/r|n/", "", $caption);
  201. $device_id = "android-".$this->guid;
  202. $data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","media_id":"'.$media_id.'","caption":"'.trim($caption).'","device_timestamp":"'.time().'","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';
  203. $sig = $this->GenerateSignature($data);
  204. $new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
  205. return $this->Request('media/configure/', true, $new_data, true);
  206. }
  207.  
  208.  
  209.  
  210. public function Post($photo, $caption){
  211. $response = $this->PostImage($photo);
  212. // print_r($response);
  213. if(empty($response[1])) {
  214. return false;
  215. }
  216.  
  217. $obj = @json_decode($response[1], true);
  218. $status = $obj['status'];
  219. if($status == 'ok') {
  220.  
  221. // Remove and line breaks from the caption
  222. $media_id = $obj['media_id'];
  223. $response = $this->PostCaption($caption, $media_id);
  224. return true;
  225. }
  226.  
  227. return false;
  228. }
  229. }
  230. ?>
Add Comment
Please, Sign In to add comment