#!/usr/bin/perl
use CGI;
use Template;
use XML::DOM;
use LWP::UserAgent;
use Unicode::String;
# you have to sign up to get a developer id
my $devId = ‘developerid’;
# we will show the favorites videos of this user as front page
my $userFavorites = ‘someuser’;
# this is an array of the tags we will allow to navigate throw
my %tags = (
‘tlaxcala’=>‘Tlaxcala’,
‘huamantla’=>‘Huamantla’,
‘huamantlada’=>‘Huamantlada’,
‘feria huamantla’=>‘Feria Huamantla’,
‘cacaxtla’=>‘Cacaxtla’,
‘apizaco’=>‘Apizaco’,
‘tlaxco’=>‘Tlaxco’,
‘feria tlaxcala’=>‘Feria Tlaxcala’
);
my $query = new CGI;
my $action = $query->param(‘action’);
my @videos = ();
my $total = 0;
my $values = {};
my $url;
my $tt = Template->new;
print "Content-type: text/html;charset=UTF-8\n\n";
SWITCH:{
if( $action eq ‘view’ ) {
$values{‘video’}=$query->param(‘video’);
my @comments = ();
# here we start parsing the XML
my $parser = XML::DOM::Parser->new();
my $doc = $parser->parsefile(‘http://www.youtube.com/api2_rest?method=youtube.videos.get_details&dev_id=FIq6LWxC3RE&video_id=’ . $values{‘video’} );
# we put this on eval to avoid errors
# here i add all the commets to an array
foreach my $comment ($doc->getElementsByTagName(‘comment’)){
my @date =
localtime( $comment->
getElementsByTagName(‘time’)->
item(0)->
getFirstChild->
getNodeValue );
author=>Unicode::String::utf8($comment->getElementsByTagName(‘author’)->item(0)->getFirstChild->getNodeValue),
text=>Unicode::String::utf8($comment->getElementsByTagName(‘text’)->item(0)->getFirstChild->getNodeValue),
time=>strftime
( ‘%r’,
@date ),
date=>strftime( ‘%d-%m-%Y’, @date )
};
}
# here i format the video time
my( $seconds,$hours,$minutes );
$seconds = $doc->getElementsByTagName(‘length_seconds’)->item(0)->getFirstChild->getNodeValue;
( $hours,
$seconds ) =
( int( $seconds/
3600 ),
$seconds %
3600 );
( $minutes,
$seconds ) =
( int( $seconds/
60 ),
$seconds %
60 );
# here i create an array of the data i will use and show the page using template toolkit
my %data = (
video=>$values{‘video’},
title=>Unicode::String::utf8($doc->getElementsByTagName(‘title’)->item(0)->getFirstChild->getNodeValue),
author=>Unicode::String::utf8($doc->getElementsByTagName(‘author’)->item(0)->getFirstChild->getNodeValue),
rating_avg=>$doc->getElementsByTagName(‘rating_avg’)->item(0)->getFirstChild->getNodeValue,
tags=>Unicode::String::utf8($doc->getElementsByTagName(‘tags’)->item(0)->getFirstChild->getNodeValue),
description=>Unicode::String::utf8($doc->getElementsByTagName(‘description’)->item(0)->getFirstChild->getNodeValue),
time=>sprintf
( ‘%02s:%02s:%02s’,
$hours,
$minutes,
$seconds),
comments=>\@comments
);
$tt->
process( ‘videos_view.tt’, \
%data ) ||
die $tt->
error;
};
last SWITCH;
}
$values{‘page’}=$query->param(‘page’);
$values{‘tag’}=$query->param(‘tag’);
if( $values{‘tag’} eq ” ) {
# i show the favorites of the user
$url=‘http://www.youtube.com/api2_rest?method=youtube.users.list_favorite_videos&dev_id=FIq6LWxC3RE&user=’ . $userFavorites;
} else {
$values{‘page’}=’1′ if( $values{‘page’} eq ”);
# here i get videos by tab
$url=‘http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag&dev_id=FIq6LWxC3RE&tag=’ . $values{‘tag’} . ‘&page=’ . $values{‘page’} . ‘&per_page=20′;
# since the api dosent support paging, i see if the next xml has data, to show next link
$nextXML=‘http://www.youtube.com/api2_rest?method=youtube.videos.list_by_tag&dev_id=FIq6LWxC3RE&tag=’ . $values{‘tag’} . ‘&page=’ . ( $values{‘page’} + 1 ) . ‘&per_page=20′;
}
my $parser = XML::DOM::Parser->new();
# we put this on eval to avoid errors
my $doc = $parser->parsefile($url);
# here i create an array with the info for videos
foreach $video ($doc->getElementsByTagName(‘video’)){
‘author’=>Unicode::String::utf8($video->getElementsByTagName(‘author’)->item(0)->getFirstChild->getNodeValue),
‘id’=>$video->getElementsByTagName(‘id’)->item(0)->getFirstChild->getNodeValue,
‘title’=>Unicode::String::utf8($video->getElementsByTagName(‘title’)->item(0)->getFirstChild->getNodeValue),
‘length_seconds’=>$video->getElementsByTagName(‘length_seconds’)->item(0)->getFirstChild->getNodeValue,
‘rating_avg’=>$video->getElementsByTagName(‘rating_avg’)->item(0)->getFirstChild->getNodeValue,
‘rating_count’=>$video->getElementsByTagName(‘rating_count’)->item(0)->getFirstChild->getNodeValue,
‘description’=>Unicode::String::utf8($video->getElementsByTagName(‘description’)->item(0)->getFirstChild->getNodeValue),
‘view_count’=>$video->getElementsByTagName(‘view_count’)->item(0)->getFirstChild->getNodeValue,
‘upload_time’=>$video->getElementsByTagName(‘upload_time’)->item(0)->getFirstChild->getNodeValue,
‘comment_count’=>$video->getElementsByTagName(‘comment_count’)->item(0)->getFirstChild->getNodeValue,
‘tags’=>Unicode::String::utf8($video->getElementsByTagName(‘tags’)->item(0)->getFirstChild->getNodeValue),
‘url’=>$video->getElementsByTagName(‘url’)->item(0)->getFirstChild->getNodeValue,
‘thumbnail_url’=>$video->getElementsByTagName(‘thumbnail_url’)->item(0)->getFirstChild->getNodeValue
};
}
my $doc2 = $parser->parsefile($nextXML);
$total = @{ $doc2->getElementsByTagName(‘video’) };
}
};
my %data = (
total=>$total,
tag=>$values{‘tag’},
tags=>\%tags,
videos=>\@videos,
page=>$values{‘page’}
);
# i show the page using template toolkit
$tt->
process( ‘videos_list.tt’, \
%data ) ||
die $tt->
error;
}