<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once "vendor/autoload.php";
require_once __DIR__."/../xpanel/inc/vt.php";
require_once __DIR__."/instafunc.php";

use Instagram\Api;
use Instagram\Model\Media;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
 
$instaAyar = new instaAyar(); 
$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/../cache');
$api = new Api($cachePool);

$api->login($instaAyar->loginUser,$instaAyar->loginPassword );  
 
$profile = $api->getProfile($instaAyar->cekilcekUser);

$medias = $profile->getMedias();
 
$i=0;


foreach ($medias as $media) {
   /** @var Instagram\Model\Media $media */
   $resimm= __DIR__."/../img/urunler/".md5($media->getId()).".jpg";
   if(!file_exists($resimm)){
       file_put_contents( $resimm,file_get_contents($media->getDisplaySrc()));
       echo $media->getDisplaySrc()."\n"; 
   }
    
   fotogir($media->getId(), md5($media->getId()).".jpg",$media->getCaption(),$media->getLink());
   $i++;
}  

 

 if(($instaAyar->count)<35){


    do {
        $profile = $api->getMoreMedias($profile);
        $medias = $profile->getMedias();
        foreach ($medias as $media) {
           
           //
            /** @var Instagram\Model\Media $media */
            $resimm= __DIR__."/../img/urunler/".md5($media->getId()).".jpg";
            if(!file_exists($resimm)){
                file_put_contents( $resimm,file_get_contents($media->getDisplaySrc()));
                echo $media->getDisplaySrc()."|".$i."\n";
            }  
            fotogir($media->getId(), md5($media->getId()).".jpg",$media->getCaption(),$media->getLink());

    
            if($i++>40)break; 
        }
        sleep(1); 
        if($i++>39)break; 
    }while ($profile->hasMoreMedias());

 }
 
 

 /* 40 dan nekadar fazlaysa sil  */

if($instaAyar->count>$instaAyar->veri_limit){
 eneskiyisil(($instaAyar->count)-$instaAyar->veri_limit);
}


