Skip to content

Commit

Permalink
Merge pull request #1 from postaddictme/master
Browse files Browse the repository at this point in the history
Branches merging
  • Loading branch information
aik27 authored Mar 14, 2018
2 parents 6796b97 + 6a4b982 commit b047a7f
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 4 deletions.
13 changes: 13 additions & 0 deletions examples/getAccountFollowings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', 'path/to/cache/folder');
$instagram->login();
sleep(2); // Delay to mimic user

$username = 'kevin';
$followers = [];
$account = $instagram->getAccount($username);
sleep(1);
$followers = $instagram->getFollowing($account->getId(), 1000, 100, true); // Get 1000 followings of 'kevin', 100 a time with random delay between requests
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';
39 changes: 39 additions & 0 deletions examples/getSidecarMediaByUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

function printMediaInfo(\InstagramScraper\Model\Media $media, $padding = '') {
echo "${padding}Id: {$media->getId()}\n";
echo "${padding}Shotrcode: {$media->getShortCode()}\n";
echo "${padding}Created at: {$media->getCreatedTime()}\n";
echo "${padding}Caption: {$media->getCaption()}\n";
echo "${padding}Number of comments: {$media->getCommentsCount()}\n";
echo "${padding}Number of likes: {$media->getLikesCount()}\n";
echo "${padding}Get link: {$media->getLink()}\n";
echo "${padding}High resolution image: {$media->getImageHighResolutionUrl()}\n";
echo "${padding}Media type (video/image/sidecar): {$media->getType()}\n";
}

// If account is public you can query Instagram without auth
$instagram = new \InstagramScraper\Instagram();

// If account is private and you subscribed to it firstly login
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
$instagram->login();

$media = $instagram->getMediaByUrl('https://www.instagram.com/p/BQ0lhTeAYo5');
echo "Media info:\n";
printMediaInfo($media);

$padding = ' ';
echo "Sidecar medias info:\n";
foreach ($media->getSidecarMedias() as $sidecarMedia) {
printMediaInfo($sidecarMedia, $padding);
echo "\n";
}

$account = $media->getOwner();
echo "Account info:\n";
echo "Id: {$account->getId()}\n";
echo "Username: {$account->getUsername()}\n";
echo "Full name: {$account->getFullName()}\n";
echo "Profile pic url: {$account->getProfilePicUrl()}\n";
46 changes: 42 additions & 4 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Instagram
private $sessionUsername;
private $sessionPassword;
private $userSession;
private $userAgent = null;

public $pagingTimeLimitSec = self::PAGING_TIME_LIMIT_SEC;
public $pagingDelayMinimumMicrosec = self::PAGING_DELAY_MINIMUM_MICROSEC;
Expand All @@ -50,6 +51,7 @@ public static function withCredentials($username, $password, $sessionFolder = nu
if (is_string($sessionFolder)) {
CacheManager::setDefaultConfig([
'path' => $sessionFolder,
'ignoreSymfonyNotice' => true,
]);
static::$instanceCache = CacheManager::getInstance('files');
} else {
Expand Down Expand Up @@ -152,6 +154,35 @@ public function searchAccountsByUsername($username)
return $accounts;
}

/**
* @param $userAgent
*
* @return string
*/
public function setUserAgent($userAgent)
{
return $this->userAgent = $userAgent;
}

/**
* @param $userAgent
*
* @return null
*/
public function resetUserAgent($userAgent)
{
return $this->userAgent = null;
}

/**
*
* @return string
*/
public function getUserAgent()
{
return $this->userAgent;
}

/**
* @param $session
*
Expand All @@ -166,11 +197,17 @@ private function generateHeaders($session)
$cookies .= "$key=$value; ";
}
$headers = [
'cookie' => $cookies,
'referer' => Endpoints::BASE_URL . '/',
'cookie' => $cookies,
'referer' => Endpoints::BASE_URL . '/',
'x-csrftoken' => $session['csrftoken'],
];
}

if($this->getUserAgent())
{
$headers['user-agent'] = $this->getUserAgent();
}

return $headers;
}

Expand Down Expand Up @@ -697,8 +734,9 @@ public function getCurrentTopMediasByTagName($tagName)
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
$medias = [];
foreach ($jsonResponse['tag']['top_posts']['nodes'] as $mediaArray) {
$medias[] = Media::create($mediaArray);
$nodes = (array) @$jsonResponse['graphql']['hashtag']['edge_hashtag_to_media']['edges'];
foreach ($nodes as $mediaArray) {
$medias[] = Media::create($mediaArray['node']);
}
return $medias;
}
Expand Down
23 changes: 23 additions & 0 deletions src/InstagramScraper/Model/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class Account extends AbstractModel
*/
protected $profilePicUrl = '';

/**
* Profile picture url HD
* @var string
*/
protected $profilePicUrlHd = '';

/**
* Information filled by user
* @var string
Expand Down Expand Up @@ -123,6 +129,20 @@ public function getProfilePicUrl()
return $this->profilePicUrl;
}

/**
* @return string
*/
public function getProfilePicUrlHd()
{
$toReturn = $this->profilePicUrl;

if ($this->profilePicUrlHd !== '') {
$toReturn = $this->profilePicUrlHd;
}

return $toReturn;
}

/**
* @return string
*/
Expand Down Expand Up @@ -199,6 +219,9 @@ protected function initPropertiesCustom($value, $prop, $array)
case 'profile_pic_url':
$this->profilePicUrl = $value;
break;
case 'profile_pic_url_hd':
$this->profilePicUrlHd = $value;
break;
case 'biography':
$this->biography = $value;
break;
Expand Down
26 changes: 26 additions & 0 deletions src/InstagramScraper/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ class Media extends AbstractModel
*/
protected $commentsCount = 0;

/**
* @var Media[]|array
*/
protected $sidecarMedias = [];

/**
* @param string $code
*
Expand Down Expand Up @@ -366,6 +371,14 @@ public function getCommentsCount()
return $this->commentsCount;
}

/**
* @return Media[]|array
*/
public function getSidecarMedias()
{
return $this->sidecarMedias;
}

/**
* @param $value
* @param $prop
Expand Down Expand Up @@ -495,6 +508,19 @@ protected function initPropertiesCustom($value, $prop, $arr)
}
}
break;
case 'edge_sidecar_to_children':
if (!is_array($arr[$prop]['edges'])) {
break;
}

foreach ($arr[$prop]['edges'] as $edge) {
if (!isset($edge['node'])) {
continue;
}

$this->sidecarMedias[] = static::create($edge['node']);
}
break;
case 'owner':
$this->owner = Account::create($arr[$prop]);
break;
Expand Down

0 comments on commit b047a7f

Please sign in to comment.