You are here

private function StatusTwitter::getTweetMedia in Heartbeat 8

2 calls to StatusTwitter::getTweetMedia()
StatusTwitter::sendRequest in modules/statusmessage/src/StatusTwitter.php
StatusTwitter::setNodeData in modules/statusmessage/src/StatusTwitter.php

File

modules/statusmessage/src/StatusTwitter.php, line 201

Class

StatusTwitter

Namespace

Drupal\statusmessage

Code

private function getTweetMedia($data) {
  $media = new \stdClass();
  $images = [];
  $video = null;
  $userImage = null;
  if ($data->user->profile_image_url_https) {
    $userImage = file_get_contents($data->user->profile_image_url_https);
    $file = file_save_data($userImage, 'public://' . substr($data->user->profile_image_url_https, strrpos($data->user->profile_image_url_https, '/') + 1), FILE_EXISTS_REPLACE);
    $userImage = $file
      ->id();
  }
  foreach ($data->extended_entities->media as $media) {
    $image = file_get_contents($media->media_url);
    $file = file_save_data($image, 'public://' . substr($media->media_url, strrpos($media->media_url, '/') + 1), FILE_EXISTS_REPLACE);
    $images[] = $file
      ->id();
  }
  if (!empty($data->extended_entities->media[0]->video_info->variants)) {
    $z = null;
    $vidUrl = null;
    $bitrate = new \stdClass();
    $bitrate->value = null;
    $bitrate->index = null;
    $variantCount = count($data->extended_entities->media[0]->video_info->variants);
    if ($variantCount > 1) {
      for ($z = 0; $z < $variantCount; $z++) {
        if ($data->extended_entities->media[0]->video_info->variants[$z]->bitrate && $data->extended_entities->media[0]->video_info->variants[$z]->content_type === 'video/mp4') {
          if ($data->extended_entities->media[0]->video_info->variants[$z]->bitrate > $bitrate->value) {
            $bitrate->value = $data->extended_entities->media[0]->video_info->variants[$z]->bitrate;
            $bitrate->index = $z;
          }
        }
      }
    }
    else {
      $bitrate->index = 0;
    }
    if ($bitrate->index !== null) {
      $data->extended_entities->media[0]->video_info->variants[$bitrate->index]->url;

      //        $video = system_retrieve_file($vidUrl, null, TRUE);
      //        $file = File::create([
      //          'id' => 'id',
      //        ])->save();
      $video = file_get_contents($data->extended_entities->media[0]->video_info->variants[$bitrate->index]->url);
      $file = file_save_data($video, 'public://' . substr($data->extended_entities->media[0]->video_info->variants[$bitrate->index]->url, strrpos($data->extended_entities->media[0]->video_info->variants[$bitrate->index]->url, '/') + 1), FILE_EXISTS_REPLACE);
      $video = $file
        ->id();
    }
  }
  $media->images = $images;
  $media->video = $video;
  $media->userImage = $userImage;
  return $media;
}