You are here

private function FeedsYouTubeFetcher::parseVideoItems in Feeds: YouTube Parser 8

Parse a YouTube video feed.

Parameters

array $video_items:

Return value

array

1 call to FeedsYouTubeFetcher::parseVideoItems()
FeedsYouTubeFetcher::requestNextPage in src/Feeds/Fetcher/FeedsYouTubeFetcher.php
Perform API request to YouTube API and retrieve search result.

File

src/Feeds/Fetcher/FeedsYouTubeFetcher.php, line 148

Class

FeedsYouTubeFetcher
Constructs FeedsYouTubeFetcher object.

Namespace

Drupal\feeds_youtube\Feeds\Fetcher

Code

private function parseVideoItems($video_items) {
  $items = [];
  foreach ($video_items['items'] as $key => $video) {
    $item = [
      'video_id' => '',
      'video_url' => '',
      'title' => '',
      'description' => '',
      'thumbnail_default' => '',
      'thumbnail_medium' => '',
      'thumbnail_high' => '',
      'thumbnail_standard' => '',
      'thumbnail_maxres' => '',
      'category' => '',
      'tags' => '',
      'duration' => '',
      'duration_raw' => '',
      'published_datetime' => '',
      'published_timestamp' => '',
      'view_count' => '',
      'fav_count' => '',
      'likes' => '',
      'dislikes' => '',
      'favorites' => '',
      'embedded_player' => '',
    ];
    if (isset($video['id']) && is_array($video['id']) && isset($video['id']['videoId'])) {
      $item['video_id'] = $video['id']['videoId'];
    }
    elseif (isset($video['contentDetails']) && isset($video['contentDetails']['videoId'])) {
      $item['video_id'] = $video['contentDetails']['videoId'];
    }
    elseif (isset($video['snippet']) && isset($video['snippet']['resourceId']) && isset($video['snippet']['resourceId']['videoId'])) {
      $item['video_id'] = $video['snippet']['resourceId']['videoId'];
    }
    if (!empty($item['video_id'])) {
      $item['video_url'] = 'https://www.youtube.com/watch?v=' . $item['video_id'];
    }
    if (isset($video['snippet'])) {
      $item['title'] = $video['snippet']['title'];
      $item['description'] = $video['snippet']['description'];
      if (isset($video['snippet']['thumbnails']['default'])) {
        $item['thumbnail_default'] = $video['snippet']['thumbnails']['default']['url'];
      }
      if (isset($video['snippet']['thumbnails']['standard'])) {
        $item['thumbnail_standard'] = $video['snippet']['thumbnails']['standard']['url'];
      }
      if (isset($video['snippet']['thumbnails']['medium'])) {
        $item['thumbnail_medium'] = $video['snippet']['thumbnails']['medium']['url'];
      }
      if (isset($video['snippet']['thumbnails']['high'])) {
        $item['thumbnail_high'] = $video['snippet']['thumbnails']['high']['url'];
      }
      if (isset($video['snippet']['thumbnails']['maxres'])) {
        $item['thumbnail_maxres'] = $video['snippet']['thumbnails']['maxres']['url'];
      }
      if (isset($video['snippet']['categoryId'])) {
        $item['category'] = $video['snippet']['categoryId'];
      }
      if (isset($video['snippet']['tags'])) {
        $item['tags'] = implode(', ', $video['snippet']['tags']);
      }
      $published_timestamp = strtotime($video['snippet']['publishedAt']);
    }
    if (isset($video['contentDetails'])) {
      if (isset($video['contentDetails']['duration'])) {
        $item['duration'] = $this
          ->timeToDuration($video['contentDetails']['duration']);
        $item['duration_raw'] = $video['contentDetails']['duration'];
      }
      if (!isset($published_timestamp) && isset($video['contentDetails']['videoPublishedAt'])) {
        $published_timestamp = strtotime($video['snippet']['publishedAt']);
      }
    }
    if (isset($video['statistics'])) {
      $item['view_count'] = $video['statistics']['viewCount'];
      $item['fav_count'] = $video['statistics']['favoriteCount'];
      $item['likes'] = $video['statistics']['likeCount'];
      $item['dislikes'] = $video['statistics']['dislikeCount'];
      $item['favorites'] = $video['statistics']['favoriteCount'];
    }
    if (isset($video['player'])) {
      $item['embedded_player'] = $video['player']['embedHtml'];
    }
    if (isset($published_timestamp)) {
      $item['published_datetime'] = date('Y-m-d H:i:s', $published_timestamp);
      $item['published_timestamp'] = $published_timestamp;
    }
    $items[] = $item;
  }
  return $items;
}