Skip to content

Commit

Permalink
added method getTaggedMedias()
Browse files Browse the repository at this point in the history
  • Loading branch information
mewlabs committed May 30, 2019
1 parent 97a5114 commit 58d5dc1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": ">=5.4.0",
"php": "5.6",
"mewlabs/unirest-php": "dev-master",
"phpFastCache/phpFastCache": "5.0.*",
"ext-curl": "*",
Expand Down
16 changes: 16 additions & 0 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Endpoints

const ACCOUNT_MEDIAS2 = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after=';

const ACCOUNT_TAGGED_MEDIA = 'https://www.instagram.com/graphql/query/?query_hash=ff260833edf142911047af6024eb634a&variables={variables}';

// Look alike??
const URL_SIMILAR = 'https://www.instagram.com/graphql/query/?query_id=17845312237175864&id=4663052';

Expand Down Expand Up @@ -110,6 +112,20 @@ public static function getMediasJsonByLocationIdLink($facebookLocationId, $maxId
. ($maxId ? ('&max_id=' . $maxId) : '');
}

public static function getAccountTaggedMediaLink($uid, $after = '', $count = 12)
{
$variables = [
'id' => $uid,
'first' => $count,
];

if ($after) {
$variables['after'] = $after;
}

return str_replace('{variables}', urlencode(json_encode($variables)), static::ACCOUNT_TAGGED_MEDIA);
}

public static function getMediasJsonByTagLink($tag, $maxId = '')
{
$url = str_replace('{tag}', urlencode($tag), static::MEDIA_JSON_BY_TAG);
Expand Down
55 changes: 55 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,61 @@ public function getMediasByLocationId($facebookLocationId, $quantity = 24, $offs
return $medias;
}

/**
* @param integer $uid
* @param string $after
* @param integer $count
*
* @return array
* @throws InstagramException
*/
public function getTaggedMedias($uid, $after = '', $count = 12)
{
$medias = [];

$toReturn = [
'medias' => $medias,
'maxId' => $after,
'hasNextPage' => true,
];

$response = Request::get(Endpoints::getAccountTaggedMediaLink($uid, $after, $count),
$this->generateHeaders($this->userSession));

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

$this->parseCookies($response->headers);

$arr = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);

if (!is_array($arr)) {
throw new InstagramException('Response decoding failed. Returned data corrupted or this library outdated. Please report issue');
}

if (empty($nodes = $arr['data']['user']['edge_user_to_photos_of_you']['edges'])) {
return $toReturn;
}

foreach ($nodes as $mediaArray) {
$medias[] = Media::create($mediaArray['node']);
}

$after = $arr['data']['user']['edge_user_to_photos_of_you']['page_info']['end_cursor'];
$hasNextPage = $arr['data']['user']['edge_user_to_photos_of_you']['page_info']['has_next_page'];
$count = $arr['data']['user']['edge_user_to_photos_of_you']['count'];

$toReturn = [
'medias' => $medias,
'count' => $count,
'maxId' => $after,
'hasNextPage' => $hasNextPage,
];

return $toReturn;
}

/**
* @param string $facebookLocationId
* @param string $maxId
Expand Down

0 comments on commit 58d5dc1

Please sign in to comment.