_youtube = new \Madcoda\Youtube\Youtube([ 'key' => $config['key'] ]); } protected function _getFeedUrl($feed) { return 'https://www.youtube.com/feeds/videos.xml?channel_id=' . $feed; } protected function _getCachePath() { return '../cache/youtube.%s'; } protected function _mapRssItem($item) { $itemObj = parent::_mapRssItem($item); if (array_key_exists('descriptionlink', $this->_options)) { $linkConfig = explode(':', $this->_options['descriptionlink']); $linkDomain = NULL; if (!is_numeric($linkConfig[0])) { $linkDomain = array_shift($linkConfig); if (!$linkConfig) { $linkConfig = ['1']; } } $linkIndex = intval($linkConfig[0]) - 1; $textLinks = []; if (preg_match_all('#(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-&?=%.]+#', $itemObj->Text, $textLinks)) { $textLinks = $textLinks[0]; if ($linkDomain) { $textLinks = array_values(array_filter($textLinks, function($link) use($linkDomain) { if($linkParts = parse_url($link)) { return $linkParts['host'] == $linkDomain; } })); } if (count($textLinks) > $linkIndex) { $itemObj->Link = $textLinks[$linkIndex]; } } } return $itemObj; } private function _getVideoMetadata($ids) { $cacheFile = sprintf($this->_getCachePath() . '.metadata', $this->_feed); $cacheData = []; if (file_exists($cacheFile)) { $cacheData = unserialize(file_get_contents($cacheFile)); } $oldIDs = array_keys($cacheData); $newIDs = array_diff($ids, $oldIDs); if ($newIDs) { $metadata = $this->_youtube->getVideosInfo($newIDs); foreach ($metadata as $video) { $cacheData[$video->id] = $video; } } file_put_contents($cacheFile, serialize($cacheData)); return array_filter($cacheData, function($item) use($ids) { return in_array($item->id, $ids); }); } private function _getVideoDurations($ids) { $metadata = $this->_getVideoMetadata($ids); $return = []; foreach ($ids as $id) { $duration = new \DateInterval($metadata[$id]->contentDetails->duration); $return[$id] = $duration->h * 3600 + $duration->i * 60 + $duration->s; } return $return; } private function _getVideoID($item) { return preg_replace('/^yt:video:/', '', $item->ID); } protected function _filterItemContent($items) { $items = parent::_filterItemContent($items); if (array_key_exists('duration', $this->_options)) { $duration = intval($this->_options['duration']); $videoIDs = array_map(array($this, '_getVideoID'), $items); $durations = $this->_getVideoDurations($videoIDs); $minDuration = self::DEFAULT_MIN_DURATION; if (is_numeric($this->_options['duration'])) { $minDuration = intval($this->_options['duration']); } $items = array_filter($items, function($item) use($minDuration, $durations) { $videoID = $this->_getVideoID($item); return $durations[$videoID] >= $minDuration; }); } return $items; } } ?>