Skip to content

Commit

Permalink
implemented media::hasLiked() and added function to example likeAndUn…
Browse files Browse the repository at this point in the history
…likeMdeia.php (#835)
  • Loading branch information
ricdijk authored Dec 28, 2020
1 parent 7c9e479 commit 1e7545e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
36 changes: 33 additions & 3 deletions examples/likeAndUnlikeMedia.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
<?php
ini_set('display_errors', 1);ini_set('display_startup_errors', 1);error_reporting(E_ALL);
use Phpfastcache\Helper\Psr16Adapter;
use InstagramScraper\Exception\InstagramException;

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

$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'user', 'passwd', new Psr16Adapter('Files'));
$instagram->login();

$mediaId='2474033634010898853';

function loghasLiked($bool)
{
echo "HasLiked: ";
var_export($bool);
if ($bool === null) echo " -> Unknown\n<br>"; //e.g. getMedia by tag won't give us haslike (-> an additional call to getMediaByCode/Id will give the result)
elseif ($bool) echo " -> Liked\n<br>";
else echo " -> Not Liked\n<br>";
}

try {
$instagram->like('1663256735663694497');
$instagram->unlike('1663256735663694497');
loghasLiked($instagram->getMediaById($mediaId)->getHasLiked());
//hasLiked: False -> Not Liked

$instagram->like($mediaId);
loghasLiked($instagram->getMediaById($mediaId)->getHasLiked());
//hasLiked: True -> Liked

$instagram->unlike($mediaId);
loghasLiked($instagram->getMediaById($mediaId)->getHasLiked());
//hasLiked: False -> Not Liked

echo "\n<br>";
$media = $instagram->getCurrentTopMediasByTagName('Photography')[0];
loghasLiked($media->getHasLiked());
//hasLiked: NULL -> Unknown

$media = $instagram->getMediaById($media->getId());
loghasLiked($media->getHasLiked());
//hasLiked: False -> Not Liked

} catch (InstagramException $ex) {
echo $ex->getMessage();
}
18 changes: 17 additions & 1 deletion src/InstagramScraper/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ class Media extends AbstractModel
*/
protected $likesCount = 0;

/**
* @var boolean
*/
protected $hasLiked = null;

/**
* @var
*/
Expand Down Expand Up @@ -401,6 +406,14 @@ public function getLikesCount()
return $this->likesCount;
}

/**
* @return boolean
*/
public function getHasLiked()
{
return $this->hasLiked;
}

/**
* @return mixed
*/
Expand Down Expand Up @@ -672,6 +685,9 @@ protected function initPropertiesCustom($value, $prop, $arr)
case 'edge_liked_by':
$this->likesCount = $arr[$prop]['count'];
break;
case 'viewer_has_liked':
$this->hasLiked = $arr[$prop];
break;
case 'edge_media_to_caption':
if (is_array($arr[$prop]['edges']) && !empty($arr[$prop]['edges'])) {
$first_caption = $arr[$prop]['edges'][0];
Expand Down Expand Up @@ -774,4 +790,4 @@ public function getOwner()
{
return $this->owner;
}
}
}

0 comments on commit 1e7545e

Please sign in to comment.