domingo, 10 de mayo de 2009

Bloque T2.9 Ejercicio 1 - Practica 5

Pues como cuando empezamos a hacer este ejercicio en clase estabamos también pendientes de la práctica 5, decidí matar dos pájaros de un tiro, así que en la práctica 5 los parámetros se pasan por la interfaz REST (el parámetro l -de longitud a mostrar- y el parámetro ind -indice- del RSS que queremos leer).

#!/usr/bin/perl

use CGI qw(:standard);
use XML::RSS;
use LWP::Simple qw(get);
use HTTP::Date;

#Definimos las URLs a las que vamos a aacceder.
my @urls=("", "http://www.animaadversa.es/rssfeed.xml", "http://feeds.feedburner.com/NoPuedoCreerQueLoHayanInventado", "http://ponlelaquesea.wordpress.com/feed/", "http://noticiasaudio.com/feed/", "http://feeds.feedburner.com/hispasonic");
#Chanchullo para poder acceder desde el indice 1.

#====== CREAR EL ARCHIVO CON LOS ITEMS RSS ORDENADOS =======
#Extraemos el parámetro de la URL.
my $mostrar = param('ind');
my $maximo = 5;
if(!$mostrar){
$mostrar = 1;
}else{
$maximo = $mostrar;
}

#Creamos un array con todos los items, que luego ordenaremos.
my $i=0;
my @arrayitem=([],[],[],[],[]);
for($indice=$mostrar; $indice<=$maximo; $indice++){
my $rdf = get($urls[$indice]);
my $rss = new XML::RSS;
$rss->parse($rdf);
$nombre= $rss->{'channel'}->{'title'};
$enlace= $rss->{'channel'}->{'link'};
foreach my $item ( @{ $rss->{items} } ){
$arrayitem[$i][0] = $$item{title};
$arrayitem[$i][1] = $$item{description};
$arrayitem[$i][2] = $$item{link};
$arrayitem[$i][3] = str2time($$item{pubDate});
$arrayitem[$i][4] = "<a href='$enlace'>$nombre</a>";
$i++;
}
}

#Y lo ordenamos por fecha.
@arrayitem = sort { $a->[3] cmp $b->[3] } @arrayitem;

#====== MOSTRAR LOS CANALES RSS ===========
#Creamos el archivo con todos los items que encontremos.
print header( -type => 'text/html' );
print "<html><head><title>Lector de RSS</title></head><body>";
#Damos formato a los nombres, fechas y demás y lo mostramos de más reciente a más lejano
#Extraemos el parámetro de longitud
my $longitud = param('l');
if(!$longitud){
$longitud=@arrayitem-1;
}
for ($i=@arrayitem-1; $i>=@arrayitem-$longitud; $i--){
print "<div class='noticia'><h2>".$arrayitem[$i][0]."</h2><h5>visto en ".$arrayitem[$i][4]."</h5><p>".$arrayitem[$i][1]."</p><h4>".time2str($arrayitem[$i][3])."</h4><p><a href='".$arrayitem[$i][2]."'>Ver mas</a></p></div>\n";
}

#Y aquí se debería mostrar
print "</body></html>";

No hay comentarios:

Publicar un comentario