Commit 4f42e466 authored by Eloy Lafuente's avatar Eloy Lafuente
Browse files

MDL-21293 tag_youtube block - migratin from YouTube API to Google one

TODO: fix the configuration form to use the new stuff...
parent d51eee7e
......@@ -3,7 +3,6 @@
require_once($CFG->dirroot.'/tag/lib.php');
require_once($CFG->libdir . '/filelib.php');
define('YOUTUBE_DEV_KEY', 'Dlp6qqRbI28');
define('DEFAULT_NUMBER_OF_VIDEOS', 5);
class block_tag_youtube extends block_base {
......@@ -65,11 +64,11 @@ class block_tag_youtube extends block_base {
$numberofvideos = $this->config->numberofvideos;
}
$request = 'http://www.youtube.com/api2_rest?method=youtube.videos.list_by_playlist';
$request .= '&dev_id=' . YOUTUBE_DEV_KEY;
$request .= "&id={$this->config->playlist}";
$request .= "&page=1";
$request .= "&per_page={$numberofvideos}";
$request = 'http://gdata.youtube.com/feeds/api/playlists/' .
$this->config->playlist .
'?start-index=1&max-results=' .
$numberofvideos .
'&format=5';
return $this->fetch_request($request);
}
......@@ -96,11 +95,11 @@ class block_tag_youtube extends block_base {
$numberofvideos = $this->config->numberofvideos;
}
$request = 'http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag';
$request .= '&dev_id='. YOUTUBE_DEV_KEY;
$request .= "&tag={$querytag}";
$request .= "&page=1";
$request .= "&per_page={$numberofvideos}";
$request = 'http://gdata.youtube.com/feeds/api/videos?vq=' .
$querytag .
'&start-index=1&max-results=' .
$numberofvideos .
'&format=5';
return $this->fetch_request($request);
}
......@@ -127,12 +126,14 @@ class block_tag_youtube extends block_base {
$numberofvideos = $this->config->numberofvideos;
}
$request = 'http://www.youtube.com/api2_rest?method=youtube.videos.list_by_category_and_tag';
$request .= '&category_id='.$this->config->category;
$request .= '&dev_id=' . YOUTUBE_DEV_KEY;
$request .= "&tag={$querytag}";
$request .= "&page=1";
$request .= "&per_page={$numberofvideos}";
$request = 'http://gdata.youtube.com/feeds/api/videos?category=' .
$this->category_map_old2new($this->config->category) .
'&vq=' .
$querytag .
'&start-index=1&max-results=' .
$numberofvideos .
'&format=5';
return $this->fetch_request($request);
}
......@@ -152,18 +153,86 @@ class block_tag_youtube extends block_base {
$text = '';
$text .= '<ul class="yt-video-entry unlist img-text">';
foreach($xml->video_list->video as $video){
foreach($xml->entry as $entry){
$media = $entry->children('http://search.yahoo.com/mrss/');
$playerattrs = $media->group->player->attributes();
$url = s($playerattrs['url']);
$thumbattrs = $media->group->thumbnail[0]->attributes();
$thumbnail = s($thumbattrs['url']);
$title = s($media->group->title);
$yt = $media->children('http://gdata.youtube.com/schemas/2007');
$secattrs = $yt->duration->attributes();
$seconds = $secattrs['seconds'];
$text .= '<li>';
$text .= '<div class="clearfix">';
$text .= '<a href="'. s($video->url) . '">';
$text .= '<img alt="" class="youtube-thumb" src="'. $video->thumbnail_url .'" /></a>';
$text .= '</div><span><a href="'. s($video->url) . '">'.s($video->title).'</a></span>';
$text .= '<a href="'. $url . '">';
$text .= '<img alt="" class="youtube-thumb" src="'. $thumbnail .'" /></a>';
$text .= '</div><span><a href="'. $url . '">'. $title .'</a></span>';
$text .= '<div>';
$text .= format_time($video->length_seconds);
$text .= format_time($seconds);
$text .= "</div></li>\n";
}
$text .= "</ul><div class=\"clearer\"></div>\n";
return $text;
}
function get_categories() {
// TODO: Right now using sticky categories from
// http://gdata.youtube.com/schemas/2007/categories.cat
// This should be performed from time to time by the block insead
// and cached somewhere, avoiding deprecated ones and observing regions
return array (
'0' => get_string('anycategory', 'block_tag_youtube'),
'Film' => get_string('filmsanimation', 'block_tag_youtube'),
'Autos' => get_string('autosvehicles', 'block_tag_youtube'),
'Music' => get_string('music', 'block_tag_youtube'),
'Animals'=> get_string('petsanimals', 'block_tag_youtube'),
'Sports' => get_string('sports', 'block_tag_youtube'),
'Travel' => get_string('travel', 'block_tag_youtube'),
'Games' => get_string('gadgetsgames', 'block_tag_youtube'),
'Comedy' => get_string('comedy', 'block_tag_youtube'),
'People' => get_string('peopleblogs', 'block_tag_youtube'),
'News' => get_string('newspolitics', 'block_tag_youtube'),
'Entertainment' => get_string('entertainment', 'block_tag_youtube'),
'Education' => get_string('education', 'block_tag_youtube'),
'Howto' => get_string('howtodiy', 'block_tag_youtube'),
'Tech' => get_string('scienceandtech', 'block_tag_youtube')
);
}
/**
* Provide conversion from old numeric categories available in youtube API
* to the new ones available in the Google API
*
* @param int $oldcat old category code
* @return mixed new category code or 0 (if no match found)
*
* TODO: Someday this should be applied on upgrade for all the existing
* block instances so we won't need the mapping any more. That would imply
* to implement restore handling to perform the conversion of old blocks.
*/
function category_map_old2new($oldcat) {
$oldoptions = array (
0 => '0',
1 => 'Film',
2 => 'Autos',
23 => 'Comedy',
24 => 'Entertainment',
10 => 'Music',
25 => 'News',
22 => 'People',
15 => 'Animals',
26 => 'Howto',
17 => 'Sports',
19 => 'Travel',
20 => 'Games'
);
if (array_key_exists($oldcat, $oldoptions)) {
return $oldoptions[$oldcat];
} else {
return $oldcat;
}
}
}
......@@ -9,6 +9,7 @@ $string['includeonlyvideosfromplaylist'] = 'Include only videos from the playlis
$string['filmsanimation'] = 'Films &amp; Animation';
$string['autosvehicles'] = 'Autos &amp; Vehicles';
$string['comedy'] = 'Comedy';
$string['education'] = 'Education';
$string['entertainment'] = 'Entertainment';
$string['music'] = 'Music';
$string['newspolitics'] = 'News &amp; Politics';
......@@ -18,5 +19,6 @@ $string['howtodiy'] = 'How-to &amp; DIY';
$string['sports'] = 'Sports';
$string['travel'] = 'Travel &amp; Places';
$string['gadgetsgames'] = 'Gadgets &amp; Games';
$string['scienceandtech'] = 'Science &amp; Tech';
?>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment