jeffchannell.com

Add LinkedTube as a Video Provider in JomSocial

Posted in Joomla!
2010-08-19 08:05:00 +0000 UTC

I had a request recently from Woman Poker Player to add the video site LinkedTube to the list of available video providers in JomSocial. After a bit of investigation, I ended up developing a solution based on the core YouTube library (as LinkedTube is really merely a wrapper for YouTube videos). I've been given permission to post this here for the benefit of the community, and I've sent it to Azrul as well for possible inclusion in the JomSocial core.

Adding LinkedTube consists of two easy steps. First, create the file components/com_community/libraries/videos/linkedtube.php and add the following code:

<?php
/**
 * @category	Helper
 * @package		JomSocial
 * @copyright (C) 2008 by Slashes & Dots Sdn Bhd - All rights reserved!
 * @license http://www.jomsocial.com Copyrighted Commercial Software
 */
defined('_JEXEC') or die('Restricted access');

require_once (COMMUNITY_COM_PATH.DS.'models'.DS.'videos.php');

/**
 * Class to manipulate data from LinkedTube
 * 	 	
 * @access	public 
 */
class CTableVideoLinkedtube extends CVideoProvider
{
	var $xmlContent = null;
	var $ltContent = null;
	var $ytid = null;
	var $url = '';
	
	/**
	 * Return feedUrl of the video
	 * use 
	 */
	function getFeedUrl() { 
		return 'http://gdata.youtube.com/feeds/api/videos/' . $this->getYoutubeId();
	}
	
	function init($url) {
		$this->url = $url;
	}
	
	/*
	 * Return true if successfully connect to remote video provider
	 * and the video is valid
	 */	 	 
	function isValid() {
		// Connect and get the youtube id
		CFactory::load('helpers', 'remote');
		$this->xmlContent	= cRemoteGetContent($this->getFeedUrl());
		$videoId = $this->getId();
		if (empty($videoId) || $this->xmlContent == 'Invalid id') {
			$this->setError(JText::_('CC INVALID VIDEO ID'));
			return false;
		}
		if($this->xmlContent == false) {
			$this->setError(JText::_('CC ERROR FETCHING VIDEO'));
			return false;
		}
		if($this->xmlContent == 'Video not found') {
			$this->setError(JText::_('CC YOUTUBE VIDEO NOT FOUND'));
			return false;
		}
		return true;
	}
	
	function getYoutubeId() {
		if (empty($this->ytid)) {
			// fetch youtube id from source
			preg_match('`&vid=(.{11})`', $this->getLtContent($this->getId()), $matches);
			$this->ytid = $matches[1];
		}
		return $this->ytid;
	}
	
	function getLtContent($id) {
		if (empty($this->ltContent)) {
			CFactory::load('helpers', 'remote');
			$this->ltContent = cRemoteGetContent("http://www.linkedtube.com/{$id}.htm");
			if($this->ltContent == false) {
				$this->setError(JText::_('CC ERROR FETCHING VIDEO'));
				return false;
			}
		}
		return $this->ltContent;
	}
	
	/**
	 * Extract LinkedTube video id from the video url submitted by the user
	 * 	 	
	 * @access	public
	 * @param	video url
	 * @return videoid	 
	 */
	function getId() {
		$regexp = '`^(https?\\:\\/{2}www\\.linkedtube\\.com\\/)(.{43})(\\.htm)$`i';
		if (!preg_match($regexp, $this->url)) {
			return null;
		} else {
			return preg_replace($regexp, '$2', trim($this->url));
		}
	}
	
	/**
	 * Return the video provider's name
	 * 
	 */
	function getType() {
		return 'linkedtube';
	}
	
	function getTitle() {
		$title = '';
		// Store video title
		$pattern =  "/<title type='text'>(.*?)<\\/title>/i";
		preg_match_all($pattern, $this->xmlContent, $matches);
		if ($matches) {
		    $title = $matches[1][0];
		}
		
		return $title;
	}
	
	function getDescription() {
		$description = '';
		// Store description
		$pattern =  "/<content type='text'>(.*?)<\\/content>/s";
		preg_match_all($pattern, $this->xmlContent, $matches);
		if ($matches) {
		    $description = $matches[1][0];
		}
		
		return $description;
	}
	
	function getDuration() {
		$duration = 0;
		// Store duration
		$pattern =  "/seconds='(.+?)'/i";
		preg_match_all($pattern, $this->xmlContent, $matches);
		if ($matches) {
			$duration = $matches[1][0];
		}
		
		return $duration;
	}
	
	/**
	 * Get video's thumbnail URL from videoid
	 * 
	 * @access 	public
	 * @param 	videoid
	 * @return url
	 */
	function getThumbnail() {
		return 'http://img.youtube.com/vi/' . $this->getYoutubeId() . '/default.jpg';
	}
	
	/**
	 * 
	 * 
	 * @return $embedvideo specific embeded code to play the video
	 */
	function getViewHTML($videoId, $videoWidth, $videoHeight) {
		if (!$videoId) {
			$videoId	= $this->getId();
		}
		// heh, just rip the src url from the content we loaded ;)
		preg_match('`(http\\:\\/\\/www\\.linkedtube\\.com\\/static\\/flash\\/player\\.swf\\?[^"]*?)"`', $this->getLtContent($videoId), $matches);
		if ($matches) {
			return '<embed src="'.$matches[1].'" width="'.$videoWidth.'" height="'.$videoHeight.'" quality="high" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"><noembed><a href="http://www.linkedtube.com/<?php echo $id; ?>.htm">LinkedTube</a></noembed></embed>';
		} else {
			return JText::_('CC ERROR FETCHING VIDEO');
		}
	}

}

Once that's done, let your users know that LinkedTube is a viable option by adding it to the list hard-coded in videos.add.php in your JomSocial template.