Skip to content

Commit

Permalink
allow pending requests (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Tarkhanov authored Jan 31, 2021
1 parent cdcb044 commit c3de997
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class Endpoints
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}';
const THREADS_URL = 'https://i.instagram.com/api/v1/direct_v2/inbox/?persistentBadging=true&folder=&limit={limit}&thread_message_limit={messageLimit}&cursor={cursor}';
const THREADS_PENDING_REQUESTS_URL = 'https://i.instagram.com/api/v1/direct_v2/pending_inbox/?limit={limit}&cursor={cursor}';
const THREADS_APPROVE_MULTIPLE_URL = 'https://i.instagram.com/api/v1/direct_v2/threads/approve_multiple/';

// Look alike??
const URL_SIMILAR = 'https://www.instagram.com/graphql/query/?query_id=17845312237175864&id=4663052';
Expand Down Expand Up @@ -236,4 +238,19 @@ public static function getThreadsUrl($limit, $messageLimit, $cursor)

return $url;
}

public static function getThreadsPendingRequestsUrl($limit, $cursor = null)
{
$url = static::THREADS_PENDING_REQUESTS_URL;

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

return $url;
}

public static function getThreadsApproveMultipleUrl()
{
return static::THREADS_APPROVE_MULTIPLE_URL;
}
}
52 changes: 52 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -2248,12 +2248,18 @@ public function getPaginateThreads($limit = 10, $messageLimit = 10, $cursor = nu
*/
public function getThreads($count = 10, $limit = 10, $messageLimit = 10)
{
$this->allowPendingRequests();

$threads = [];
$cursor = null;

while (count($threads) < $count) {
$result = $this->getPaginateThreads($limit, $messageLimit, $cursor);

if (!isset($result['threads'])) {
break;
}

$threads = array_merge($threads, $result['threads']);

if (!$result['hasOlder'] || !$result['oldestCursor']) {
Expand All @@ -2265,4 +2271,50 @@ public function getThreads($count = 10, $limit = 10, $messageLimit = 10)

return $threads;
}

/**
* @param int $limit
*
* @return void
* @throws InstagramException
*/
public function allowPendingRequests($limit = 10)
{
$response = Request::get(
Endpoints::getThreadsPendingRequestsUrl($limit),
array_merge(
['x-ig-app-id' => self::X_IG_APP_ID],
$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.');
}

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

if (!isset($jsonResponse['status']) || $jsonResponse['status'] !== 'ok') {
throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.');
}

if (!isset($jsonResponse['inbox']['threads']) || empty($jsonResponse['inbox']['threads'])) {
return;
}

$threadIds = [];

foreach ($jsonResponse['inbox']['threads'] as $thread) {
$threadIds[] = $thread['thread_id'];
}

Request::post(
Endpoints::getThreadsApproveMultipleUrl(),
array_merge(
['x-ig-app-id' => self::X_IG_APP_ID],
$this->generateHeaders($this->userSession)
),
['thread_ids' => json_encode($threadIds)]
);
}
}

0 comments on commit c3de997

Please sign in to comment.