Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sidecar don't return complete data #221

Closed
xx19941215 opened this issue Nov 21, 2017 · 1 comment · Fixed by #266
Closed

Sidecar don't return complete data #221

xx19941215 opened this issue Nov 21, 2017 · 1 comment · Fixed by #266

Comments

@xx19941215
Copy link

I want to crawl this url https://www.instagram.com/p/BbrT88PByGY/
this is result
image
The returned data only contains the picture of the cover...

@Parviz-Elite
Copy link

Parviz-Elite commented Nov 22, 2017

i have the same problem, replace this code in \src\InstagramScraper\Model\Media.php
and this function to get data : $media->getsidecarMedia();

namespace InstagramScraper\Model;

use InstagramScraper\Endpoints;

/**

  • Class Media

  • @Package InstagramScraper\Model
    */
    class Media extends AbstractModel
    {
    const TYPE_IMAGE = 'image';
    const TYPE_VIDEO = 'video';
    const TYPE_SIDECAR = 'sidecar';
    const TYPE_CAROUSEL = 'carousel';

    /**

    • @var string
      */
      protected $id = '';

    /**

    • @var string
      */
      protected $shortCode = '';

    /**

    • @var int
      */
      protected $createdTime = 0;

    /**

    • @var string
      */
      protected $type = '';

    /**

    • @var string
      */
      protected $link = '';

    /**

    • @var string
      */
      protected $imageLowResolutionUrl = '';

    /**

    • @var string
      */
      protected $imageThumbnailUrl = '';

    /**

    • @var string
      */
      protected $imageThumbnailSrc = '';

    /**

    • @var string
      */
      protected $imageStandardResolutionUrl = '';

    /**

    • @var string
      */
      protected $imageHighResolutionUrl = '';

    /**

    • @var array
      */
      protected $carouselMedia = [];

    /**

    • @var array
      */
      protected $sidecarMedia = [];

    /**

    • @var string
      */
      protected $caption = '';

    /**

    • @var bool
      */
      protected $isCaptionEdited = false;

    /**

    • @var bool
      */
      protected $isAd = false;

    /**

    • @var string
      */
      protected $videoLowResolutionUrl = '';

    /**

    • @var string
      */
      protected $videoStandardResolutionUrl = '';

    /**

    • @var string
      */
      protected $videoLowBandwidthUrl = '';

    /**

    • @var int
      */
      protected $videoViews = 0;

    /**

    • @var Account
      */
      protected $owner;

    /**

    • @var int
      */
      protected $ownerId = 0;

    /**

    • @var int
      */
      protected $likesCount = 0;

    /**

    • @var
      */
      protected $locationId;

    /**

    • @var string
      */
      protected $locationName = '';

    /**

    • @var string
      */
      protected $commentsCount = 0;

    /**

    • @param string $code
    • @return int
      */
      public static function getIdFromCode($code)
      {
      $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
      $id = 0;
      for ($i = 0; $i < strlen($code); $i++) {
      $c = $code[$i];
      $id = $id * 64 + strpos($alphabet, $c);
      }
      return $id;
      }

    /**

    • @param string $id
    • @return mixed
      */
      public static function getLinkFromId($id)
      {
      $code = Media::getCodeFromId($id);
      return Endpoints::getMediaPageLink($code);
      }

    /**

    • @param string $id
    • @return string
      */
      public static function getCodeFromId($id)
      {
      $parts = explode('', $id);
      $id = $parts[0];
      $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-
      ';
      $code = '';
      while ($id > 0) {
      $remainder = $id % 64;
      $id = ($id - $remainder) / 64;
      $code = $alphabet{$remainder} . $code;
      };
      return $code;
      }

    /**

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

    /**

    • @return string
      */
      public function getShortCode()
      {
      return $this->shortCode;
      }

    /**

    • @return int
      */
      public function getCreatedTime()
      {
      return $this->createdTime;
      }

    /**

    • @return string
      */
      public function getType()
      {
      return $this->type;
      }

    /**

    • @return string
      */
      public function getLink()
      {
      return $this->link;
      }

    /**

    • @return string
      */
      public function getImageLowResolutionUrl()
      {
      return $this->imageLowResolutionUrl;
      }

    /**

    • @return string
      */
      public function getImageThumbnailUrl()
      {
      return $this->imageThumbnailUrl;
      }

    /**

    • @return string
      */
      public function getImageThumbnailSrc()
      {
      return $this->imageThumbnailSrc;
      }

    /**

    • @return string
      */
      public function getImageStandardResolutionUrl()
      {
      return $this->imageStandardResolutionUrl;
      }

    /**

    • @return string
      */
      public function getImageHighResolutionUrl()
      {
      return $this->imageHighResolutionUrl;
      }

    /**

    • @return array
      */
      public function getCarouselMedia()
      {
      return $this->carouselMedia;
      }

    /**

    • @return array
      */
      public function getsidecarMedia()
      {
      return $this->sidecarMedia;
      }

    /**

    • @return string
      */
      public function getCaption()
      {
      return $this->caption;
      }

    /**

    • @return bool
      */
      public function isCaptionEdited()
      {
      return $this->isCaptionEdited;
      }

    /**

    • @return bool
      */
      public function isAd()
      {
      return $this->isAd;
      }

    /**

    • @return string
      */
      public function getVideoLowResolutionUrl()
      {
      return $this->videoLowResolutionUrl;
      }

    /**

    • @return string
      */
      public function getVideoStandardResolutionUrl()
      {
      return $this->videoStandardResolutionUrl;
      }

    /**

    • @return string
      */
      public function getVideoLowBandwidthUrl()
      {
      return $this->videoLowBandwidthUrl;
      }

    /**

    • @return int
      */
      public function getVideoViews()
      {
      return $this->videoViews;
      }

    /**

    • @return int
      */
      public function getOwnerId()
      {
      return $this->ownerId;
      }

    /**

    • @return int
      */
      public function getLikesCount()
      {
      return $this->likesCount;
      }

    /**

    • @return mixed
      */
      public function getLocationId()
      {
      return $this->locationId;
      }

    /**

    • @return string
      */
      public function getLocationName()
      {
      return $this->locationName;
      }

    /**

    • @return string
      */
      public function getCommentsCount()
      {
      return $this->commentsCount;
      }

    /**

    • @param $value
    • @param $prop
      */
      protected function initPropertiesCustom($value, $prop, $arr)
      {
      switch ($prop) {
      case 'id':
      $this->id = $value;
      break;
      case 'type':
      $this->type = $value;
      break;
      case 'created_time':
      $this->createdTime = (int)$value;
      break;
      case 'code':
      $this->shortCode = $value;
      $this->link = Endpoints::getMediaPageLink($this->shortCode);
      break;
      case 'link':
      $this->link = $value;
      break;
      case 'comments':
      $this->commentsCount = $arr[$prop]['count'];
      break;
      case 'likes':
      $this->likesCount = $arr[$prop]['count'];
      break;
      case 'images':
      $images = self::getImageUrls($arr[$prop]['standard_resolution']['url']);
      $this->imageLowResolutionUrl = $images['low'];
      $this->imageThumbnailUrl = $images['thumbnail'];
      $this->imageStandardResolutionUrl = $images['standard'];
      $this->imageHighResolutionUrl = $images['high'];
      break;
      case 'carousel_media':
      $this->type = self::TYPE_CAROUSEL;
      $this->carouselMedia = [];
      foreach ($arr["carousel_media"] as $carouselArray) {
      $temp[] = self::setCarouselMedia($arr, $carouselArray, $this);
      }
      $this->carouselMedia = $temp;
      break;
      case 'caption':
      $this->caption = $arr[$prop];
      break;
      case 'video_views':
      $this->videoViews = $value;
      $this->type = static::TYPE_VIDEO;
      break;
      case 'videos':
      $this->videoLowResolutionUrl = $arr[$prop]['low_resolution']['url'];
      $this->videoStandardResolutionUrl = $arr[$prop]['standard_resolution']['url'];
      $this->videoLowBandwidthUrl = $arr[$prop]['low_bandwidth']['url'];
      break;
      case 'location':
      switch ($prop) {
      case 'id':
      $this->locationId = $value[$prop];
      break;
      case 'name':
      $this->locationId = $value[$prop];
      break;
      }
      $this->locationName = $arr[$prop]['name'];
      break;
      case 'user':
      $this->owner = Account::create($arr[$prop]);
      break;
      case 'is_video':
      if ((bool)$value) {
      $this->type = static::TYPE_VIDEO;
      }
      break;
      case 'video_url':
      $this->videoStandardResolutionUrl = $value;
      break;
      case 'video_view_count':
      $this->videoViews = $value;
      break;
      case 'caption_is_edited':
      $this->isCaptionEdited = $value;
      break;
      case 'is_ad':
      $this->isAd = $value;
      break;
      case 'taken_at_timestamp':
      $this->createdTime = $value;
      break;
      case 'shortcode':
      $this->shortCode = $value;
      $this->link = Endpoints::getMediaPageLink($this->shortCode);
      break;
      case 'edge_media_to_comment':
      $this->commentsCount = $arr[$prop]['count'];
      break;
      case 'edge_media_preview_like':
      $this->likesCount = $arr[$prop]['count'];
      break;
      case 'display_url':
      $images = self::getImageUrls($arr[$prop]);
      $this->imageStandardResolutionUrl = $images['standard'];
      $this->imageLowResolutionUrl = $images['low'];
      $this->imageHighResolutionUrl = $images['high'];
      $this->imageThumbnailUrl = $images['thumbnail'];
      break;
      case 'edge_media_to_caption':
      if (is_array($arr[$prop]['edges']) && !empty($arr[$prop]['edges'])) {
      $first_caption = $arr[$prop]['edges'][0];
      if (is_array($first_caption) && isset($first_caption['node'])) {
      if (is_array($first_caption['node']) && isset($first_caption['node']['text'])) {
      $this->caption = $arr[$prop]['edges'][0]['node']['text'];
      }
      }
      }
      break;
      case 'edge_sidecar_to_children':
      $this->type = self::TYPE_SIDECAR;
      $this->sidecarMedia = [];
      foreach ($arr[$prop]['edges'] as $sidecarArray) {
      $sctmp[] = self::setsidecarMedia($arr, $sidecarArray, $this);
      }
      $this->sidecarMedia = $sctmp;
      break;
      case 'owner':
      $this->owner = Account::create($arr[$prop]);
      break;
      case 'date':
      $this->createdTime = (int)$value;
      break;
      case 'display_src':
      $images = static::getImageUrls($value);
      $this->imageStandardResolutionUrl = $images['standard'];
      $this->imageLowResolutionUrl = $images['low'];
      $this->imageHighResolutionUrl = $images['high'];
      $this->imageThumbnailUrl = $images['thumbnail'];
      if (!isset($this->type)) {
      $this->type = static::TYPE_IMAGE;
      }
      break;
      case 'thumbnail_src':
      $this->imageThumbnailSrc = $arr[$prop];
      break;
      case '__typename':
      if ($value == 'GraphImage') {
      $this->type = static::TYPE_IMAGE;
      } else if ($value == 'GraphVideo') {
      $this->type = static::TYPE_VIDEO;
      } else if ($value == 'GraphSidecar') {
      $this->type = static::TYPE_SIDECAR;
      }
      break;
      }
      if (!$this->ownerId && !is_null($this->owner)) {
      $this->ownerId = $this->getOwner()->getId();
      }
      }

    /**

    • @param string $imageUrl
    • @return array
      */
      private static function getImageUrls($imageUrl)
      {
      $parts = explode('/', parse_url($imageUrl)['path']);
      $imageName = $parts[sizeof($parts) - 1];
      $urls = [
      'thumbnail' => Endpoints::INSTAGRAM_CDN_URL . 't/s150x150/' . $imageName,
      'low' => Endpoints::INSTAGRAM_CDN_URL . 't/s320x320/' . $imageName,
      'standard' => Endpoints::INSTAGRAM_CDN_URL . 't/s640x640/' . $imageName,
      'high' => Endpoints::INSTAGRAM_CDN_URL . 't/' . $imageName,
      ];
      return $urls;
      }

    /**

    • @param $mediaArray

    • @param $carouselArray

    • @param $instance

    • @return mixed
      */
      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 ($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']);
      }
      }
      array_push($instance->carouselMedia, $carouselMedia);
      return $mediaArray;
      }

    private static function setsidecarMedia($mediaArray, $sidecarArray, $instance)
    {
    foreach ( $sidecarArray as $mdia ) {
    $mdtmp[] = $mdia["display_url"];
    }
    return $mdtmp;
    }

    /**

    • @return Account
      */
      public function getOwner()
      {
      return $this->owner;
      }
      }

koninka added a commit to koninka/instagram-php-scraper that referenced this issue Feb 2, 2018
@raiym raiym closed this as completed in #266 Feb 2, 2018
raiym added a commit that referenced this issue Feb 2, 2018
Add sidecar images for 'sidecar' media type. Fixes #221
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants