Skip to content

Commit

Permalink
Get storie reel high light location (#862)
Browse files Browse the repository at this point in the history
* changed Exemple getHighights to demonstrate how to get Stories

* added function getHighlightStories with exemple

* removed passwd

* Implementet getStories method with 3 arguments, backwards compatible

* typing error

Co-authored-by: ricdijk <richard@vandijkonlinenl>
  • Loading branch information
ricdijk and ricdijk authored Jan 18, 2021
1 parent 78f6860 commit cdcb044
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 71 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ composer.lock
.DS_Store
phpunit.phar
tests/sessions
phpunit.xml
phpunit.xml
settings.json
6 changes: 5 additions & 1 deletion examples/getHighlights.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'user', 'passwd', new Psr16Adapter('Files'));
$settings=json_decode(file_get_contents('settings.json'));
$username = $settings->username;
$password = $settings->password;

$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), $username, $password, new Psr16Adapter('Files'));
$instagram->login();

//$userId = $instagram->getAccount('instagram')->getId();
Expand Down
7 changes: 6 additions & 1 deletion examples/getStories.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));

$settings=json_decode(file_get_contents('settings.json'));
$username = $settings->username;
$password = $settings->password;

$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), $username, $password, new Psr16Adapter('Files'));
$instagram->login();

$stories = $instagram->getStories();
Expand Down
8 changes: 6 additions & 2 deletions examples/getStoriesFromUserStories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
require __DIR__ . '/../vendor/autoload.php';
use Phpfastcache\Helper\Psr16Adapter;

$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'user', 'passed', new Psr16Adapter('Files'));
$settings=json_decode(file_get_contents('settings.json'));
$username = $settings->username;
$password = $settings->password;

$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), $username, $password, new Psr16Adapter('Files'));
$instagram->login();
$instagram->saveSession();
$instagram->saveSession(10*24*3600);


// *************************** get storie from unsetStories **********************************
Expand Down
119 changes: 53 additions & 66 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ private static function getErrorBody($rawError)
if (is_object($rawError)) {
$str = '';
foreach ($rawError as $key => $value) {
//to avoid erros in an error function make sure all are string before concat operation
if (is_array($value)) $value=json_encode((array)$value);
if (is_object($value)) $value=json_encode((array)$value);
if (is_array($key)) $key=json_encode((array)$key);
if (is_object($key)) $key=json_encode((array)$key);
$str .= ' ' . $key . ' => ' . $value . ';';
}
return $str;
Expand Down Expand Up @@ -1683,16 +1688,23 @@ public function getPaginateAllFollowing($accountId, $pageSize = 20, $nextPage =
'accounts' => $accounts
];
}

/**
* @param array $reel_ids - array of instagram user ids
* @return array
* @throws InstagramException
*/
public function getStories($reel_ids = null)
public function getStories($reel_ids = null, $highlight_reel_ids=[], $location_ids=[])
{
if (empty($reel_ids) ) $reel_ids = [];
elseif (!is_array($reel_ids)) $reel_ids = [$reel_ids];
if (!is_array($highlight_reel_ids)) $highlight_reel_ids = [$highlight_reel_ids];
if (!is_array($location_ids)) $location_ids = [$location_ids];
//-------------------------------------------------------------------------------------------------------------
// if no arguments: get user stories
//-------------------------------------------------------------------------------------------------------------
$variables = ['precomposed_overlay' => false, 'reel_ids' => []];
if (empty($reel_ids)) {
if ($reel_ids == [] && $highlight_reel_ids == [] && $location_ids==[]) {

$response = Request::get(Endpoints::getUserStoriesLink($variables),
$this->generateHeaders($this->userSession));

Expand All @@ -1707,36 +1719,44 @@ public function getStories($reel_ids = null)
}

foreach ($jsonResponse['data']['user']['feed_reels_tray']['edge_reels_tray_to_reel']['edges'] as $edge) {
$variables['reel_ids'][] = $edge['node']['id'];
}
} else {
$variables['reel_ids'] = $reel_ids;
}

$response = Request::get(Endpoints::getStoriesLink($variables),
$this->generateHeaders($this->userSession));

if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}

$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);

if (empty($jsonResponse['data']['reels_media'])) {
return [];
}
$reel_ids[] = $edge['node']['id'];
}
}
//-------------------------------------------------------------------------------------------------------------
// Get stories from reel_ids (users), highlight_ids and/or location_ids
//-------------------------------------------------------------------------------------------------------------
$variables = [
'highlight_reel_ids'=> $highlight_reel_ids,
'reel_ids'=> $reel_ids,
'location_ids'=> $location_ids,
'precomposed_overlay'=> False,
];

$url = Endpoints::HIGHLIGHT_STORIES . '&variables=' . json_encode($variables);
$response = Request::get($url, $this->generateHeaders($this->userSession));

if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}

$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);

if (empty($jsonResponse['data']['reels_media'])) {
return [];
}

$stories = [];
foreach ($jsonResponse['data']['reels_media'] as $highlight) {
$UserStories = UserStories::create();
$UserStories->setOwner(Account::create($highlight['owner']));
foreach ($highlight['items'] as $item) {
$UserStories->addStory(Story::create($item));
}
$stories[] = $UserStories;
}
return $stories;
}

$stories = [];
foreach ($jsonResponse['data']['reels_media'] as $user) {
$UserStories = UserStories::create();
$UserStories->setOwner(Account::create($user['user']));
foreach ($user['items'] as $item) {
$UserStories->addStory(Story::create($item));
}
$stories[] = $UserStories;
}
return $stories;
}

/**
* @param bool $force
Expand Down Expand Up @@ -2170,40 +2190,7 @@ public function getHighlights($userId)
*/
public function getHighlightStories($highlight_reel_ids)
{
if (!is_array($highlight_reel_ids)) {
$highlight_reel_ids = [$highlight_reel_ids];
}

$variables = [
'highlight_reel_ids'=> $highlight_reel_ids,
'reel_ids'=> [],
'location_ids'=> [],
'precomposed_overlay'=> False,
];

$url = Endpoints::HIGHLIGHT_STORIES . '&variables=' . json_encode($variables);
$response = Request::get($url, $this->generateHeaders($this->userSession));

if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}

$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);

if (empty($jsonResponse['data']['reels_media'])) {
return [];
}

$stories = [];
foreach ($jsonResponse['data']['reels_media'] as $highlight) {
$UserStories = UserStories::create();
$UserStories->setOwner(Account::create($highlight['owner']));
foreach ($highlight['items'] as $item) {
$UserStories->addStory(Story::create($item));
}
$stories[] = $UserStories;
}
return $stories;
return $this->getStories([], $highlight_reel_ids, []);
}

/**
Expand Down

0 comments on commit cdcb044

Please sign in to comment.