Skip to content

Commit

Permalink
New feature and fix (#1024)
Browse files Browse the repository at this point in the history
* Add method for get info about current authenticated user

* Change getMediaUrl and Media class to new Instagram structure
  • Loading branch information
fonclub authored Jan 24, 2022
1 parent a2a508b commit 5d8a2af
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 24 deletions.
30 changes: 28 additions & 2 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,10 +819,10 @@ public function getMediaByUrl($mediaUrl)
}

$mediaArray = $this->decodeRawBodyToJson($response->raw_body);
if (!isset($mediaArray['graphql']['shortcode_media'])) {
if (!isset($mediaArray['items'])) {
throw new InstagramException('Media with this code does not exist');
}
return Media::create($mediaArray['graphql']['shortcode_media']);
return Media::create(current($mediaArray['items']));
}

/**
Expand Down Expand Up @@ -2627,4 +2627,30 @@ public function allowPendingRequests($limit = 10)
['thread_ids' => json_encode($threadIds)]
);
}

/**
* @return Account
* @throws InstagramException
* @throws InstagramNotFoundException
*/
public function getCurrentUserInfo(): Account
{
$response = Request::get(Endpoints::USER_FEED_hash, $this->generateHeaders($this->userSession));

if ($response->code === static::HTTP_NOT_FOUND) {
throw new InstagramNotFoundException('Account with given username does not exist.');
}
if ($response->code !== static::HTTP_OK) {
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

$this->parseCookies($response->headers);
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);

if(!isset($jsonResponse['data']['user']['username'])){
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
}

return $this->getAccount($jsonResponse['data']['user']['username']);
}
}
152 changes: 130 additions & 22 deletions src/InstagramScraper/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Media extends AbstractModel
const TYPE_VIDEO = 'video';
const TYPE_SIDECAR = 'sidecar';
const TYPE_CAROUSEL = 'carousel';

const MEDIA_TYPE_IMAGE = 1;
const MEDIA_TYPE_VIDEO = 2;
const MEDIA_TYPE_CAROUSEL = 8;

/**
* @var string
Expand Down Expand Up @@ -197,6 +201,8 @@ class Media extends AbstractModel
* @var array
*/
protected $taggedUsersIds=[];

private $media_type;

/**
* @param string $code
Expand Down Expand Up @@ -587,7 +593,6 @@ protected function initPropertiesCustom($value, $prop, $arr)
$this->commentsDisabled = $value;
break;
case 'comments':
$this->commentsCount = $arr[$prop]['count'];
break;
case 'edge_liked_by':
case 'edge_media_preview_like':
Expand Down Expand Up @@ -637,7 +642,7 @@ protected function initPropertiesCustom($value, $prop, $arr)
}
break;
case 'caption':
$this->caption = $arr[$prop];
$this->caption = $value['text'] ?? '';
break;
case 'accessibility_caption':
$this->altText = $value;
Expand Down Expand Up @@ -666,10 +671,10 @@ protected function initPropertiesCustom($value, $prop, $arr)
break;
case 'location':
if (isset($arr[$prop])) {
$this->locationId = $arr[$prop]['id'] ? $arr[$prop]['id'] : null;
$this->locationName = $arr[$prop]['name'] ? $arr[$prop]['name'] : null;
$this->locationSlug = $arr[$prop]['slug'] ? $arr[$prop]['slug'] : null;
$this->locationAddressJson = isset($arr[$prop]['address_json']) ? $arr[$prop]['address_json'] : null;
$this->locationId = $arr[$prop]['id'] ?? null;
$this->locationName = $arr[$prop]['name'] ?? null;
$this->locationSlug = $arr[$prop]['slug'] ?? null;
$this->locationAddressJson = $arr[$prop]['address_json'] ?? null;
}
break;
case 'owner':
Expand All @@ -684,6 +689,7 @@ protected function initPropertiesCustom($value, $prop, $arr)
case 'video_url':
$this->videoStandardResolutionUrl = $value;
break;
case 'view_count':
case 'video_view_count':
$this->videoViews = $value;
break;
Expand Down Expand Up @@ -777,6 +783,57 @@ protected function initPropertiesCustom($value, $prop, $arr)
}
}
break;
case 'comment_count':
$this->commentsCount = $value;
break;
case 'media_type':
$this->media_type = $value;
switch ($value){
case static::MEDIA_TYPE_IMAGE:
$this->type = static::TYPE_IMAGE;
break;
case static::MEDIA_TYPE_VIDEO:
$this->type = static::TYPE_VIDEO;
break;
case static::MEDIA_TYPE_CAROUSEL:
$this->type = static::TYPE_CAROUSEL;
break;
}
break;
case 'image_versions2':
foreach ($value['candidates'] as $media) {
$mediasUrl[] = $media['url'];
switch ($media['width']) {
case 150:
$this->imageThumbnailUrl = $media['url'];
break;
case 320:
$this->imageLowResolutionUrl = $media['url'];
break;
case 750:
$this->imageStandardResolutionUrl = $media['url'];
break;
case 1080:
$this->imageHighResolutionUrl = $media['url'];
break;
}
}
break;
case 'video_versions':
foreach ($value as $media) {
switch ($media['type']) {
case 101:
$this->videoStandardResolutionUrl = $media['url'];
break;
case 102:
$this->videoLowResolutionUrl = $media['url'];
break;
case 103:
$this->videoLowBandwidthUrl = $media['url'];
break;
}
}
break;
}
if (!$this->ownerId && !is_null($this->owner)) {
$this->ownerId = $this->getOwner()->getId();
Expand All @@ -793,25 +850,76 @@ protected function initPropertiesCustom($value, $prop, $arr)
private static function setCarouselMedia($mediaArray, $carouselArray, $instance)
{
$carouselMedia = new CarouselMedia();
$carouselMedia->setType($carouselArray['type']);

if (isset($carouselArray['images'])) {
$carouselImages = self::getImageUrls($carouselArray['images']['standard_resolution']['url']);
$carouselMedia->setImageLowResolutionUrl($carouselImages['low']);
$carouselMedia->setImageThumbnailUrl($carouselImages['thumbnail']);
$carouselMedia->setImageStandardResolutionUrl($carouselImages['standard']);
$carouselMedia->setImageHighResolutionUrl($carouselImages['high']);
}
if(isset($carouselArray['type'])) {
$carouselMedia->setType($carouselArray['type']);

if (isset($carouselArray['images'])) {
$carouselImages = self::getImageUrls($carouselArray['images']['standard_resolution']['url']);
$carouselMedia->setImageLowResolutionUrl($carouselImages['low']);
$carouselMedia->setImageThumbnailUrl($carouselImages['thumbnail']);
$carouselMedia->setImageStandardResolutionUrl($carouselImages['standard']);
$carouselMedia->setImageHighResolutionUrl($carouselImages['high']);
}

if ($carouselMedia->getType() === self::TYPE_VIDEO) {
if (isset($mediaArray['video_views'])) {
$carouselMedia->setVideoViews($carouselArray['video_views']);
if ($carouselMedia->getType() === self::TYPE_VIDEO) {
if (isset($mediaArray['video_views'])) {
$carouselMedia->setVideoViews($carouselArray['video_views']);
}
if (isset($carouselArray['videos'])) {
$carouselMedia->setVideoLowResolutionUrl($carouselArray['videos']['low_resolution']['url']);
$carouselMedia->setVideoStandardResolutionUrl($carouselArray['videos']['standard_resolution']['url']);
$carouselMedia->setVideoLowBandwidthUrl($carouselArray['videos']['low_bandwidth']['url']);
}
}
} elseif(isset($carouselArray['media_type'])) {
switch ($carouselArray['media_type']){
case static::MEDIA_TYPE_IMAGE:
$carouselMedia->setType(static::TYPE_IMAGE);
break;
case static::MEDIA_TYPE_VIDEO:
$carouselMedia->setType(static::TYPE_VIDEO);
break;
}
if (isset($carouselArray['videos'])) {
$carouselMedia->setVideoLowResolutionUrl($carouselArray['videos']['low_resolution']['url']);
$carouselMedia->setVideoStandardResolutionUrl($carouselArray['videos']['standard_resolution']['url']);
$carouselMedia->setVideoLowBandwidthUrl($carouselArray['videos']['low_bandwidth']['url']);

if($carouselArray['media_type'] == static::MEDIA_TYPE_VIDEO && isset($carouselArray['video_versions'])){
if (isset($mediaArray['view_count'])) {
$carouselMedia->setVideoViews($carouselArray['view_count']);
}

foreach ($carouselArray['video_versions'] as $media) {
switch ($media['type']) {
case 101:
$carouselMedia->setVideoStandardResolutionUrl($media['url']);
break;
case 102:
$carouselMedia->setVideoLowResolutionUrl($media['url']);
break;
case 103:
$carouselMedia->setVideoLowBandwidthUrl($media['url']);
break;
}
}
}

if($carouselArray['media_type'] == static::MEDIA_TYPE_IMAGE && isset($carouselArray['image_versions2'])){
foreach ($carouselArray['image_versions2']['candidates'] as $media) {
$mediasUrl[] = $media['url'];
switch ($media['width']) {
case 150:
$carouselMedia->setImageThumbnailUrl($media['url']);
break;
case 320:
$carouselMedia->setImageLowResolutionUrl($media['url']);
break;
case 750:
$carouselMedia->setImageStandardResolutionUrl($media['url']);
break;
case 1080:
$carouselMedia->setImageHighResolutionUrl($media['url']);
break;
}
}
}
}
array_push($instance->carouselMedia, $carouselMedia);
return $mediaArray;
Expand Down

0 comments on commit 5d8a2af

Please sign in to comment.