Skip to content

Commit 1d7abb5

Browse files
author
Alexander Tarkhanov
authored
Get threads from direct and usage extends cache interface (#758)
* can use extends cache interface (ex. /Illuminate/Contracts/Cache/Repository) * get threads * add example getThreads * fix all examples
1 parent 1aeaf43 commit 1d7abb5

28 files changed

+751
-29
lines changed

examples/addAndDeleteComment.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22
use InstagramScraper\Exception\InstagramException;
3+
use Phpfastcache\Helper\Psr16Adapter;
34

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

6-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
7+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
78
$instagram->login();
89

910
try {
@@ -12,7 +13,7 @@
1213
$comment = $instagram->addComment($mediaId, 'Text 1');
1314
// replied to comment
1415
$instagram->addComment($mediaId, 'Text 2', $comment);
15-
16+
1617
$instagram->deleteComment($mediaId, $comment);
1718
} catch (InstagramException $ex) {
1819
echo $ex->getMessage();

examples/getAccountFollowers.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', 'path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68
sleep(2); // Delay to mimic user
79

@@ -10,4 +12,4 @@
1012
$account = $instagram->getAccount($username);
1113
sleep(1);
1214
$followers = $instagram->getFollowers($account->getId(), 1000, 100, true); // Get 1000 followers of 'kevin', 100 a time with random delay between requests
13-
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';
15+
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';

examples/getAccountFollowings.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', 'path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68
sleep(2); // Delay to mimic user
79

@@ -10,4 +12,4 @@
1012
$account = $instagram->getAccount($username);
1113
sleep(1);
1214
$followers = $instagram->getFollowing($account->getId(), 1000, 100, true); // Get 1000 followings of 'kevin', 100 a time with random delay between requests
13-
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';
15+
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';

examples/getAccountMediasByUsername.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

46
// If account is public you can query Instagram without auth
@@ -28,6 +30,6 @@
2830

2931

3032
// If account private you should be subscribed and after auth it will be available
31-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', 'path/to/cache/folder');
33+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
3234
$instagram->login();
3335
$medias = $instagram->getMedias('private_account', 100);

examples/getCurrentTopMediasByLocationId.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79
$medias = $instagram->getCurrentTopMediasByLocationId('1');

examples/getCurrentTopMediasByTagName.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79
$medias = $instagram->getCurrentTopMediasByTagName('youneverknow');

examples/getHighlights.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79
$userId = $instagram->getAccount('instagram')->getId();

examples/getLocationById.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79
// Location id from facebook

examples/getMediaByCode.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
// If account is public you can query Instagram without auth
35
$instagram = new \InstagramScraper\Instagram();
46

57
// If account is private and you subscribed to it, first login
6-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
8+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
79
$instagram->login();
810

911
$media = $instagram->getMediaByCode('BHaRdodBouH');

examples/getMediaById.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

46
// If account is public you can query Instagram without auth
57
$instagram = new \InstagramScraper\Instagram();
68

79
// If account is private and you subscribed to it, first login
8-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
10+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
911
$instagram->login();
1012

1113
$media = $instagram->getMediaById('1270593720437182847');

examples/getMediaByUrl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

46
// If account is public you can query Instagram without auth
57
$instagram = new \InstagramScraper\Instagram();
68

79
// If account is private and you subscribed to it, first login
8-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
10+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
911
$instagram->login();
1012

1113
$media = $instagram->getMediaByUrl('https://www.instagram.com/p/BHaRdodBouH');

examples/getMediaComments.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', 'path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79
// Get media comments by shortcode

examples/getMediasByLocationId.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79
$medias = $instagram->getMediasByLocationId('1', 20);

examples/getMediasByTag.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79
$medias = $instagram->getMediasByTag('youneverknow', 20);

examples/getPaginateMediasByTag.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79
$result = $instagram->getPaginateMediasByTag('zara');
@@ -12,4 +14,4 @@
1214
$medias = array_merge($medias, $result['medias']);
1315
}
1416

15-
echo json_encode($medias);
17+
echo json_encode($medias);

examples/getSidecarMediaByUrl.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

46
function printMediaInfo(\InstagramScraper\Model\Media $media, $padding = '') {
@@ -17,7 +19,7 @@ function printMediaInfo(\InstagramScraper\Model\Media $media, $padding = '') {
1719
$instagram = new \InstagramScraper\Instagram();
1820

1921
// If account is private and you subscribed to it firstly login
20-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
22+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
2123
$instagram->login();
2224

2325
$media = $instagram->getMediaByUrl('https://www.instagram.com/p/BQ0lhTeAYo5');

examples/getStories.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79
$stories = $instagram->getStories();
8-
print_r($stories);
10+
print_r($stories);

examples/getThreads.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
4+
require __DIR__ . '/../vendor/autoload.php';
5+
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
7+
$instagram->login();
8+
$instagram->saveSession();
9+
10+
$threads = $instagram->getThreads(10, 10, 20);
11+
$thread = $threads[0];
12+
13+
echo "Thread Info:\n";
14+
echo "Id: {$thread->getId()}\n";
15+
echo "Title: {$thread->getTitle()}\n";
16+
echo "Type: {$thread->getType()}\n";
17+
echo "Read State: {$thread->getReadState()}\n\n";
18+
19+
$items = $thread->getItems();
20+
$item = $items[0];
21+
22+
echo "Item Info:\n";
23+
echo "Id: {$item->getId()}\n";
24+
echo "Type: {$item->getType()}\n";
25+
echo "Time: {$item->getTime()}\n";
26+
echo "User ID: {$item->getUserId()}\n";
27+
echo "Text: {$item->getText()}\n\n";
28+
29+
$reelShare = $item->getReelShare();
30+
31+
echo "Reel Share Info:\n";
32+
echo "Text: {$reelShare->getText()}\n";
33+
echo "Type: {$reelShare->getType()}\n";
34+
echo "Owner Id: {$reelShare->getOwnerId()}\n";
35+
echo "Mentioned Id: {$reelShare->getMentionedId()}\n\n";
36+
37+
$reelMedia = $reelShare->getMedia();
38+
39+
echo "Reel Media Info:\n";
40+
echo "Id: {$reelMedia->getId()}\n";
41+
echo "Caption: {$reelMedia->getCaption()}\n";
42+
echo "Code: {$reelMedia->getCode()}\n";
43+
echo "Expiring At: {$reelMedia->getExpiringAt()}\n";
44+
echo "Image: {$reelMedia->getImage()}\n\n";
45+
46+
// $user = $reelMedia->getUser(); // InstagramScraper\Model\Account
47+
// $mentions = $reelMedia->getMentions(); // InstagramScraper\Model\Account[]

examples/likeAndUnlikeMedia.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
2-
2+
use Phpfastcache\Helper\Psr16Adapter;
33
use InstagramScraper\Exception\InstagramException;
44

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

7-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
7+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
88
$instagram->login();
99

1010
try {

examples/paginateAccountMediaByUsername.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
23
require __DIR__ . '/../vendor/autoload.php';
34

4-
55
// getPaginateMedias() works with and without authentication
6-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
77
$instagram->login();
88

99
$result = $instagram->getPaginateMedias('kevin');

examples/searchAccountsByUsername.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
24
require __DIR__ . '/../vendor/autoload.php';
35

4-
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', 'path/to/cache/folder/');
6+
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
57
$instagram->login();
68

79

examples/setCustomCookies.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
23
use InstagramScraper\Exception\InstagramException;
34

45
require __DIR__ . '/../vendor/autoload.php';
@@ -14,7 +15,7 @@
1415
"ds_user_id" => "36*****872",
1516
];
1617

17-
$instagram = \InstagramScraper\Instagram::withCredentials($this->instaUsername, $this->instaPassword,new Psr16Adapter('Files') );
18+
$instagram = \InstagramScraper\Instagram::withCredentials($this->instaUsername, $this->instaPassword, new Psr16Adapter('Files'));
1819
$instagram->setUserAgent('User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0');
1920
$instagram->setCustomCookies($newCookie);
2021
$instagram->login();

src/InstagramScraper/Endpoints.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Endpoints
3535
const DELETE_COMMENT_URL = 'https://www.instagram.com/web/comments/{mediaId}/delete/{commentId}/';
3636
const ACCOUNT_MEDIAS2 = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after=';
3737
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}';
38+
const THREADS_URL = 'https://www.instagram.com/direct_v2/web/inbox/?persistentBadging=true&folder=&limit={limit}&thread_message_limit={messageLimit}&cursor={cursor}';
3839

3940
// Look alike??
4041
const URL_SIMILAR = 'https://www.instagram.com/graphql/query/?query_id=17845312237175864&id=4663052';
@@ -216,4 +217,15 @@ public static function getHighlightUrl($id)
216217
{
217218
return str_replace('{userId}', urlencode($id), static::HIGHLIGHT_URL);
218219
}
220+
221+
public static function getThreadsUrl($limit, $messageLimit, $cursor)
222+
{
223+
$url = static::THREADS_URL;
224+
225+
$url = str_replace('{limit}', $limit, $url);
226+
$url = str_replace('{messageLimit}', $messageLimit, $url);
227+
$url = str_replace('{cursor}', $cursor, $url);
228+
229+
return $url;
230+
}
219231
}

0 commit comments

Comments
 (0)