Sunday, June 28, 2009

Youtube channel embedding being all borked up?

Update: Google/YouTube seem to have taken the option to generate gadgets showing all the videos from a given user offline, and as such the script below doesn't work anymore. As a workaround I've posted a small script which generates the old embed code for an user-specified YouTube user.

In a previous post I wanted to embed two youtube channel gadgets, but after pasting the official code, I kept getting the following error message: "The URL is not valid and cannot be loaded."

After looking into it a little, it seems at one step the following code is generated: <iframe src="http://?... (the domain is missing). After searching around a little, I came up with the following Perl script which generates (what seems to be) the correct embed code:

use strict;
use warnings;
use LWP::Simple;

my $youtubeCN = get(shift @ARGV);
die("Script tag not found!\n")
 unless $youtubeCN =~ /&lt;script src=&quot;(.*?)&quot;&gt;/;
my $youtubeScr = $1;
$youtubeScr =~ s/&amp;/&/g;
$youtubeScr =~ s/&amp;/&/g;
my $gmodScr = get($youtubeScr);

$gmodScr =~ s/http:\/\/\?container/http:\/\/www.gmodules.com\/ig\/ifr\?/;
print "<script type=\"text/javascript\">$gmodScr</script>\n"; 

It needs one parameter, the URL of the youtube channel: perl bork_youtube_channel.pl 'http://www.youtube.com/user/pennsays' and it will spit out the code you need to insert. Warning! This is an unofficial method and has the drawback that it may break later on when YouTube changes their embed model. You can download the latest version of the script from my SVN repo.

PS: it seems that playing videos with the channel gadget is more permissive than directly on Youtube, in the sense that I didn't get a single "this can not be played due to age/location restriction" message.

PS2: Does anybody have an idea why do I have to do the replacement twice? When I do it only one, it doesn't seem to "stick". :-(

Update: so I'm an idiot, the URL was double-encoded for some reason...

No comments:

Post a Comment