Skip to content

Commit

Permalink
Add getPaginateMediasByLocationId Function
Browse files Browse the repository at this point in the history
  • Loading branch information
david-kurniawan committed May 14, 2019
1 parent 8db1a95 commit f9075dd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add getPaginateMediasByLocationId Function
61 changes: 61 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,67 @@ public function getPaginateMediasByTag($tag, $maxId = '')
return $toReturn;
}

/**
* @param string $facebookLocationId
* @param string $maxId
*
* @return array
* @throws InstagramException
*/
public function getPaginateMediasByLocationId($facebookLocationId, $maxId = '')
{
$hasNextPage = true;
$medias = [];

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

$response = Request::get(Endpoints::getMediasJsonByLocationIdLink($facebookLocationId, $maxId),
$this->generateHeaders($this->userSession));

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

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

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

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

if (empty($arr['graphql']['location']['edge_location_to_media']['count'])) {
return $toReturn;
}

$nodes = $arr['graphql']['location']['edge_location_to_media']['edges'];

if (empty($nodes)) {
return $toReturn;
}

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

$maxId = $arr['graphql']['location']['edge_location_to_media']['page_info']['end_cursor'];
$hasNextPage = $arr['graphql']['location']['edge_location_to_media']['page_info']['has_next_page'];
$count = $arr['graphql']['location']['edge_location_to_media']['count'];

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

return $toReturn;
}

/**
* @param $tagName
*
Expand Down

0 comments on commit f9075dd

Please sign in to comment.