Skip to content

Commit

Permalink
Add login process
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrimaud committed Sep 2, 2019
1 parent 28e124f commit 1a4f0ac
Show file tree
Hide file tree
Showing 16 changed files with 393 additions and 60 deletions.
File renamed without changes.
117 changes: 117 additions & 0 deletions examples/getMediasWithLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

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

$cache = new Instagram\Storage\CacheManager(__DIR__ . '/../cache/');

try {

$api = new Instagram\Api($cache);
$api->login('username', 'password');
$api->setUserName('pgrimaud');

// First page

/** @var \Instagram\Hydrator\Component\Feed $feed */
$feed = $api->getFeed();

echo '============================' . "\n";
echo 'User Informations : ' . "\n";
echo '============================' . "\n\n";

echo 'ID : ' . $feed->getId() . "\n";
echo 'Full Name : ' . $feed->getFullName() . "\n";
echo 'UserName : ' . $feed->getUserName() . "\n";
echo 'Following : ' . $feed->getFollowing() . "\n";
echo 'Followers : ' . $feed->getFollowers() . "\n\n";

echo '============================' . "\n";
echo 'Medias first page : ' . "\n";
echo '============================' . "\n\n";

/** @var \Instagram\Hydrator\Component\Media $media */
foreach ($feed->getMedias() as $media) {
echo 'ID : ' . $media->getId() . "\n";
echo 'Caption : ' . $media->getCaption() . "\n";
echo 'Link : ' . $media->getLink() . "\n";
echo 'Likes : ' . $media->getLikes() . "\n";
echo 'Date : ' . $media->getDate()->format('Y-m-d h:i:s') . "\n";
echo '============================' . "\n";
}

// Second page

$api->setEndCursor($feed->getEndCursor());

sleep(1); // avoir 429 Rate limit from Instagram

$feed = $api->getFeed();

echo "\n\n";
echo '============================' . "\n";
echo 'Medias third page : ' . "\n";
echo '============================' . "\n\n";

/** @var \Instagram\Hydrator\Component\Media $media */
foreach ($feed->getMedias() as $media) {
echo 'ID : ' . $media->getId() . "\n";
echo 'Caption : ' . $media->getCaption() . "\n";
echo 'Link : ' . $media->getLink() . "\n";
echo 'Likes : ' . $media->getLikes() . "\n";
echo 'Date : ' . $media->getDate()->format('Y-m-d h:i:s') . "\n";
echo '============================' . "\n";
}

// Third page

$api->setEndCursor($feed->getEndCursor());

sleep(1); // avoir 429 Rate limit from Instagram

$feed = $api->getFeed();

echo "\n\n";
echo '============================' . "\n";
echo 'Medias fourth page : ' . "\n";
echo '============================' . "\n\n";

/** @var \Instagram\Hydrator\Component\Media $media */
foreach ($feed->getMedias() as $media) {
echo 'ID : ' . $media->getId() . "\n";
echo 'Caption : ' . $media->getCaption() . "\n";
echo 'Link : ' . $media->getLink() . "\n";
echo 'Likes : ' . $media->getLikes() . "\n";
echo 'Date : ' . $media->getDate()->format('Y-m-d h:i:s') . "\n";
echo '============================' . "\n";
}

// Fourth Page

$api->setEndCursor($feed->getEndCursor());

sleep(1); // avoir 429 Rate limit from Instagram

$feed = $api->getFeed();

echo "\n\n";
echo '============================' . "\n";
echo 'Medias second page : ' . "\n";
echo '============================' . "\n\n";

/** @var \Instagram\Hydrator\Component\Media $media */
foreach ($feed->getMedias() as $media) {
echo 'ID : ' . $media->getId() . "\n";
echo 'Caption : ' . $media->getCaption() . "\n";
echo 'Link : ' . $media->getLink() . "\n";
echo 'Likes : ' . $media->getLikes() . "\n";
echo 'Date : ' . $media->getDate()->format('Y-m-d h:i:s') . "\n";
echo '============================' . "\n";
}

// And etc...

} catch (Exception $exception) {
print_r($exception->getMessage());
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
print_r($exception->getMessage());
}
14 changes: 0 additions & 14 deletions examples/profile.php → examples/getProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,6 @@
echo 'Private Account : ' . ($feed->isPrivate() ? 'Yes' : 'No') . "\n";
echo 'Media Count : ' . $feed->getMediaCount() . "\n\n";

echo '============================' . "\n";
echo 'Medias first page : ' . "\n";
echo '============================' . "\n\n";

/** @var \Instagram\Hydrator\Component\Media $media */
foreach ($feed->getMedias() as $media) {
echo 'ID : ' . $media->getId() . "\n";
echo 'Caption : ' . $media->getCaption() . "\n";
echo 'Link : ' . $media->getLink() . "\n";
echo 'Likes : ' . $media->getLikes() . "\n";
echo 'Date : ' . $media->getDate()->format('Y-m-d h:i:s') . "\n";
echo '============================' . "\n";
}

} catch (Exception $exception) {
print_r($exception->getMessage());
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
Expand Down
36 changes: 36 additions & 0 deletions examples/getProfileWithLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

try {
$cache = new Instagram\Storage\CacheManager(__DIR__ . '/../cache/');

$api = new Instagram\Api($cache);
$api->login('username', 'password');
$api->setUserName('pgrimaud');

// only the first page
/** @var \Instagram\Hydrator\Component\Feed $feed */
$feed = $api->getFeed();

echo '============================' . "\n";
echo 'User Informations : ' . "\n";
echo '============================' . "\n\n";

echo 'ID : ' . $feed->getId() . "\n";
echo 'Full Name : ' . $feed->getFullName() . "\n";
echo 'UserName : ' . $feed->getUserName() . "\n";
echo 'Following : ' . $feed->getFollowing() . "\n";
echo 'Followers : ' . $feed->getFollowers() . "\n";
echo 'Biography : ' . $feed->getBiography() . "\n";
echo 'External Url : ' . $feed->getExternalUrl() . "\n";
echo 'Profile Picture : ' . $feed->getProfilePicture() . "\n";
echo 'Verified Account : ' . ($feed->isVerified() ? 'Yes' : 'No') . "\n";
echo 'Private Account : ' . ($feed->isPrivate() ? 'Yes' : 'No') . "\n";
echo 'Media Count : ' . $feed->getMediaCount() . "\n\n";

} catch (Exception $exception) {
print_r($exception->getMessage());
} catch (\GuzzleHttp\Exception\GuzzleException $e) {
print_r($exception->getMessage());
}
35 changes: 32 additions & 3 deletions src/Instagram/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Instagram;

use GuzzleHttp\Client;
use Instagram\Exception\CacheException;
use GuzzleHttp\Cookie\CookieJar;
use Instagram\Auth\Login;
use Instagram\Exception\InstagramCacheException;
use Instagram\Exception\InstagramException;
use Instagram\Hydrator\HtmlHydrator;
use Instagram\Hydrator\JsonHydrator;
Expand Down Expand Up @@ -35,6 +37,7 @@ class Api

/**
* Api constructor.
*
* @param Client|null $client
* @param CacheManager|null $cacheManager
*/
Expand All @@ -46,9 +49,10 @@ public function __construct(CacheManager $cacheManager = null, Client $client =

/**
* @param integer $limit
*
* @return Hydrator\Component\Feed
*
* @throws CacheException
* @throws InstagramCacheException
* @throws InstagramException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
Expand All @@ -61,7 +65,7 @@ public function getFeed($limit = 12)

if ($this->endCursor) {
if (!$this->cacheManager instanceof CacheManager) {
throw new CacheException('CacheManager object must be specified to use pagination');
throw new InstagramCacheException('CacheManager object must be specified to use pagination');
}

$feed = new JsonTransportFeed($this->client, $this->endCursor, $this->cacheManager);
Expand Down Expand Up @@ -93,4 +97,29 @@ public function setEndCursor($endCursor)
{
$this->endCursor = $endCursor;
}

/**
* @param $username
* @param $password
* @param Client|null $client
*
* @throws Exception\InstagramAuthException
* @throws InstagramCacheException
* @throws InstagramException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function login($username, $password, Client $client = null)
{
if (!$this->cacheManager instanceof CacheManager) {
throw new InstagramCacheException('CacheManager is required with login');
}

$login = new Login($client);
$cookies = $login->execute($username, $password);

if ($cookies instanceof CookieJar) {
$this->cacheManager->sessionName = $username;
$this->cacheManager->setSession($username, $cookies);
}
}
}
81 changes: 81 additions & 0 deletions src/Instagram/Auth/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Instagram\Auth;

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use Instagram\Exception\InstagramAuthException;
use Instagram\Exception\InstagramException;
use Instagram\Transport\TransportFeed;

class Login
{
/**
* @var Client
*/
private $client;

/**
* @param Client|null $client
*/
public function __construct(Client $client = null)
{
$this->client = $client ?: new Client();
}

/**
* @param $username
* @param $password
*
* @return CookieJar
*
* @throws InstagramAuthException
* @throws InstagramException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function execute($username, $password)
{
$query = $this->client->request('GET', TransportFeed::BASE_URL, [
'headers' => [
'user-agent' => TransportFeed::USER_AGENT
]
]);

$html = (string)$query->getBody();

preg_match('/<script type="text\/javascript">window\._sharedData\s?=(.+);<\/script>/', $html, $matches);

if (!isset($matches[1])) {
throw new InstagramException('Unable to extract JSON data');
}

$data = json_decode($matches[1]);

$cookieJar = new CookieJar();

$query = $this->client->request('POST', TransportFeed::AUTH_URL, [
'form_params' => [
'username' => $username,
'password' => $password,
],
'headers' => [
'cookie' => 'ig_cb=1; csrftoken=' . $data->config->csrf_token,
'referer' => TransportFeed::BASE_URL,
'x-csrftoken' => $data->config->csrf_token,
'user-agent' => TransportFeed::USER_AGENT,
],
'cookies' => $cookieJar
]);

if ($query->getStatusCode() !== 200) {
throw new InstagramAuthException('Unknown error');
} else {
$response = json_decode((string)$query->getBody());
if ($response->authenticated == true) {
return $cookieJar;
} else {
throw new InstagramAuthException('Wrong login / password');
}
}
}
}
7 changes: 0 additions & 7 deletions src/Instagram/Exception/CacheException.php

This file was deleted.

7 changes: 7 additions & 0 deletions src/Instagram/Exception/InstagramAuthException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Instagram\Exception;

class InstagramAuthException extends \Exception
{
}
7 changes: 7 additions & 0 deletions src/Instagram/Exception/InstagramCacheException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Instagram\Exception;

class InstagramCacheException extends \Exception
{
}
2 changes: 1 addition & 1 deletion src/Instagram/Hydrator/HtmlHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getHydratedData()
$media->setComments($node->edge_media_to_comment->count);
$media->setLikes($node->edge_liked_by->count);

$media->setLink(TransportFeed::INSTAGRAM_ENDPOINT . "p/{$node->shortcode}/");
$media->setLink(TransportFeed::BASE_URL . "p/{$node->shortcode}/");

$media->setThumbnails($node->thumbnail_resources);

Expand Down
2 changes: 1 addition & 1 deletion src/Instagram/Hydrator/JsonHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function getHydratedData()
$media->setComments($node->edge_media_to_comment->count);
$media->setLikes($node->edge_media_preview_like->count);

$media->setLink(TransportFeed::INSTAGRAM_ENDPOINT . "p/{$node->shortcode}/");
$media->setLink(TransportFeed::BASE_URL . "p/{$node->shortcode}/");

$media->setThumbnails($node->thumbnail_resources);

Expand Down
Loading

0 comments on commit 1a4f0ac

Please sign in to comment.