Moodle

upgrade block tag_youtube to support new Google Data API

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.9
  • Fix Version/s: 1.9.8
  • Component/s: Blocks
  • Labels:
    None

Description

recently i noticed the the block tag_youtube does not show video thumbnails
it appears, Google no longer support the old RSS/XML format as described in:
http://code.google.com/apis/youtube/migration.html

so i read through the new API (version 2):
http://code.google.com/apis/gdata/docs/2.0/reference.html#Queries

and luckily, found IBM's implementation for it, too:
http://www.ibm.com/developerworks/xml/library/x-youtubeapi/
(thanks IBM, we love you)

and i have made a little patch to the moodle/blocks/tag_youtube/block_tag_youtube.php file

see comments with instructions of how to apply the patch

Issue Links

Activity

Hide
Nadav Kavalerchik added a comment -

open file moodle/blocks/tag_youtube/block_tag_youtube.php

inside function get_videos_by_tag , remark around lines 103-107 and add the unremarked code :

//         $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}";

//http://gdata.youtube.com/feeds/api/videos?vq=monkey&start-index=1&max-results=50

        $request = 'http://gdata.youtube.com/feeds/api/videos';
		$request .= "?vq={$querytag}";
        //$request .= '&dev_id='. YOUTUBE_DEV_KEY;
        $request .= "&start-index=1";
        $request .= "&max-results={$numberofvideos}";
		//$request .= "&alt=rss";

        return $this->render_video_list_xml($request);

now, add a new function :

function render_video_list_xml($feedURL){

        $text = '';
        $text .= '<ul class="yt-video-entry unlist img-text">';

		// read feed into SimpleXML object
		$sxml = simplexml_load_file($feedURL);

		// iterate over entries in feed
		foreach ($sxml->entry as $entry) {
		// get nodes in media: namespace for media information
		$media = $entry->children('http://search.yahoo.com/mrss/');

		// get video player URL
		$attrs = $media->group->player->attributes();
		$watch = $attrs['url'];

		// get video thumbnail
		$attrs = $media->group->thumbnail[0]->attributes();
		$thumbnail = $attrs['url'];

		// get <yt:duration> node for video length
		$yt = $media->children('http://gdata.youtube.com/schemas/2007');
		$attrs = $yt->duration->attributes();
		$length = $attrs['seconds'];

		// get <yt:stats> node for viewer statistics
		$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
		$attrs = $yt->statistics->attributes();
		$viewCount = $attrs['viewCount'];

		// get <gd:rating> node for video ratings
		$gd = $entry->children('http://schemas.google.com/g/2005');
		if ($gd->rating) {
			$attrs = $gd->rating->attributes();
			$rating = $attrs['average'];
		} else {
			$rating = 0;
		}
			$text .= '<li>';
			$text .= '<div class="item">';
			$text .= '<span class="title">';
			$text .= "<a href=\"$watch\">{$media->group->title}</a>";
			$text .= '</span>';
			$text .= "<p>{$media->group->description}</p>";
			$text .= "<a href=\"$watch\"><img src=\"$thumbnail\" /></a>";
			$text .= '</div></li>\n';
		}

		// if youtube is offline, or for whatever reason the previous
		// 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;
    }

and you are ready to go

Show
Nadav Kavalerchik added a comment - open file moodle/blocks/tag_youtube/block_tag_youtube.php inside function get_videos_by_tag , remark around lines 103-107 and add the unremarked code :
//         $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}";

//http://gdata.youtube.com/feeds/api/videos?vq=monkey&start-index=1&max-results=50

        $request = 'http://gdata.youtube.com/feeds/api/videos';
		$request .= "?vq={$querytag}";
        //$request .= '&dev_id='. YOUTUBE_DEV_KEY;
        $request .= "&start-index=1";
        $request .= "&max-results={$numberofvideos}";
		//$request .= "&alt=rss";

        return $this->render_video_list_xml($request);
now, add a new function :
function render_video_list_xml($feedURL){

        $text = '';
        $text .= '<ul class="yt-video-entry unlist img-text">';

		// read feed into SimpleXML object
		$sxml = simplexml_load_file($feedURL);

		// iterate over entries in feed
		foreach ($sxml->entry as $entry) {
		// get nodes in media: namespace for media information
		$media = $entry->children('http://search.yahoo.com/mrss/');

		// get video player URL
		$attrs = $media->group->player->attributes();
		$watch = $attrs['url'];

		// get video thumbnail
		$attrs = $media->group->thumbnail[0]->attributes();
		$thumbnail = $attrs['url'];

		// get <yt:duration> node for video length
		$yt = $media->children('http://gdata.youtube.com/schemas/2007');
		$attrs = $yt->duration->attributes();
		$length = $attrs['seconds'];

		// get <yt:stats> node for viewer statistics
		$yt = $entry->children('http://gdata.youtube.com/schemas/2007');
		$attrs = $yt->statistics->attributes();
		$viewCount = $attrs['viewCount'];

		// get <gd:rating> node for video ratings
		$gd = $entry->children('http://schemas.google.com/g/2005');
		if ($gd->rating) {
			$attrs = $gd->rating->attributes();
			$rating = $attrs['average'];
		} else {
			$rating = 0;
		}
			$text .= '<li>';
			$text .= '<div class="item">';
			$text .= '<span class="title">';
			$text .= "<a href=\"$watch\">{$media->group->title}</a>";
			$text .= '</span>';
			$text .= "<p>{$media->group->description}</p>";
			$text .= "<a href=\"$watch\"><img src=\"$thumbnail\" /></a>";
			$text .= '</div></li>\n';
		}

		// if youtube is offline, or for whatever reason the previous
		// 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;
    }
and you are ready to go
Hide
Eloy Lafuente (stronk7) added a comment -

Hi Nadav,

thanks for the complete report!

In Moodle 1.9.x we cannot rely in SimpleXML to do the parsing work, so I'm going to perform the migration to the new API using the current parser. Anyway, in 2.0, we are already using SimpleXML (but against the old API so I'll migrate that too).

Ciao

Show
Eloy Lafuente (stronk7) added a comment - Hi Nadav, thanks for the complete report! In Moodle 1.9.x we cannot rely in SimpleXML to do the parsing work, so I'm going to perform the migration to the new API using the current parser. Anyway, in 2.0, we are already using SimpleXML (but against the old API so I'll migrate that too). Ciao
Hide
Eloy Lafuente (stronk7) added a comment -

Changes to 1.9 committed. Everything seems to work ok:

  • search by tag
  • seach by category
  • list from playlist
  • new category codes

Plz, review... ciao

Show
Eloy Lafuente (stronk7) added a comment - Changes to 1.9 committed. Everything seems to work ok:
  • search by tag
  • seach by category
  • list from playlist
  • new category codes
Plz, review... ciao
Hide
Eloy Lafuente (stronk7) added a comment -

Done, now HEAD is also using the new API (parsed by SimpleXML).

Thanks Nadav. Resolving this a fixed... ciao

Show
Eloy Lafuente (stronk7) added a comment - Done, now HEAD is also using the new API (parsed by SimpleXML). Thanks Nadav. Resolving this a fixed... ciao
Hide
Helen Foster added a comment -

Eloy, thanks for fixing this issue

I tested the YouTube block in the latest 1.9.7+ and video thumbnails were displayed fine. (I didn't manage to test in HEAD as I couldn't get blocks to display on the tags page!)

Show
Helen Foster added a comment - Eloy, thanks for fixing this issue I tested the YouTube block in the latest 1.9.7+ and video thumbnails were displayed fine. (I didn't manage to test in HEAD as I couldn't get blocks to display on the tags page!)
Hide
Eloy Lafuente (stronk7) added a comment -

Yeah, I had to use one temporal "trick" here in order to display the block in the user profile page, in order to be able to check it was working in HEAD.

Thanks!, ciao

Show
Eloy Lafuente (stronk7) added a comment - Yeah, I had to use one temporal "trick" here in order to display the block in the user profile page, in order to be able to check it was working in HEAD. Thanks!, ciao

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: