Skip to content

Commit 5d8a2af

Browse files
authored
New feature and fix (#1024)
* Add method for get info about current authenticated user * Change getMediaUrl and Media class to new Instagram structure
1 parent a2a508b commit 5d8a2af

File tree

2 files changed

+158
-24
lines changed

2 files changed

+158
-24
lines changed

src/InstagramScraper/Instagram.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,10 @@ public function getMediaByUrl($mediaUrl)
819819
}
820820

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

828828
/**
@@ -2627,4 +2627,30 @@ public function allowPendingRequests($limit = 10)
26272627
['thread_ids' => json_encode($threadIds)]
26282628
);
26292629
}
2630+
2631+
/**
2632+
* @return Account
2633+
* @throws InstagramException
2634+
* @throws InstagramNotFoundException
2635+
*/
2636+
public function getCurrentUserInfo(): Account
2637+
{
2638+
$response = Request::get(Endpoints::USER_FEED_hash, $this->generateHeaders($this->userSession));
2639+
2640+
if ($response->code === static::HTTP_NOT_FOUND) {
2641+
throw new InstagramNotFoundException('Account with given username does not exist.');
2642+
}
2643+
if ($response->code !== static::HTTP_OK) {
2644+
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
2645+
}
2646+
2647+
$this->parseCookies($response->headers);
2648+
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);
2649+
2650+
if(!isset($jsonResponse['data']['user']['username'])){
2651+
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.');
2652+
}
2653+
2654+
return $this->getAccount($jsonResponse['data']['user']['username']);
2655+
}
26302656
}

src/InstagramScraper/Model/Media.php

Lines changed: 130 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class Media extends AbstractModel
1414
const TYPE_VIDEO = 'video';
1515
const TYPE_SIDECAR = 'sidecar';
1616
const TYPE_CAROUSEL = 'carousel';
17+
18+
const MEDIA_TYPE_IMAGE = 1;
19+
const MEDIA_TYPE_VIDEO = 2;
20+
const MEDIA_TYPE_CAROUSEL = 8;
1721

1822
/**
1923
* @var string
@@ -197,6 +201,8 @@ class Media extends AbstractModel
197201
* @var array
198202
*/
199203
protected $taggedUsersIds=[];
204+
205+
private $media_type;
200206

201207
/**
202208
* @param string $code
@@ -587,7 +593,6 @@ protected function initPropertiesCustom($value, $prop, $arr)
587593
$this->commentsDisabled = $value;
588594
break;
589595
case 'comments':
590-
$this->commentsCount = $arr[$prop]['count'];
591596
break;
592597
case 'edge_liked_by':
593598
case 'edge_media_preview_like':
@@ -637,7 +642,7 @@ protected function initPropertiesCustom($value, $prop, $arr)
637642
}
638643
break;
639644
case 'caption':
640-
$this->caption = $arr[$prop];
645+
$this->caption = $value['text'] ?? '';
641646
break;
642647
case 'accessibility_caption':
643648
$this->altText = $value;
@@ -666,10 +671,10 @@ protected function initPropertiesCustom($value, $prop, $arr)
666671
break;
667672
case 'location':
668673
if (isset($arr[$prop])) {
669-
$this->locationId = $arr[$prop]['id'] ? $arr[$prop]['id'] : null;
670-
$this->locationName = $arr[$prop]['name'] ? $arr[$prop]['name'] : null;
671-
$this->locationSlug = $arr[$prop]['slug'] ? $arr[$prop]['slug'] : null;
672-
$this->locationAddressJson = isset($arr[$prop]['address_json']) ? $arr[$prop]['address_json'] : null;
674+
$this->locationId = $arr[$prop]['id'] ?? null;
675+
$this->locationName = $arr[$prop]['name'] ?? null;
676+
$this->locationSlug = $arr[$prop]['slug'] ?? null;
677+
$this->locationAddressJson = $arr[$prop]['address_json'] ?? null;
673678
}
674679
break;
675680
case 'owner':
@@ -684,6 +689,7 @@ protected function initPropertiesCustom($value, $prop, $arr)
684689
case 'video_url':
685690
$this->videoStandardResolutionUrl = $value;
686691
break;
692+
case 'view_count':
687693
case 'video_view_count':
688694
$this->videoViews = $value;
689695
break;
@@ -777,6 +783,57 @@ protected function initPropertiesCustom($value, $prop, $arr)
777783
}
778784
}
779785
break;
786+
case 'comment_count':
787+
$this->commentsCount = $value;
788+
break;
789+
case 'media_type':
790+
$this->media_type = $value;
791+
switch ($value){
792+
case static::MEDIA_TYPE_IMAGE:
793+
$this->type = static::TYPE_IMAGE;
794+
break;
795+
case static::MEDIA_TYPE_VIDEO:
796+
$this->type = static::TYPE_VIDEO;
797+
break;
798+
case static::MEDIA_TYPE_CAROUSEL:
799+
$this->type = static::TYPE_CAROUSEL;
800+
break;
801+
}
802+
break;
803+
case 'image_versions2':
804+
foreach ($value['candidates'] as $media) {
805+
$mediasUrl[] = $media['url'];
806+
switch ($media['width']) {
807+
case 150:
808+
$this->imageThumbnailUrl = $media['url'];
809+
break;
810+
case 320:
811+
$this->imageLowResolutionUrl = $media['url'];
812+
break;
813+
case 750:
814+
$this->imageStandardResolutionUrl = $media['url'];
815+
break;
816+
case 1080:
817+
$this->imageHighResolutionUrl = $media['url'];
818+
break;
819+
}
820+
}
821+
break;
822+
case 'video_versions':
823+
foreach ($value as $media) {
824+
switch ($media['type']) {
825+
case 101:
826+
$this->videoStandardResolutionUrl = $media['url'];
827+
break;
828+
case 102:
829+
$this->videoLowResolutionUrl = $media['url'];
830+
break;
831+
case 103:
832+
$this->videoLowBandwidthUrl = $media['url'];
833+
break;
834+
}
835+
}
836+
break;
780837
}
781838
if (!$this->ownerId && !is_null($this->owner)) {
782839
$this->ownerId = $this->getOwner()->getId();
@@ -793,25 +850,76 @@ protected function initPropertiesCustom($value, $prop, $arr)
793850
private static function setCarouselMedia($mediaArray, $carouselArray, $instance)
794851
{
795852
$carouselMedia = new CarouselMedia();
796-
$carouselMedia->setType($carouselArray['type']);
797-
798-
if (isset($carouselArray['images'])) {
799-
$carouselImages = self::getImageUrls($carouselArray['images']['standard_resolution']['url']);
800-
$carouselMedia->setImageLowResolutionUrl($carouselImages['low']);
801-
$carouselMedia->setImageThumbnailUrl($carouselImages['thumbnail']);
802-
$carouselMedia->setImageStandardResolutionUrl($carouselImages['standard']);
803-
$carouselMedia->setImageHighResolutionUrl($carouselImages['high']);
804-
}
853+
if(isset($carouselArray['type'])) {
854+
$carouselMedia->setType($carouselArray['type']);
855+
856+
if (isset($carouselArray['images'])) {
857+
$carouselImages = self::getImageUrls($carouselArray['images']['standard_resolution']['url']);
858+
$carouselMedia->setImageLowResolutionUrl($carouselImages['low']);
859+
$carouselMedia->setImageThumbnailUrl($carouselImages['thumbnail']);
860+
$carouselMedia->setImageStandardResolutionUrl($carouselImages['standard']);
861+
$carouselMedia->setImageHighResolutionUrl($carouselImages['high']);
862+
}
805863

806-
if ($carouselMedia->getType() === self::TYPE_VIDEO) {
807-
if (isset($mediaArray['video_views'])) {
808-
$carouselMedia->setVideoViews($carouselArray['video_views']);
864+
if ($carouselMedia->getType() === self::TYPE_VIDEO) {
865+
if (isset($mediaArray['video_views'])) {
866+
$carouselMedia->setVideoViews($carouselArray['video_views']);
867+
}
868+
if (isset($carouselArray['videos'])) {
869+
$carouselMedia->setVideoLowResolutionUrl($carouselArray['videos']['low_resolution']['url']);
870+
$carouselMedia->setVideoStandardResolutionUrl($carouselArray['videos']['standard_resolution']['url']);
871+
$carouselMedia->setVideoLowBandwidthUrl($carouselArray['videos']['low_bandwidth']['url']);
872+
}
873+
}
874+
} elseif(isset($carouselArray['media_type'])) {
875+
switch ($carouselArray['media_type']){
876+
case static::MEDIA_TYPE_IMAGE:
877+
$carouselMedia->setType(static::TYPE_IMAGE);
878+
break;
879+
case static::MEDIA_TYPE_VIDEO:
880+
$carouselMedia->setType(static::TYPE_VIDEO);
881+
break;
809882
}
810-
if (isset($carouselArray['videos'])) {
811-
$carouselMedia->setVideoLowResolutionUrl($carouselArray['videos']['low_resolution']['url']);
812-
$carouselMedia->setVideoStandardResolutionUrl($carouselArray['videos']['standard_resolution']['url']);
813-
$carouselMedia->setVideoLowBandwidthUrl($carouselArray['videos']['low_bandwidth']['url']);
883+
884+
if($carouselArray['media_type'] == static::MEDIA_TYPE_VIDEO && isset($carouselArray['video_versions'])){
885+
if (isset($mediaArray['view_count'])) {
886+
$carouselMedia->setVideoViews($carouselArray['view_count']);
887+
}
888+
889+
foreach ($carouselArray['video_versions'] as $media) {
890+
switch ($media['type']) {
891+
case 101:
892+
$carouselMedia->setVideoStandardResolutionUrl($media['url']);
893+
break;
894+
case 102:
895+
$carouselMedia->setVideoLowResolutionUrl($media['url']);
896+
break;
897+
case 103:
898+
$carouselMedia->setVideoLowBandwidthUrl($media['url']);
899+
break;
900+
}
901+
}
814902
}
903+
904+
if($carouselArray['media_type'] == static::MEDIA_TYPE_IMAGE && isset($carouselArray['image_versions2'])){
905+
foreach ($carouselArray['image_versions2']['candidates'] as $media) {
906+
$mediasUrl[] = $media['url'];
907+
switch ($media['width']) {
908+
case 150:
909+
$carouselMedia->setImageThumbnailUrl($media['url']);
910+
break;
911+
case 320:
912+
$carouselMedia->setImageLowResolutionUrl($media['url']);
913+
break;
914+
case 750:
915+
$carouselMedia->setImageStandardResolutionUrl($media['url']);
916+
break;
917+
case 1080:
918+
$carouselMedia->setImageHighResolutionUrl($media['url']);
919+
break;
920+
}
921+
}
922+
}
815923
}
816924
array_push($instance->carouselMedia, $carouselMedia);
817925
return $mediaArray;

0 commit comments

Comments
 (0)