Skip to content

Commit

Permalink
Add method to fetch stories by highlights folder id
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrimaud committed Sep 21, 2021
1 parent bb2d1d4 commit 0b5c4e2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
28 changes: 28 additions & 0 deletions examples/story-highlights-by-id-folder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use Instagram\Api;
use Instagram\Exception\InstagramException;

use Psr\Cache\CacheException;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

require realpath(dirname(__FILE__)) . '/../vendor/autoload.php';
$credentials = include_once realpath(dirname(__FILE__)) . '/credentials.php';

$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/../cache');

try {
$api = new Api($cachePool);
$api->login($credentials->getLogin(), $credentials->getPassword());

// url is https://www.instagram.com/stories/highlights/17924739640580662/
$folder = $api->getStoriesOfHighlightsFolderById(17924739640580662);
dd($folder->getStories());

} catch (InstagramException $e) {
print_r($e->getMessage());
} catch (CacheException $e) {
print_r($e->getMessage());
}
16 changes: 16 additions & 0 deletions src/Instagram/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -713,4 +713,20 @@ public function commentPost(int $postId, string $message): string
$request = new CommentPost($this->client, $this->session);
return $request->comment($postId, $message);
}

/**
* @param int $folderId
*
* @return StoryHighlightsFolder
*
* @throws Exception\InstagramAuthException
* @throws Exception\InstagramFetchException
*/
public function getStoriesOfHighlightsFolderById(int $folderId): StoryHighlightsFolder
{
$storyFolder = new StoryHighlightsFolder();
$storyFolder->setId($folderId);

return $this->getStoriesOfHighlightsFolder($storyFolder);
}
}
8 changes: 6 additions & 2 deletions src/Instagram/Model/StoryHighlightsFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ public function addStory(StoryMedia $story): void
/**
* @return StoryMedia[]
*/
public function getStories(): array
public function getStories(bool $oldestInFirst = true): array
{
return array_reverse($this->stories);
if ($oldestInFirst) {
return array_reverse($this->stories);
} else {
return $this->stories;
}
}

public function orderStories(): void
Expand Down
27 changes: 27 additions & 0 deletions tests/StoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,31 @@ public function testHighlightsStoriesFetch()

$api->logout('username');
}

public function testHighlightsStoriesByIdFetch()
{
$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/cache');

$mock = new MockHandler([
new Response(200, ['Set-Cookie' => 'cookie'], file_get_contents(__DIR__ . '/fixtures/home.html')),
new Response(200, [], file_get_contents(__DIR__ . '/fixtures/login-success.json')),
new Response(200, [], file_get_contents(__DIR__ . '/fixtures/highlights-stories.json')),
]);

$handlerStack = HandlerStack::create($mock);
$client = new Client(['handler' => $handlerStack]);

$api = new Api($cachePool, $client);

// clear cache
$api->logout('username');

$api->login('username', 'password');

$storyFolder = $api->getStoriesOfHighlightsFolderById(12345);

$this->assertCount(33, $storyFolder->getStories(false));

$api->logout('username');
}
}

0 comments on commit 0b5c4e2

Please sign in to comment.