Skip to content

Commit

Permalink
Merge pull request #216 from eversionsystems/master
Browse files Browse the repository at this point in the history
Fix #20 Get Media Likes
  • Loading branch information
raiym authored Nov 19, 2017
2 parents 980b1ce + 5c813db commit b84a101
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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 LIKES_BY_SHORTCODE = 'https://www.instagram.com/graphql/query/?query_id=17864450716183058&variables={"shortcode":"{{shortcode}}","first":{{count}},"after":"{{likeId}}"}';
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}}&after={{after}}';
const FOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/follow/';
Expand Down Expand Up @@ -94,6 +95,14 @@ public static function getLastLikesByCodeLink($code)
$url = str_replace('{{code}}', urlencode($code), static::LAST_LIKES_BY_CODE);
return $url;
}

public static function getLastLikesByCode($code, $count, $lastLikeID) {
$url = str_replace('{{shortcode}}', urlencode($code), static::LIKES_BY_SHORTCODE);
$url = str_replace('{{count}}', urlencode($count), $url);
$url = str_replace('{{likeId}}', urlencode($lastLikeID), $url);

return $url;
}

public static function getGraphQlUrl($queryId, $parameters)
{
Expand Down
59 changes: 59 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use InstagramScraper\Exception\InstagramNotFoundException;
use InstagramScraper\Model\Account;
use InstagramScraper\Model\Comment;
use InstagramScraper\Model\Like;
use InstagramScraper\Model\Location;
use InstagramScraper\Model\Media;
use InstagramScraper\Model\Tag;
Expand All @@ -18,6 +19,7 @@ class Instagram
const HTTP_NOT_FOUND = 404;
const HTTP_OK = 200;
const MAX_COMMENTS_PER_REQUEST = 300;
const MAX_LIKES_PER_REQUEST = 300;

private static $instanceCache;
private $sessionUsername;
Expand Down Expand Up @@ -387,6 +389,63 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null)
}
return $comments;
}

/**
* @param $code
* @param int $count
* @param null $maxId
*
* @return array
* @throws InstagramException
*/
public function getMediaLikesByCode($code, $count = 10, $maxId = null)
{
$remain = $count;
$likes = [];
$index = 0;
$hasPrevious = true;
while ($hasPrevious && $index < $count) {
if ($remain > self::MAX_LIKES_PER_REQUEST) {
$numberOfLikesToRetreive = self::MAX_LIKES_PER_REQUEST;
$remain -= self::MAX_LIKES_PER_REQUEST;
$index += self::MAX_LIKES_PER_REQUEST;
} else {
$numberOfLikesToRetreive = $remain;
$index += $remain;
$remain = 0;
}
if (!isset($maxId)) {
$maxId = '';

}
$commentsUrl = Endpoints::getLastLikesByCode($code, $numberOfLikesToRetreive, $maxId);
$response = Request::get($commentsUrl, $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.');
}
$cookies = self::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];
$jsonResponse = json_decode($response->raw_body, true);

$nodes = $jsonResponse['data']['shortcode_media']['edge_liked_by']['edges'];

foreach ($nodes as $likesArray) {
$likes[] = Like::create($likesArray['node']);
}

$hasPrevious = $jsonResponse['data']['shortcode_media']['edge_liked_by']['page_info']['has_next_page'];
$numberOfLikes = $jsonResponse['data']['shortcode_media']['edge_liked_by']['count'];
if ($count > $numberOfLikes) {
$count = $numberOfLikes;
}
if (sizeof($nodes) == 0) {
return $likes;
}
$maxId = $jsonResponse['data']['shortcode_media']['edge_liked_by']['page_info']['end_cursor'];
}

return $likes;
}

/**
* @param string $rawCookies
Expand Down
50 changes: 50 additions & 0 deletions src/InstagramScraper/Model/Like.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace InstagramScraper\Model;


class Like extends AbstractModel
{
/**
* @var
*/
protected $id;

/**
* @var Account
*/
protected $username;

/**
* @return mixed
*/
public function getId()
{
return $this->id;
}

/**
* @return mixed
*/
public function getUserName()
{
return $this->username;
}

/**
* @param $value
* @param $prop
*/
protected function initPropertiesCustom($value, $prop)
{
switch ($prop) {
case 'id':
$this->id = $value;
break;
case 'username':
$this->username = $value;
break;
}
}

}

0 comments on commit b84a101

Please sign in to comment.