Permalink
Browse files

Added video search funtion

  • Loading branch information...
1 parent b90e791 commit a5e0d12d7de9ae9ccefd22d1687e41c84d6ca6f9 @stuartloxton committed Jun 1, 2008
@@ -5,17 +5,26 @@ class YtdatasourceController extends AppController {
var $Youtube;
- function beforeFilter() {
- App::import('ConnectionManager');
- $this->Youtube =& ConnectionManager::getDataSource('youtube');
- }
+ function beforeFilter() {
+ App::import('ConnectionManager');
+ $this->Youtube =& ConnectionManager::getDataSource('youtube');
+ }
function index()
{
$video = $this->Youtube->video('GIel7vbRkMQ');
$this->set("video", $video);
}
+ function search() {
+ $search = $_GET['search'];
+ $results = $this->Youtube->search($search);
+ debug($results);
+ $this->set('totalResults', $results['totalResults']);
+ $this->set('startIndex', $results['startIndex']);
+ $this->set('itemsPerPage', $results['itemsPerPage']);
+ }
+
function latest($page=0)
{
@@ -2,6 +2,7 @@
class YoutubeSource extends DataSource{
var $Snoopy;
+ var $xml;
var $config;
var $description = "Youtube data API";
@@ -12,12 +13,79 @@ function __construct($config=null) {
}
uses('xml');
vendor('snoopy/snoopy');
+ $this->xml = new XML;
$this->Snoopy = new Snoopy();
}
+ function search($search, $maxresults=20, $offset=1, $sortby='relevance') {
+ $urlSearch = urlencode($search);
+ $feedUrl = 'http://gdata.youtube.com/feeds/api/videos?vq='.$urlSearch.'&orderby='.$sortby.'&start-index='.$offset.'&max-results='.$maxresults;
+ $this->Snoopy->fetch($feedUrl);
+ $result = $this->Snoopy->results;
+ $this->xml->load($result);
+ //debug($this->xml->children[0]->children[17]);
+ $return = array();
+ $return['title'] = $this->xml->children[0]->children[3]->value;
+ $return['totalResults'] = $this->xml->children[0]->children[11]->value;
+ $return['startIndex'] = $this->xml->children[0]->children[12]->value;
+ $return['itemsPerPage'] = $this->xml->children[0]->children[13]->value;
+ $entries = array();
+ foreach($this->xml->children[0]->children as $description) {
+ if($description->name == 'http://www.w3.org/2005/Atom:entry') {
+ $entry = array();
+ $entry['categories'] = array();
+ foreach($description->children as $attribute) {
+ switch($attribute->name) {
+ case 'http://www.w3.org/2005/Atom:id':
+ $id = str_replace('http://gdata.youtube.com/feeds/api/videos/', '', $attribute->value);
+ $entry['id'] = $id;
+ break;
+ case 'http://www.w3.org/2005/Atom:published':
+ $entry['published'] = $attribute->value;
+ break;
+ case 'http://www.w3.org/2005/Atom:updated':
+ $entry['updated'] = $attribute->value;
+ break;
+ case 'http://www.w3.org/2005/Atom:category':
+ $entry['categories'][] = $attribute->attributes['term'];
+ break;
+ case 'http://www.w3.org/2005/Atom:title':
+ $entry['title'] = $attribute->value;
+ break;
+ case 'http://www.w3.org/2005/Atom:content':
+ $entry['description'] = $attribute->value;
+ break;
+ case 'http://www.w3.org/2005/Atom:author':
+ $entry['author'] = $attribute->children[0]->value;
+ break;
+ case 'http://gdata.youtube.com/schemas/2007:statistics':
+ $entry['viewCount'] = $attribute->attributes['viewCount'];
+ $entry['favoriteCount'] = $attribute->attributes['favoriteCount'];
+ break;
+ case 'http://schemas.google.com/g/2005:rating':
+ $entry['averageRating'] = $attribute->attributes['average'];
+ break;
+ }
+ }
+ $entries[] = $entry;
+ }
+ }
+ $return['entries'] = $entries;
+ return $return;
+ }
+
+ function topRated($time=null)
+ {
+ # code...
+ }
+
function video($videoId) {
return new YoutubeVideo($videoId);
}
+
+ function user($username) {
+ return new YoutubeUser($username);
+ }
}
class YoutubeVideo extends YoutubeSource {
@@ -41,10 +109,9 @@ function __construct($videoId) {
}
function getDetails() {
- $xml = new XML;
$feedresult = $this->Snoopy->fetch($this->videoFeed);
- $xml->load($this->Snoopy->results);
- foreach($xml->children[0]->children as $chunk) {
+ $this->xml->load($this->Snoopy->results);
+ foreach($this->xml->children[0]->children as $chunk) {
switch ($chunk->name) {
case 'http://www.w3.org/2005/Atom:published':
$this->published = $chunk->value;
@@ -75,6 +142,7 @@ function getDetails() {
break;
}
}
+
}
function findRelated() {
$relatedFeed = 'http://gdata.youtube.com/feeds/api/videos/'.$this->id.'/related';
@@ -83,4 +151,22 @@ function findRelated() {
return $this->Snoopy->results;
}
}
+
+class YoutubeUser extends YoutubeSource {
+
+
+ var $username;
+ var $userFeed;
+
+ function __construct($username) {
+ parent::__construct();
+ $this->username = $username;
+ $this->userFeed = 'http://gdata.youtube.com/feeds/api/videos/'.$this->id;
+ $this->getDetails();
+ }
+
+ function getDetails() {
+
+ }
+}
?>
No changes.
@@ -0,0 +1,33 @@
+<dl>
+
+ <dt>Video ID:</dt>
+ <dd><?php echo $video->id; ?></dd>
+
+ <dt>Title:</dt>
+ <dd><?php echo $video->title; ?></dd>
+
+ <dt>Link:</dt>
+ <dd><a href="<?php echo $video->link; ?>"><?php echo $video->link; ?></a></dd>
+
+ <dt>Author:</dt>
+ <dd><?php echo $video->author; ?></dd>
+
+ <dt>Content:</dt>
+ <dd><?php echo $video->content; ?></dd>
+
+ <dt>Categories:</dt>
+ <dd>
+ <?php
+ foreach($video->categories as $category) {
+ echo $category.', ';
+ }
+ ?>
+ </dd>
+
+ <dt>Published:</dt>
+ <dd><?php echo $video->published; ?></dd>
+
+ <dt>Updated:</dt>
+ <dd><?php echo $video->updated; ?></dd>
+
+</dl>

0 comments on commit a5e0d12

Please sign in to comment.