Skip to content

Commit

Permalink
Fixes #193, #195, #196. getMedias() not working
Browse files Browse the repository at this point in the history
  • Loading branch information
raiym committed Nov 8, 2017
1 parent e5e53e1 commit 56294f4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
13 changes: 6 additions & 7 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Endpoints
const LOGIN_URL = 'https://www.instagram.com/accounts/login/ajax/';
const ACCOUNT_PAGE = 'https://www.instagram.com/{username}';
const MEDIA_LINK = 'https://www.instagram.com/p/{code}';
const ACCOUNT_MEDIAS = 'https://www.instagram.com/{username}/media/?max_id={max_id}';
const ACCOUNT_MEDIAS = 'https://www.instagram.com/{username}/?__a=1&max_id={max_id}';
const ACCOUNT_JSON_INFO = 'https://www.instagram.com/{username}/?__a=1';
const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1';
const MEDIA_JSON_BY_LOCATION_ID = 'https://www.instagram.com/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}';
Expand Down Expand Up @@ -109,19 +109,18 @@ public static function getFollowUrl($accountId)
$url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOW_URL);
return $url;
}

public static function getFollowersJsonLink($accountId, $count, $after = '')
{
$url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOWERS_URL);
$url = str_replace('{{count}}', urlencode($count), $url);
if ($after === '') {

if ($after === '') {
$url = str_replace('&after={{after}}', '', $url);
}
else {
} else {
$url = str_replace('{{after}}', urlencode($after), $url);
}

return $url;
}
}
11 changes: 6 additions & 5 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,23 @@ public function getMedias($username, $count = 20, $maxId = '')
if (!is_array($arr)) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}
$nodes = $arr['user']['media']['nodes'];
// fix - count takes longer/has more overhead
if (empty($arr['items']) || !isset($arr['items'])) {
if (!isset($nodes) || empty($nodes)) {
return [];
}
foreach ($arr['items'] as $mediaArray) {
foreach ($nodes as $mediaArray) {
if ($index === $count) {
return $medias;
}
$medias[] = Media::create($mediaArray);
$index++;
}
if (empty($arr['items']) || !isset($arr['items'])) {
if (empty($nodes) || !isset($nodes)) {
return $medias;
}
$maxId = $arr['items'][count($arr['items']) - 1]['id'];
$isMoreAvailable = $arr['more_available'];
$maxId = $nodes[count($nodes) - 1]['id'];
$isMoreAvailable = $arr['user']['media']['page_info']['has_next_page'];
}
return $medias;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/InstagramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function setUpBeforeClass()
'path' => $sessionFolder
]);
$instanceCache = CacheManager::getInstance('files');
self::$instagram = Instagram::withCredentials('raiym', 'uvebzdxgbkt2T5_K', $instanceCache);
self::$instagram = Instagram::withCredentials('raiym', 'youneverknow', $instanceCache);
self::$instagram->login();

}
Expand Down Expand Up @@ -78,13 +78,13 @@ public function testGetMediaByUrl()

public function testGetLocationTopMediasById()
{
$medias = self::$instagram->getLocationTopMediasById(1);
$medias = self::$instagram->getCurrentTopMediasByTagName(1);
$this->assertEquals(9, count($medias));
}

public function testGetLocationMediasById()
{
$medias = self::$instagram->getLocationMediasById(1);
$medias = self::$instagram->getMediasByLocationId(1);
$this->assertEquals(12, count($medias));
}

Expand All @@ -96,7 +96,7 @@ public function testGetLocationById()

public function testGetMediaByTag()
{
$medias = self::$instagram->getTopMediasByTagName('hello');
$medias = self::$instagram->getMediasByTag('hello');
echo json_encode($medias);
}

Expand Down

0 comments on commit 56294f4

Please sign in to comment.