Skip to content

Commit

Permalink
Highlights incl stories incl exemple (#857)
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

Co-authored-by: ricdijk <richard@vandijkonlinenl>
  • Loading branch information
ricdijk and ricdijk authored Jan 18, 2021
1 parent 91b8d5d commit 78f6860
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
42 changes: 38 additions & 4 deletions examples/getHighlights.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
<?php
ini_set('display_errors', 1);ini_set('display_startup_errors', 1);error_reporting(E_ALL);
use Phpfastcache\Helper\Psr16Adapter;

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

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

$userId = $instagram->getAccount('instagram')->getId();
//$userId = $instagram->getAccount('instagram')->getId();
$userId = 556625332;
echo "<pre>";

$highlights = $instagram->getHighlights($userId);
$hcount=0;
foreach ($highlights as $highlight) {
echo "Highlight info:\n";
$hcount++;
echo "\n------------------------------------------------------------------------------------------------------------------------\n";
echo "Highlight info ($hcount):\n";
echo "<img width=80 src='".$highlight->getImageThumbnailUrl()."'>\n";
echo "Id: {$highlight->getId()}\n";
echo "Title: {$highlight->getTitle()}\n";
echo "Image thumbnail url: {$highlight->getImageThumbnailUrl()}\n";
Expand All @@ -21,5 +28,32 @@
echo " Id: {$account->getId()}\n";
echo " Username: {$account->getUsername()}\n";
echo " Profile pic url: {$account->getProfilePicUrl()}\n";
echo "\n";

echo "------------------------------------------------------------------------------------------------------------------------\n";

$userStories=$instagram->getHighlightStories($highlight->getId());
for ($i=0; $i<count($userStories);$i++)
{
$stories = $userStories[$i]->getStories();
//$owner = $userStories[$i]->getOwner();
//echo "\n===========================================================";
//echo "\nUserStorie: " . $i;
//echo "\nId: " . $owner['id'];
//echo "\nUserName: " . $owner['username'];

//for each stories => get Story
for ($j=0; $j<count($stories);$j++)
{
$story = $stories[$j];
echo "\n--------------------------------------------------------";
echo "\nStorie: " . $j;
echo "\nId: " . $story['id'];
echo "\nCreation Time: " . $story['createdTime'];
echo "\nType: " . $story['type'];
echo "\n<img height=100 src=\"".$story['imageThumbnailUrl']."\">";

}
}

}
echo "</pre>";
1 change: 1 addition & 0 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Endpoints
const DELETE_COMMENT_URL = 'https://www.instagram.com/web/comments/{mediaId}/delete/{commentId}/';
const ACCOUNT_MEDIAS2 = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after=';
const HIGHLIGHT_URL = 'https://www.instagram.com/graphql/query/?query_hash=c9100bf9110dd6361671f113dd02e7d6&variables={"user_id":"{userId}","include_chaining":false,"include_reel":true,"include_suggested_users":false,"include_logged_out_extras":false,"include_highlight_reels":true,"include_live_status":false}';
const HIGHLIGHT_STORIES = 'https://www.instagram.com/graphql/query/?query_hash=45246d3fe16ccc6577e0bd297a5db1ab';
const THREADS_URL = 'https://www.instagram.com/direct_v2/web/inbox/?persistentBadging=true&folder=&limit={limit}&thread_message_limit={messageLimit}&cursor={cursor}';

// Look alike??
Expand Down
42 changes: 42 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,48 @@ public function getHighlights($userId)
}
return $highlights;
}
/**
* @param array $highlight_reel_ids - array of instagram highlight ids
* @return array
* @throws InstagramException
*/
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;
}

/**
* @param int $limit
Expand Down

0 comments on commit 78f6860

Please sign in to comment.