Skip to content

Commit

Permalink
Fixes #59. Incorrect number of results returned for getMediasByTag();
Browse files Browse the repository at this point in the history
  • Loading branch information
raiym committed Feb 23, 2017
1 parent 497529a commit 7e3a1dc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 3 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
try {
// $medias = Instagram::getMedias('kevin', 1497);
// echo json_encode($medias[1497]);
$media = Instagram::getMediaByCode('BL0k1EXhElI');
echo json_encode($media);
$medias = InstagramScraper\Instagram::getMediasByTag('paveldurov', 300);
echo sizeof($medias) . '\n';
// echo json_encode($medias);
} catch (\Exception $ex) {
print_r($ex);
}
Expand Down
18 changes: 12 additions & 6 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public static function getMediasByTag($tag, $count = 12, $maxId = '')
{
$index = 0;
$medias = [];
$mediaIds = [];
$hasNextPage = true;
while ($index < $count && $hasNextPage) {
$response = Request::get(Endpoints::getMediasJsonByTagLink($tag, $maxId));
Expand All @@ -204,7 +205,12 @@ public static function getMediasByTag($tag, $count = 12, $maxId = '')
if ($index === $count) {
return $medias;
}
$medias[] = Media::fromTagPage($mediaArray);
$media = Media::fromTagPage($mediaArray);
if (in_array($media->id, $mediaIds)) {
return $medias;
}
$mediaIds[] = $media->id;
$medias[] = $media;
$index++;
}
if (count($nodes) == 0) {
Expand Down Expand Up @@ -253,14 +259,14 @@ public static function getPaginateMediasByTag($tag, $maxId = '')
$medias[] = Media::fromTagPage($mediaArray);
}

$maxId = $arr['tag']['media']['page_info']['end_cursor'];
$maxId = $arr['tag']['media']['page_info']['end_cursor'];
$hasNextPage = $arr['tag']['media']['page_info']['has_next_page'];
$count = $arr['tag']['media']['count'];
$count = $arr['tag']['media']['count'];

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

Expand Down

0 comments on commit 7e3a1dc

Please sign in to comment.