This tutorial uses an old Youtube API, after google acquired Youtube a new API was released
In this brief tutorial i will show you how to show videos from youtube on you web page using PERL and Youtube API, the idea of the script is that it will show by default the favorites videos of some users, and you will be able to navigate to the tags you specify.
For this script we will use some libraries from the CPAN:
- CGI we will use to get the data send by GET
- Template toolkit This is an excellent template library
- XML:DOM this library will help us to parse the XML file that is returned by youtube
- LWP::UserAgent This is needed by XML:DOM to parse the files in remote locations
- Unicode::String We will use this to convert data to UTF8
Click here to see this script in action.
index.pl this is the perl script.
-
#!/usr/bin/perl
-
# you have to sign up to get a developer id
-
‘developerid’;
-
# we will show the favorites videos of this user as front page
-
‘someuser’;
-
# this is an array of the tags we will allow to navigate throw
-
‘tlaxcala’=>‘Tlaxcala’,
-
‘huamantla’=>‘Huamantla’,
-
‘huamantlada’=>‘Huamantlada’,
-
‘feria huamantla’=>‘Feria Huamantla’,
-
‘cacaxtla’=>‘Cacaxtla’,
-
‘apizaco’=>‘Apizaco’,
-
‘tlaxco’=>‘Tlaxco’,
-
‘feria tlaxcala’=>‘Feria Tlaxcala’‘action’"Content-type: text/html;charset=UTF-8\n\n"‘view’‘video’‘video’# here we start parsing the XML
-
‘http://www.youtube.com/api2_rest?method=youtube.videos.get_details&dev_id=FIq6LWxC3RE&video_id=’‘video’} );
-
# we put this on eval to avoid errors
-
# here i add all the commets to an array
-
‘comment’‘time’‘author’‘text’‘%r’‘%d-%m-%Y’, @date )
-
};
-
}
-
-
# here i format the video time
-
‘length_seconds’# here i create an array of the data i will use and show the page using template toolkit
-
‘video’‘title’‘author’‘rating_avg’‘tags’‘description’‘%02s:%02s:%02s’‘videos_view.tt’‘page’‘page’‘tag’‘tag’‘tag’” ) {
-
# i show the favorites of the user
-
$url=‘http://www.youtube.com/api2_rest?method=youtube.users.list_favorite_videos&dev_id=FIq6LWxC3RE&user=’‘page’}=‘1’‘page’”);
-
# here i get videos by tab
-
$url=‘http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag&dev_id=FIq6LWxC3RE&tag=’‘tag’} . ‘&page=’‘page’} . ‘&per_page=20’;
-
# since the api dosent support paging, i see if the next xml has data, to show next link
-
‘http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag&dev_id=FIq6LWxC3RE&tag=’‘tag’} . ‘&page=’‘page’} + 1 ) . ‘&per_page=20’# we put this on eval to avoid errors
-
# here i create an array with the info for videos
-
‘video’‘author’‘author’‘id’‘id’‘title’‘title’‘length_seconds’‘length_seconds’‘rating_avg’‘rating_avg’‘rating_count’‘rating_count’‘description’‘description’‘view_count’‘view_count’‘upload_time’‘upload_time’‘comment_count’‘comment_count’‘tags’‘tags’‘url’‘url’‘thumbnail_url’‘thumbnail_url’‘video’‘tag’‘page’}
-
);
-
# i show the page using template toolkit
-
‘videos_list.tt’
videos_list.tt The template where we see the list of videos.
-
-
"videosList"
-
[% FOREACH videos %]
-
-
"index.pl?action=view&video=[% id %]""[% thumbnail_url %]"
-
"index.pl?action=view&video=[% id %]"[% title %]
-
[% description %]
-
-
[% END %]
-
-
-
-
"center"
-
[% IF tag || ( page>1 && total>0 ) %]
-
[% IF ( page > 1 ) %]
-
<a href="index.pl?tag=[% tag %]&page=[% (page – 1) %]" alt="Previous" title="Previous">Previous
-
[% END %]
-
[% IF total>0 %]
-
"index.pl?tag=[% tag %]&page=[% (page + 1) %]""Next"‘Next’Next
-
[% END %]
-
-
-
-
[% END %]
-
-
[% IF tag == ” %]Favorites[% ELSE %]"index.pl"Favorites[% END %]
-
[% FOREACH key = tags.keys %]
-
|| [% IF tag == key %][% tags.$key %][% ELSE %]"index.pl?tag=[% key %]"[% tags.$key %][% END %]
-
[% END %]
-
-
videos_view.tt The template where we see a specific video.
-
-
[% title %]
-
-
"center"
-
"425""350""movie""http://www.youtube.com/v/[% video %]""wmode""transparent""http://www.youtube.com/v/[% video %]""application/x-shockwave-flash" wmode="transparent""425""350"
-
-
-
Author: "http://www.youtube.com/user/[% author %]""_blank"[% author %]
-
-
Rating: [% rating_avg %]
-
-
Tags: [% tags %]
-
-
Time: [% time %]
-
-
-
[% description %]
-
-
-
[% IF comments.size > 0 %]
-
Comments
-
[% FOREACH comment=comments %]
-
‘textoComentarios’[% comment.text %]
-
-
‘pieComentarios’
-
| By ‘http://www.youtube.com/user/[% comentario.author %]’[% comment.author %] | [% comment.date %] | [% comment.time %] |
-
-
-
-
[% END %]
-
[% END %]
-
-
Pingback: Perl Coding School » Blog Archive » perl tutorial [2008-04-02 07:43:22]