Skip to content

Commit

Permalink
Get threads from direct and usage extends cache interface (#758)
Browse files Browse the repository at this point in the history
* can use extends cache interface (ex. /Illuminate/Contracts/Cache/Repository)

* get threads

* add example getThreads

* fix all examples
  • Loading branch information
Alexander Tarkhanov authored Sep 15, 2020
1 parent 1aeaf43 commit 1d7abb5
Show file tree
Hide file tree
Showing 28 changed files with 751 additions and 29 deletions.
5 changes: 3 additions & 2 deletions examples/addAndDeleteComment.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
use InstagramScraper\Exception\InstagramException;
use Phpfastcache\Helper\Psr16Adapter;

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

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

try {
Expand All @@ -12,7 +13,7 @@
$comment = $instagram->addComment($mediaId, 'Text 1');
// replied to comment
$instagram->addComment($mediaId, 'Text 2', $comment);

$instagram->deleteComment($mediaId, $comment);
} catch (InstagramException $ex) {
echo $ex->getMessage();
Expand Down
6 changes: 4 additions & 2 deletions examples/getAccountFollowers.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

Expand All @@ -10,4 +12,4 @@
$account = $instagram->getAccount($username);
sleep(1);
$followers = $instagram->getFollowers($account->getId(), 1000, 100, true); // Get 1000 followers of 'kevin', 100 a time with random delay between requests
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';
6 changes: 4 additions & 2 deletions examples/getAccountFollowings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

Expand All @@ -10,4 +12,4 @@
$account = $instagram->getAccount($username);
sleep(1);
$followers = $instagram->getFollowing($account->getId(), 1000, 100, true); // Get 1000 followings of 'kevin', 100 a time with random delay between requests
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';
4 changes: 3 additions & 1 deletion examples/getAccountMediasByUsername.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

// If account is public you can query Instagram without auth
Expand Down Expand Up @@ -28,6 +30,6 @@


// If account private you should be subscribed and after auth it will be available
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', 'path/to/cache/folder');
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', new Psr16Adapter('Files'));
$instagram->login();
$medias = $instagram->getMedias('private_account', 100);
4 changes: 3 additions & 1 deletion examples/getCurrentTopMediasByLocationId.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

$medias = $instagram->getCurrentTopMediasByLocationId('1');
Expand Down
4 changes: 3 additions & 1 deletion examples/getCurrentTopMediasByTagName.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

$medias = $instagram->getCurrentTopMediasByTagName('youneverknow');
Expand Down
4 changes: 3 additions & 1 deletion examples/getHighlights.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

$userId = $instagram->getAccount('instagram')->getId();
Expand Down
4 changes: 3 additions & 1 deletion examples/getLocationById.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

// Location id from facebook
Expand Down
4 changes: 3 additions & 1 deletion examples/getMediaByCode.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

$media = $instagram->getMediaByCode('BHaRdodBouH');
Expand Down
4 changes: 3 additions & 1 deletion examples/getMediaById.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

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

$media = $instagram->getMediaById('1270593720437182847');
Expand Down
4 changes: 3 additions & 1 deletion examples/getMediaByUrl.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

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

$media = $instagram->getMediaByUrl('https://www.instagram.com/p/BHaRdodBouH');
Expand Down
4 changes: 3 additions & 1 deletion examples/getMediaComments.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

// Get media comments by shortcode
Expand Down
4 changes: 3 additions & 1 deletion examples/getMediasByLocationId.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

$medias = $instagram->getMediasByLocationId('1', 20);
Expand Down
4 changes: 3 additions & 1 deletion examples/getMediasByTag.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

$medias = $instagram->getMediasByTag('youneverknow', 20);
Expand Down
6 changes: 4 additions & 2 deletions examples/getPaginateMediasByTag.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

$result = $instagram->getPaginateMediasByTag('zara');
Expand All @@ -12,4 +14,4 @@
$medias = array_merge($medias, $result['medias']);
}

echo json_encode($medias);
echo json_encode($medias);
4 changes: 3 additions & 1 deletion examples/getSidecarMediaByUrl.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

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

$media = $instagram->getMediaByUrl('https://www.instagram.com/p/BQ0lhTeAYo5');
Expand Down
6 changes: 4 additions & 2 deletions examples/getStories.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

$stories = $instagram->getStories();
print_r($stories);
print_r($stories);
47 changes: 47 additions & 0 deletions examples/getThreads.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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

$threads = $instagram->getThreads(10, 10, 20);
$thread = $threads[0];

echo "Thread Info:\n";
echo "Id: {$thread->getId()}\n";
echo "Title: {$thread->getTitle()}\n";
echo "Type: {$thread->getType()}\n";
echo "Read State: {$thread->getReadState()}\n\n";

$items = $thread->getItems();
$item = $items[0];

echo "Item Info:\n";
echo "Id: {$item->getId()}\n";
echo "Type: {$item->getType()}\n";
echo "Time: {$item->getTime()}\n";
echo "User ID: {$item->getUserId()}\n";
echo "Text: {$item->getText()}\n\n";

$reelShare = $item->getReelShare();

echo "Reel Share Info:\n";
echo "Text: {$reelShare->getText()}\n";
echo "Type: {$reelShare->getType()}\n";
echo "Owner Id: {$reelShare->getOwnerId()}\n";
echo "Mentioned Id: {$reelShare->getMentionedId()}\n\n";

$reelMedia = $reelShare->getMedia();

echo "Reel Media Info:\n";
echo "Id: {$reelMedia->getId()}\n";
echo "Caption: {$reelMedia->getCaption()}\n";
echo "Code: {$reelMedia->getCode()}\n";
echo "Expiring At: {$reelMedia->getExpiringAt()}\n";
echo "Image: {$reelMedia->getImage()}\n\n";

// $user = $reelMedia->getUser(); // InstagramScraper\Model\Account
// $mentions = $reelMedia->getMentions(); // InstagramScraper\Model\Account[]
4 changes: 2 additions & 2 deletions examples/likeAndUnlikeMedia.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

use Phpfastcache\Helper\Psr16Adapter;
use InstagramScraper\Exception\InstagramException;

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

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

try {
Expand Down
4 changes: 2 additions & 2 deletions examples/paginateAccountMediaByUsername.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;
require __DIR__ . '/../vendor/autoload.php';


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

$result = $instagram->getPaginateMedias('kevin');
Expand Down
4 changes: 3 additions & 1 deletion examples/searchAccountsByUsername.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;

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

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


Expand Down
3 changes: 2 additions & 1 deletion examples/setCustomCookies.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
use Phpfastcache\Helper\Psr16Adapter;
use InstagramScraper\Exception\InstagramException;

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

$instagram = \InstagramScraper\Instagram::withCredentials($this->instaUsername, $this->instaPassword,new Psr16Adapter('Files') );
$instagram = \InstagramScraper\Instagram::withCredentials($this->instaUsername, $this->instaPassword, new Psr16Adapter('Files'));
$instagram->setUserAgent('User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0');
$instagram->setCustomCookies($newCookie);
$instagram->login();
Expand Down
12 changes: 12 additions & 0 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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 THREADS_URL = 'https://www.instagram.com/direct_v2/web/inbox/?persistentBadging=true&folder=&limit={limit}&thread_message_limit={messageLimit}&cursor={cursor}';

// Look alike??
const URL_SIMILAR = 'https://www.instagram.com/graphql/query/?query_id=17845312237175864&id=4663052';
Expand Down Expand Up @@ -216,4 +217,15 @@ public static function getHighlightUrl($id)
{
return str_replace('{userId}', urlencode($id), static::HIGHLIGHT_URL);
}

public static function getThreadsUrl($limit, $messageLimit, $cursor)
{
$url = static::THREADS_URL;

$url = str_replace('{limit}', $limit, $url);
$url = str_replace('{messageLimit}', $messageLimit, $url);
$url = str_replace('{cursor}', $cursor, $url);

return $url;
}
}
Loading

0 comments on commit 1d7abb5

Please sign in to comment.