From 56294f48574a1380c5ff9a4bba6429854359cd83 Mon Sep 17 00:00:00 2001 From: raiym Date: Wed, 8 Nov 2017 21:08:43 +0900 Subject: [PATCH] Fixes #193, #195, #196. getMedias() not working --- src/InstagramScraper/Endpoints.php | 13 ++++++------- src/InstagramScraper/Instagram.php | 11 ++++++----- tests/InstagramTest.php | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index 6442c77f..45a5319e 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -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}}'; @@ -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; } } diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index d73e66cc..457a4386 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -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; } diff --git a/tests/InstagramTest.php b/tests/InstagramTest.php index 1fe22ed4..b28698ac 100644 --- a/tests/InstagramTest.php +++ b/tests/InstagramTest.php @@ -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(); } @@ -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)); } @@ -96,7 +96,7 @@ public function testGetLocationById() public function testGetMediaByTag() { - $medias = self::$instagram->getTopMediasByTagName('hello'); + $medias = self::$instagram->getMediasByTag('hello'); echo json_encode($medias); }