Skip to content

Commit

Permalink
MDL-21293 tag_youtube block - migrate from old (not working anymore!)…
Browse files Browse the repository at this point in the history
… YouTube API

to the new Google Data API
  • Loading branch information
stronk7 committed Jan 21, 2010
1 parent 1584d69 commit bc188d0
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 49 deletions.
122 changes: 94 additions & 28 deletions blocks/tag_youtube/block_tag_youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require_once($CFG->libdir . '/magpie/rss_cache.inc');
require_once($CFG->libdir . '/phpxml/xml.php');

define('YOUTUBE_DEV_KEY', 'Dlp6qqRbI28');
define('DEFAULT_NUMBER_OF_VIDEOS', 5);
define('YOUTUBE_CACHE_EXPIRATION', 1800);

Expand All @@ -30,7 +29,7 @@ function instance_allow_multiple() {

function preferred_width() {
return 140;
}
}

function get_content() {

Expand Down Expand Up @@ -67,19 +66,18 @@ function get_videos_by_playlist(){
$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);
}

function get_videos_by_tag(){

$tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
$tag = optional_param('tag', '', PARAM_TAG); // tag
$tag = optional_param('tag', '', PARAM_TAG); // tag

if ($tag) {
$tagobject = tag_get('name', $tag);
Expand All @@ -98,19 +96,19 @@ function get_videos_by_tag(){
$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);
}

function get_videos_by_tag_and_category(){

$tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
$tag = optional_param('tag', '', PARAM_TAG); // tag
$tag = optional_param('tag', '', PARAM_TAG); // tag

if ($tag) {
$tagobject = tag_get('name', $tag);
Expand All @@ -129,12 +127,13 @@ function get_videos_by_tag_and_category(){
$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);
}
Expand All @@ -151,6 +150,8 @@ function fetch_request($request){
if ( $cache_status == 'HIT' ) {
$cached_response = $cache->get( $request );

// TODO: Stop using phpxml, switching to DOM/Simple for this
// TODO: Drop lib/phpxml if 0 uses in core/contrib
$xmlobj = XML_unserialize($cached_response);
return $this->render_video_list($xmlobj);
}
Expand All @@ -168,6 +169,8 @@ function fetch_request($request){
$cache->set($request, $response);
}

// TODO: Stop using phpxml, switching to DOM/Simple for this
// TODO: Drop lib/phpxml if 0 uses in core/contrib
$xmlobj = XML_unserialize($response);
return $this->render_video_list($xmlobj);
}
Expand All @@ -176,26 +179,89 @@ function render_video_list($xmlobj){

$text = '';
$text .= '<ul class="yt-video-entry unlist img-text">';
$videos = $xmlobj['ut_response']['video_list']['video'];
$videos = $xmlobj['feed']['entry'];

if (is_array($videos) ) {
foreach($videos as $video){
$url = s($video['media:group']['media:player attr']['url']);
$thumbnail = s($video['media:group']['media:thumbnail']['0 attr']['url']);
$title = s($video['media:group']['media:title']);
$seconds = $video['media:group']['yt:duration attr']['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";
}
} else {
} else {
// if youtube is offline, or for whatever reason the previous
// call doesn't work...
// call doesn't work...
//add_to_log(SITEID, 'blocks/tag_youtube', 'problem in getting videos off youtube');
}
$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;
}
}
}
?>
26 changes: 6 additions & 20 deletions blocks/tag_youtube/config_instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,16 @@
<tr valign="top">
<td align="right"><?php print_string('category', 'block_tag_youtube'); ?>:</td>
<td>
<?php
<?php
if (isset($this->config) && isset($this->config->category)) {
$selected = $this->config->category;
} else {
$selected = 0;
}
/* http://youtube.com/dev_api_ref?m=youtube.videos.list_by_category_and_tag */
$options = array ( 0 => get_string('anycategory', 'block_tag_youtube'),
1 => get_string('filmsanimation', 'block_tag_youtube'),
2 => get_string('autosvehicles', 'block_tag_youtube'),
23 => get_string('comedy', 'block_tag_youtube'),
24 => get_string('entertainment', 'block_tag_youtube'),
10 => get_string('music', 'block_tag_youtube'),
25 => get_string('newspolitics', 'block_tag_youtube'),
22 => get_string('peopleblogs', 'block_tag_youtube'),
15 => get_string('petsanimals', 'block_tag_youtube'),
26 => get_string('howtodiy', 'block_tag_youtube'),
17 => get_string('sports', 'block_tag_youtube'),
19 => get_string('travel', 'block_tag_youtube'),
20 => get_string('gadgetsgames', 'block_tag_youtube'),
);

choose_from_menu ($options, 'category', $selected);
?>
$categories = $this->get_categories();
$selected = $this->category_map_old2new($selected);
choose_from_menu ($categories, 'category', $selected);
?>
</td>
</tr>
<tr valign="top">
Expand All @@ -48,4 +34,4 @@
<td colspan="3" align="center">
<input type="submit" value="<?php print_string('savechanges') ?>" /></td>
</tr>
</table>
</table>
2 changes: 2 additions & 0 deletions lang/en_utf8/block_tag_youtube.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
$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';
Expand All @@ -18,5 +19,6 @@
$string['sports'] = 'Sports';
$string['travel'] = 'Travel &amp; Places';
$string['gadgetsgames'] = 'Gadgets &amp; Games';
$string['scienceandtech'] = 'Science &amp; Tech';

?>
2 changes: 1 addition & 1 deletion lib/phpxml/xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function XML(){
xml_set_character_data_handler($this->parser, 'data');
}
function destruct(){ xml_parser_free($this->parser); }
function & parse(&$data){
function parse(&$data){
$this->document = array();
$this->stack = array();
$this->parent = &$this->document;
Expand Down

0 comments on commit bc188d0

Please sign in to comment.