Skip to content

Commit

Permalink
Fixes #108 get location medias
Browse files Browse the repository at this point in the history
  • Loading branch information
raiym committed Nov 26, 2018
1 parent 77efeaf commit 1316eb3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
50 changes: 26 additions & 24 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 . '/',
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;",
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/InstagramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 1316eb3

Please sign in to comment.