Skip to content

Commit

Permalink
Merge pull request #483 from David-Kurniawan/master
Browse files Browse the repository at this point in the history
Add getPaginateMediasByLocationId Function
  • Loading branch information
raiym authored May 18, 2019
2 parents adc7291 + b1c5871 commit 2599b88
Show file tree
Hide file tree
Showing 3 changed files with 71 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 @@ -940,6 +940,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
9 changes: 9 additions & 0 deletions tests/InstagramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ public function testAddAndDeleteComment()
self::$instagram->deleteComment('1663256735663694497', $comment1);
$this->assertTrue(true, 'Return type ensures this assertion is never reached on failure');
}

/**
* @group getPaginateMediasByLocationId
*/
public function testGetPaginateMediasByLocationId()
{
$medias = self::$instagram->getPaginateMediasByLocationId('201176299974017');
echo json_encode($medias);
}
// TODO: Add test getMediaById
// TODO: Add test getLocationById
}

0 comments on commit 2599b88

Please sign in to comment.