Skip to content

Commit

Permalink
Parse instagram stories
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Rusakov committed Dec 4, 2017
1 parent e1ae14c commit e77314c
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 2 deletions.
8 changes: 8 additions & 0 deletions examples/getStories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

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

$stories = $instagram->getStories();
print_r($stories);
21 changes: 19 additions & 2 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ class Endpoints

const GRAPH_QL_QUERY_URL = 'https://www.instagram.com/graphql/query/?query_id={{queryId}}';

//stories use GRAPH_QL_QUERY_URL
const USER_STORIES_QUERY_ID = '17890626976041463';
const STORIES_QUERY_ID = '17873473675158481';


public static function getAccountPageLink($username)
{
Expand Down Expand Up @@ -108,8 +112,9 @@ public static function getLastLikesByCode($code, $count, $lastLikeID)
public static function getGraphQlUrl($queryId, $parameters)
{
$url = str_replace('{{queryId}}', urlencode($queryId), static::GRAPH_QL_QUERY_URL);
foreach ($parameters as $key => $value) {
$url .= "&$key=$value";
if (!empty($parameters)) {
$query_string = http_build_query($parameters);
$url .= '&' . $query_string;
}
return $url;
}
Expand Down Expand Up @@ -147,4 +152,16 @@ public static function getFollowingJsonLink($accountId, $count, $after = '')

return $url;
}

public static function getUserStoriesLink()
{
$url = self::getGraphQlUrl(static::USER_STORIES_QUERY_ID, ['variables' => json_encode([])]);
return $url;
}

public static function getStoriesLink($variables)
{
$url = self::getGraphQlUrl(static::STORIES_QUERY_ID, ['variables' => json_encode($variables)]);
return $url;
}
}
45 changes: 45 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use InstagramScraper\Model\Like;
use InstagramScraper\Model\Location;
use InstagramScraper\Model\Media;
use InstagramScraper\Model\Story;
use InstagramScraper\Model\Tag;
use phpFastCache\CacheManager;
use Unirest\Request;
Expand Down Expand Up @@ -899,6 +900,50 @@ public function getFollowing($accountId, $count = 20, $pageSize = 20, $delayed =
return $accounts;
}

public function getStories()
{
$response = Request::get(Endpoints::getUserStoriesLink(),
$this->generateHeaders($this->userSession));

if ($response->code !== 200) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);
if (empty($jsonResponse['data']['user']['feed_reels_tray']['edge_reels_tray_to_reel']['edges'])) {
return [];
}

$variables = ['precomposed_overlay' => false, 'reel_ids' => []];
foreach ($jsonResponse['data']['user']['feed_reels_tray']['edge_reels_tray_to_reel']['edges'] as $edge) {
$variables['reel_ids'][] = $edge['node']['id'];
}

$response = Request::get(Endpoints::getStoriesLink($variables),
$this->generateHeaders($this->userSession));

if ($response->code !== 200) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$jsonResponse = json_decode($response->raw_body, true, 512, JSON_BIGINT_AS_STRING);

if (empty($jsonResponse['data']['reels_media'])) {
return [];
}

$stories = [];
foreach ($jsonResponse['data']['reels_media'] as $user) {
$Story = Story::create();
$Story->setOwner(Account::create($user['user']));
foreach ($user['items'] as $item) {
$Story->addStory(Media::create($item));
}
$stories[] = $Story;
}
return $stories;
}

/**
* @param bool $force
*
Expand Down
10 changes: 10 additions & 0 deletions src/InstagramScraper/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,16 @@ protected function initPropertiesCustom($value, $prop, $arr)
$this->videoStandardResolutionUrl = $arr[$prop]['standard_resolution']['url'];
$this->videoLowBandwidthUrl = $arr[$prop]['low_bandwidth']['url'];
break;
case 'video_resources':
foreach ($value as $video) {
if ($video['profile'] == 'MAIN') {
$this->videoStandardResolutionUrl = $video['src'];
} elseif ($video['profile'] == 'BASELINE') {
$this->videoLowResolutionUrl = $video['src'];
$this->videoLowBandwidthUrl = $video['src'];
}
}
break;
case 'location':
switch ($prop) {
case 'id':
Expand Down
41 changes: 41 additions & 0 deletions src/InstagramScraper/Model/Story.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace InstagramScraper\Model;

/**
* Class Story
* @package InstagramScraper\Model
*/
class Story extends AbstractModel
{
/** @var Account */
protected $owner;

/** @var Media[] */
protected $stories;

public function setOwner($owner)
{
$this->owner = $owner;
}

public function getOwner()
{
return $this->owner;
}

public function addStory($story)
{
$this->stories[] = $story;
}

public function setStories($stories)
{
$this->stories = $stories;
}

public function getStories()
{
return $this->stories;
}
}

0 comments on commit e77314c

Please sign in to comment.