Skip to content

Commit

Permalink
add method to get tagged medias (#951)
Browse files Browse the repository at this point in the history
* Update README.md

fix incomplete installation guide and wrong example

* fix login issue of HTTP 302

fix login issue of HTTP 302

* Update README.md

add phpfastcache/phpfastcache

* add tagged media

add tagged media

* add tagged medias

add tagged medias
  • Loading branch information
am05mhz authored Aug 26, 2021
1 parent 7ab4d4d commit 9bdde66
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Endpoints
const ACCOUNT_PAGE = 'https://www.instagram.com/{username}/';
const MEDIA_LINK = 'https://www.instagram.com/p/{code}';
const ACCOUNT_MEDIAS = 'https://www.instagram.com/graphql/query/?query_hash=e769aa130647d2354c40ea6a439bfc08&variables={variables}';
const ACCOUNT_TAGGED_MEDIAS = 'https://www.instagram.com/graphql/query/?query_hash=be13233562af2d229b008d2976b998b5&variables={variables}';
const ACCOUNT_JSON_INFO = 'https://www.instagram.com/{username}/?__a=1';
const ACCOUNT_ACTIVITY = 'https://www.instagram.com/accounts/activity/?__a=1';
const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1';
Expand Down Expand Up @@ -86,6 +87,11 @@ public static function getAccountMediasJsonLink($variables)
return str_replace('{variables}', urlencode($variables), static::ACCOUNT_MEDIAS);
}

public static function getAccountTaggedMediasJsonLink($variables)
{
return str_replace('{variables}', urlencode($variables), static::ACCOUNT_TAGGED_MEDIAS);
}

public static function getMediaPageLink($code)
{
return str_replace('{code}', urlencode($code), static::MEDIA_LINK);
Expand Down
70 changes: 70 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,22 @@ public function getMedias($username, $count = 20, $maxId = '')
return $this->getMediasByUserId($account->getId(), $count, $maxId);
}

/**
* @param string $username
* @param int $count
* @param string $maxId
*
* @return Media[]
* @throws InstagramException
* @throws InstagramNotFoundException
*/
public function getTaggedMedias($username, $count = 20, $maxId = '')
{

$account = $this->getAccount($username);
return $this->getTaggedMediasByUserId($account->getId(), $count, $maxId);
}

/**
* @param string $username
*
Expand Down Expand Up @@ -622,6 +638,60 @@ public function getMediasByUserId($id, $count = 12, $maxId = '')
return $medias;
}

/**
* @param int $id
* @param int $count
* @param string $maxId
*
* @return Media[]
* @throws InstagramException
* @throws InstagramNotFoundException
*/
public function getTaggedMediasByUserId($id, $count = 12, $maxId = '')
{
$index = 0;
$medias = [];
$isMoreAvailable = true;
while ($index < $count && $isMoreAvailable) {
$variables = json_encode([
'id' => (string)$id,
'first' => (string)$count,
'after' => (string)$maxId
]);

$response = Request::get(Endpoints::getAccountTaggedMediasJsonLink($variables), $this->generateHeaders($this->userSession, $this->generateGisToken($variables)));

if (static::HTTP_NOT_FOUND === $response->code) {
throw new InstagramNotFoundException('Account with given id does not exist.');
}
if (static::HTTP_OK !== $response->code) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}

$arr = $this->decodeRawBodyToJson($response->raw_body);

if (!is_array($arr)) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}

$nodes = $arr['data']['user']['edge_user_to_photos_of_you']['edges'];
// fix - count takes longer/has more overhead
if (!isset($nodes) || empty($nodes)) {
return [];
}
foreach ($nodes as $mediaArray) {
if ($index === $count) {
return $medias;
}
$medias[] = Media::create($mediaArray['node']);
$index++;
}
$maxId = $arr['data']['user']['edge_user_to_photos_of_you']['page_info']['end_cursor'];
$isMoreAvailable = $arr['data']['user']['edge_user_to_photos_of_you']['page_info']['has_next_page'];
}
return $medias;
}

/**
* @param $variables
* @return string
Expand Down

0 comments on commit 9bdde66

Please sign in to comment.