From 1316eb3de5d2b21af202c470b3bc1280982f213c Mon Sep 17 00:00:00 2001 From: raiym Date: Mon, 26 Nov 2018 23:40:54 +0400 Subject: [PATCH] Fixes #108 get location medias --- src/InstagramScraper/Instagram.php | 50 ++++++++++++++++-------------- tests/InstagramTest.php | 4 +-- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 47d42c01..847e829a 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -136,12 +136,15 @@ public static function setAccountMediasRequestCount($count) { Endpoints::setAccountMediasRequestCount($count); } - /** - * Set custom curl opts - */ - public static function curlOpts($opts) { - Request::curlOpts($opts); - } + + /** + * Set custom curl opts + */ + public static function curlOpts($opts) + { + Request::curlOpts($opts); + } + /** * @param array $config */ @@ -229,9 +232,9 @@ private function generateHeaders($session, $gisToken = null) foreach ($session as $key => $value) { $cookies .= "$key=$value; "; } - + $csrf = empty($session['csrftoken']) ? $session['x-csrftoken'] : $session['csrftoken']; - + $headers = [ 'cookie' => $cookies, 'referer' => Endpoints::BASE_URL . '/', @@ -1000,7 +1003,7 @@ public function getCurrentTopMediasByLocationId($facebookLocationId) * @return Media[] * @throws InstagramException */ - public function getMediasByLocationId($facebookLocationId, $quantity = 12, $offset = '') + public function getMediasByLocationId($facebookLocationId, $quantity = 24, $offset = '') { $index = 0; $medias = []; @@ -1014,20 +1017,19 @@ public function getMediasByLocationId($facebookLocationId, $quantity = 12, $offs $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $arr = $this->decodeRawBodyToJson($response->raw_body); - $nodes = $arr['location']['media']['nodes']; - printf($nodes); + $nodes = $arr['graphql']['location']['edge_location_to_media']['edges']; foreach ($nodes as $mediaArray) { if ($index === $quantity) { return $medias; } - $medias[] = Media::create($mediaArray); + $medias[] = Media::create($mediaArray['node']); $index++; } if (empty($nodes)) { return $medias; } - $hasNext = $arr['location']['media']['page_info']['has_next_page']; - $offset = $arr['location']['media']['page_info']['end_cursor']; + $hasNext = $arr['graphql']['location']['edge_location_to_media']['page_info']['has_next_page']; + $offset = $arr['graphql']['location']['edge_location_to_media']['page_info']['end_cursor']; } return $medias; } @@ -1270,11 +1272,11 @@ public function login($force = false, $support_two_step_verification = false) if (isset($match[1])) { $csrfToken = $match[1]; } - if(isset($response->headers['Set-Cookie'])): - $cookies = static::parseCookies( $response->headers['Set-Cookie'] ); - else: - $cookies = static::parseCookies( $response->headers['set-cookie'] ); - endif; + if (isset($response->headers['Set-Cookie'])): + $cookies = static::parseCookies($response->headers['Set-Cookie']); + else: + $cookies = static::parseCookies($response->headers['set-cookie']); + endif; $mid = $cookies['mid']; $headers = [ 'cookie' => "ig_cb=1; csrftoken=$csrfToken; mid=$mid;", @@ -1302,11 +1304,11 @@ public function login($force = false, $support_two_step_verification = false) } } - if(isset($response->headers['Set-Cookie'])): - $cookies = static::parseCookies( $response->headers['Set-Cookie'] ); - else: - $cookies = static::parseCookies( $response->headers['set-cookie'] ); - endif; + if (isset($response->headers['Set-Cookie'])): + $cookies = static::parseCookies($response->headers['Set-Cookie']); + else: + $cookies = static::parseCookies($response->headers['set-cookie']); + endif; $cookies['mid'] = $mid; $cachedString->set($cookies); static::$instanceCache->save($cachedString); diff --git a/tests/InstagramTest.php b/tests/InstagramTest.php index e3b9c415..859e98d2 100644 --- a/tests/InstagramTest.php +++ b/tests/InstagramTest.php @@ -91,8 +91,8 @@ public function testGetLocationTopMediasById() public function testGetLocationMediasById() { - $medias = self::$instagram->getMediasByLocationId(1); - $this->assertEquals(12, count($medias)); + $medias = self::$instagram->getMediasByLocationId(1, 56); + $this->assertEquals(56, count($medias)); } public function testGetLocationById()