Skip to content

Commit

Permalink
- Fixes #122
Browse files Browse the repository at this point in the history
- Now getMediaByUrl and getMediaByCode are non-static
  • Loading branch information
raiym committed Jun 17, 2017
1 parent edd2940 commit dc03fd6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/InstagramScraper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_once dirname(__FILE__) . '/InstagramScraper/Instagram.php';
require_once dirname(__FILE__) . '/InstagramScraper/Endpoints.php';
require_once dirname(__FILE__) . '/InstagramScraper/InstagramQueryId.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Account.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Comment.php';
require_once dirname(__FILE__) . '/InstagramScraper/Model/Location.php';
Expand Down
24 changes: 22 additions & 2 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ class Endpoints
const ACCOUNT_JSON_INFO_BY_ID = 'ig_user({userId}){id,username,external_url,full_name,profile_pic_url,biography,followed_by{count},follows{count},media{count},is_private,is_verified}';
const COMMENTS_BEFORE_COMMENT_ID_BY_CODE = 'https://www.instagram.com/graphql/query/?query_id=17852405266163336&shortcode={{shortcode}}&first={{count}}&after={{commentId}}';
const LAST_LIKES_BY_CODE = 'ig_shortcode({{code}}){likes{nodes{id,user{id,profile_pic_url,username,follows{count},followed_by{count},biography,full_name,media{count},is_private,external_url,is_verified}},page_info}}';

const FOLLOWING_URL = 'https://www.instagram.com/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}';
const FOLLOWERS_URL = 'https://www.instagram.com/graphql/query/?query_id=17851374694183129&id={{accountId}}&first={{count}}';
const FOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/follow/';
const UNFOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/unfollow/';
const USER_FEED = 'https://www.instagram.com/graphql/query/?query_id=17861995474116400&fetch_media_item_count=12&fetch_media_item_cursor=&fetch_comment_count=4&fetch_like=10';
const USER_FEED2 = 'https://www.instagram.com/?__a=1';
const INSTAGRAM_QUERY_URL = 'https://www.instagram.com/query/';
const INSTAGRAM_GRAPHQL_QUERY_URL = 'https://www.instagram.com/graphql/query/';
const INSTAGRAM_CDN_URL = 'https://scontent.cdninstagram.com/';

const ACCOUNT_MEDIAS2 = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after=';

// Look alike??
const URL_SIMILAR = 'https://www.instagram.com/graphql/query/?query_id=17845312237175864&id=4663052';

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


public static function getAccountPageLink($username)
{
return str_replace('{username}', urlencode($username), Endpoints::ACCOUNT_PAGE);
Expand Down Expand Up @@ -81,6 +93,14 @@ public static function getLastLikesByCodeLink($code)
{
$url = str_replace('{{code}}', urlencode($code), Endpoints::LAST_LIKES_BY_CODE);
return $url;
}

public static function getGraphQlUrl($queryId, $parameters)
{
$url = str_replace('{{queryId}}', urlencode($queryId), Endpoints::GRAPH_QL_QUERY_URL);
foreach ($parameters as $key => $value) {
$url .= "&$key=$value";
}
return $url;
}
}
98 changes: 54 additions & 44 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,6 @@ public static function getMedias($username, $count = 20, $maxId = '')
return $medias;
}

public static function getMediaByCode($mediaCode)
{
return self::getMediaByUrl(Endpoints::getMediaPageLink($mediaCode));
}

public static function getMediaByUrl($mediaUrl)
{
if (filter_var($mediaUrl, FILTER_VALIDATE_URL) === false) {
throw new \InvalidArgumentException('Malformed media url');
}
$response = Request::get(rtrim($mediaUrl, '/') . '/?__a=1');
if ($response->code === 404) {
throw new InstagramNotFoundException('Media with given code does not exist or account is private.');
}
if ($response->code !== 200) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . $response->body . ' Something went wrong. Please report issue.');
}
$mediaArray = json_decode($response->raw_body, true);
if (!isset($mediaArray['graphql']['shortcode_media'])) {
throw new InstagramException('Media with this code does not exist');
}
return Media::fromMediaPage($mediaArray['graphql']['shortcode_media']);
}

public static function searchAccountsByUsername($username)
{
// TODO: Add tests and auth
Expand Down Expand Up @@ -171,12 +147,44 @@ public static function searchTagsByTagName($tag)
return $hashtags;
}

public static function getMediaById($mediaId)
public function getMediaById($mediaId)
{
$mediaLink = Media::getLinkFromId($mediaId);
return self::getMediaByUrl($mediaLink);
}

public function getMediaByUrl($mediaUrl)
{
if (filter_var($mediaUrl, FILTER_VALIDATE_URL) === false) {
throw new \InvalidArgumentException('Malformed media url');
}
$response = Request::get(rtrim($mediaUrl, '/') . '/?__a=1', $this->generateHeaders($this->userSession));
if ($response->code === 404) {
throw new InstagramNotFoundException('Media with given code does not exist or account is private.');
}
if ($response->code !== 200) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . $response->body . ' Something went wrong. Please report issue.');
}
$mediaArray = json_decode($response->raw_body, true);
if (!isset($mediaArray['graphql']['shortcode_media'])) {
throw new InstagramException('Media with this code does not exist');
}
return Media::fromMediaPage($mediaArray['graphql']['shortcode_media']);
}

private function generateHeaders($session)
{
$headers = [];
if ($session) {
$cookies = '';
foreach ($session as $key => $value) {
$cookies .= "$key=$value; ";
}
$headers = ['cookie' => $cookies, 'referer' => Endpoints::BASE_URL . '/', 'x-csrftoken' => $session['csrftoken']];
}
return $headers;
}

public function getPaginateMedias($username, $maxId = '')
{
$hasNextPage = true;
Expand Down Expand Up @@ -220,19 +228,6 @@ public function getPaginateMedias($username, $maxId = '')
return $toReturn;
}

private function generateHeaders($session)
{
$headers = [];
if ($session) {
$cookies = '';
foreach ($session as $key => $value) {
$cookies .= "$key=$value; ";
}
$headers = ['cookie' => $cookies, 'referer' => Endpoints::BASE_URL . '/', 'x-csrftoken' => $session['csrftoken']];
}
return $headers;
}

public function getMediaCommentsById($mediaId, $count = 10, $maxId = null)
{
$code = Media::getCodeFromId($mediaId);
Expand Down Expand Up @@ -307,9 +302,10 @@ public function getAccountById($id)
throw new \InvalidArgumentException('User id must be integer or integer wrapped in string');
}

$parameters = Endpoints::getAccountJsonInfoLinkByAccountId($id);

$response = Request::post(Endpoints::INSTAGRAM_QUERY_URL, $this->generateHeaders($this->userSession), ['q' => $parameters]);
$p['id'] = $id;
$p['first'] = 1;
$url = Endpoints::getGraphQlUrl(InstagramQueryId::USER_MEDIAS, $p);
$response = Request::get($url, $this->generateHeaders($this->userSession));

if ($response->code !== 200) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . $response->body . ' Something went wrong. Please report issue.');
Expand All @@ -322,10 +318,24 @@ public function getAccountById($id)
if ($userArray['status'] === 'fail') {
throw new InstagramException($userArray['message']);
}
if (!isset($userArray['username'])) {
throw new InstagramNotFoundException('User with this id not found');
if (!isset($userArray['data']['user']) || $userArray['data']['user'] == null) {
throw new InstagramNotFoundException("User with this id not found");
}
return Account::fromAccountPage($userArray);
$edges = $userArray['data']['user']['edge_owner_to_timeline_media']['edges'];
if (sizeof($edges) == 0) {
throw new InstagramNotFoundException("User exists but library could not pull information about user");
}
$shortcode = $edges[0]['node']['shortcode'];

$media = $this->getMediaByCode($shortcode);
return $media->owner;
}

// TODO: use new

public function getMediaByCode($mediaCode)
{
return $this->getMediaByUrl(Endpoints::getMediaPageLink($mediaCode));
}

public function getMediasByTag($tag, $count = 12, $maxId = '')
Expand Down
13 changes: 13 additions & 0 deletions src/InstagramScraper/InstagramQueryId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace InstagramScraper;


class InstagramQueryId
{
/**
* id = {{accoundId}}, first = {{count}}, after = {{mediaId}}
*/
const USER_MEDIAS = '17880160963012870';

}
2 changes: 1 addition & 1 deletion tests/InstagramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function setUpBeforeClass()
'path' => $sessionFolder
]);
$instanceCache = CacheManager::getInstance('files');
self::$instagram = Instagram::withCredentials('USERNAME', 'PASSWORD', $instanceCache);
self::$instagram = Instagram::withCredentials('raiym', 'math071312', $instanceCache);
self::$instagram->login();

}
Expand Down

0 comments on commit dc03fd6

Please sign in to comment.