Support » Plugin: Related YouTube Videos » Related Youtube Playlist

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Chris

    (@zenation)

    Nope. This plugin is mainly for finding videos based on dynamic search terms. Usually, the results are like a million times better for matching stand-alone video(s) than for playlists. YT seems to only try to match the playlist titles against search terms – which is almost never “the best” you can get.

    If you want to embed specific playlists or channels you might try “Youtube Channel Gallery” or one of these plugins https://wordpress.org/plugins/tags/youtube-playlist.

    thanks for your reply

    I did not mean Youtube Playlist

    This Example
    http://associategoliath.com/demo-site/new-nintendo-3ds-xl-red/

    It works just like your Plugin

    But Number of videos (or search results) Small

    Api.php

    <?php
    require_once ASG_APPLICATION_PATH . '/models/AbstractModel.php';
    
    class Asg_Model_Youtube_Api extends Asg_Model_AbstractModel
    {
        protected $_http;
    
        protected $_apiKey;
    
        protected $_baseUrl = 'https://www.googleapis.com/youtube/v3/';
    
        protected $_enabled;
    
        protected $_retryCount = 0;
    
        public function __construct()
        {
            $this->_http = Asg_Application::getService('Affiliate_Helper_Http')
                ->getClient();
    
            $options = Asg_Application::getModel('Options');
    
            $this->_apiKey = $options->getValue('google_api_key');
    
            $this->_enabled = '' != $this->_apiKey;
        }
    
        public function search($keyword, $maxResults, $type='video')
        {
            return $this->request('search', array(
                'part' => 'snippet',
                'q' => $keyword,
                'type' => $type,
                'maxResults' => $maxResults,
            ));
        }
    
        public function getVideoInfo($id)
        {
            $result = $this->request('videos', array(
                'part' => 'snippet',
                'id' => $id,
            ));
    
            if (!isset($result['items'][0])) {
                throw new Asg_Model_Youtube_Api_Exception(
                    "Video ID '{$id}' not found.");
            }
    
            return $result['items'][0];
        }
    
        public function request($resource, array $getParams,
            array $postParams=array(), $method='GET')
        {
            if (!$this->_enabled) {
                throw new Asg_Model_Youtube_Api_Exception(
                    'YouTube API key is not set up.');
            }
    
            $getParams['key'] = $this->_apiKey;
    
            for ($iRetry = 0; $iRetry <= $this->_retryCount; $iRetry++) {
                try {
                    $response = $this->_http
                        ->setUri($this->_baseUrl . $resource)
                        ->setParameterGet($getParams)
                        ->setParameterPost($postParams)
                        ->request($method);
                } catch (AsgZend_Http_Client_Exception $e) {
                    if ($iRetry < $this->_retryCount) {
                        continue;
                    }
                    throw new Asg_Model_Youtube_Api_ConnectionException(
                        "Could not connect to YouTube API ({$e->getMessage()})");
                }
    
                break;
            }
    
            $result = json_decode($response->getBody(), TRUE);
            if (!$result) {
                throw new Asg_Model_Youtube_Api_Exception(
                    'Invalid YouTube API response: Could not decode JSON.');
            }
    
            if (isset($result['error']['message'])) {
                $message = $result['error']['message'];
                if (isset($result['error']['errors'][0]['reason'])) {
                    switch ($result['error']['errors'][0]['reason']) {
                        case 'keyInvalid':
                            $message = 'Invalid Google API key.';
                            break;
                    }
                }
                throw new Asg_Model_Youtube_Api_Exception($message);
            }
    
            if ($response->isError()) {
                throw new Asg_Model_Youtube_Api_Exception(
                    "YouTube API request failed due to HTTP error #{$response->getStatus()} ({$response->getMessage()})");
            }
    
            return $result;
        }
    
        public function isEnabled()
        {
            return $this->_enabled;
        }
    }
    
    class Asg_Model_Youtube_Api_Exception extends Exception
    {
    
    }
    
    class Asg_Model_Youtube_Api_ConnectionException
        extends Asg_Model_Youtube_Api_Exception
    {
    
    }<code></code>

    Playlist.php

    <?php
    require_once ASG_APPLICATION_PATH . '/models/AbstractModel.php';
    
    class Asg_Model_Youtube_Playlist extends Asg_Model_AbstractModel
    {
        public $postId;
    
        public function cacheVideoInfo($id)
        {
            $meta = Asg_Application::getModel('PostMeta')->setPostId($this->postId);
    
            $key = "youtube_snippet_{$id}";
            $info = $meta->getValue($key);
            if (is_array($info)) {
                return $this;
            }
    
            $info = Asg_Application::getModel('Youtube_Api')
                ->getVideoInfo($id);
            $meta->setValue($key, $info);
    
            return $this;
        }
    
        public function generate()
        {
            $meta = Asg_Application::getModel('PostMeta')->setPostId($this->postId);
    
            $manual = $meta->getValue('youtube_manual_list');
            $manual = '' != $manual? explode("\n", $manual) : array();
    
            $search = $meta->getValue('youtube_data');
            $search = isset($search['items'])? $search['items'] : array();
    
            $blacklist = $meta->getValue('youtube_blacklist');
            $blacklist = '' != $blacklist? explode("\n", $blacklist) : array();
    
            $result = array();
    
            foreach ($search as $key=>$item) {
                $id = $item['id']['videoId'];
                if (in_array($id, $blacklist)) {
                    unset($search[$key]);
                    continue;
                }
    
                $item['id'] = $id;
    
                $search[$key] = $item;
            }
    
            if (!$manual) {
                return array_values($search);
            }
    
            $maxResults = (int)Asg_Application::getConfig()->youtube->maxResults;
    
            foreach ($manual as $id) {
                if (in_array($id, $blacklist)) {
                    continue;
                }
    
                $item = $meta->getValue("youtube_snippet_{$id}");
                if (!is_array($item)) {
                    continue;
                }
                $result[$id] = $item;
            }
    
            if (count($result) < $maxResults) {
                foreach ($search as $item) {
                    $id = $item['id'];
                    if (isset($result[$id])) {
                        continue;
                    }
                    $result[] = $item;
                }
            }
    
            $result = array_values($result);
    
            return $result;
        }
    
        public function setPostId($value)
        {
            $this->postId = $value;
    
            return $this;
        }
    }
    Plugin Author Chris

    (@zenation)

    Ah, okay, my mistake.

    You can build your own (HTML) template after retrieving your list of videos via this plugin. The “playlist” can/must then be created via Javascript and CSS.

    So if you happen to have a jQuery plugin or so that can create such a playlist I can help you patching it together with the RYTV plugin.

    ok Thank you so much

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Related Youtube Playlist’ is closed to new replies.