#!/usr/bin/perl -w # Author: Jarle Bjørgeengen # Date: 2009-03-01 # License: GNU General Public license # Inngansportal til Frikanalen sine videoer i theora format # # http://wiki.nuug.no/grupper/video/pubfrikanalen use strict; use warnings; use XML::Simple; use CGI qw/:standard/; # SOAP:Lite må modifiseres til å gjøre ting på MS måten :-/ use SOAP::Lite on_action => sub {sprintf '%s/%s', @_}, ; use Data::Dumper; use Date::Parse; use POSIX qw(strftime); use Encode; use URI::Escape; my $query = new CGI; my $nuug_frikanalen_url = 'http://www.nuug.no/pub/video/frikanalen/'; my $scripturl = url(); my $category = $query->param("category"); my $cat = $category ? "category=$category" : ""; my $organization = $query->param("organization"); my $org_escaped = uri_escape($organization); my $org = $organization ? "organization=$org_escaped" : ""; my $editor = $query->param("editor"); my $ed_escaped = uri_escape($editor); my $ed = $editor ? "editor=$ed_escaped" : ""; my $sort = $query->param("sort"); my $sor = $sort ? "sort=$sort" : ""; my $searchtype = $sort ? "$sort" : "MostRecent"; my $page = $query->param("page") ; if ( ! $page ) { $page = 0; } my $video_count = 0; my $videos_per_page = 20; my $offset = $page ? $page * $videos_per_page : 0; my $rss = $query->param("rss"); my $metainfo = "/home/ftp/pub/video/frikanalen/meta.xml"; my $ref = XMLin($metainfo); my $videodir = '/data/video/frikanalen'; binmode STDOUT, ":utf8"; if (defined $rss) { generate_rss(); exit 0; } else { &printheader; &printbody; &printfooter; exit 0; } ########################################### sub get_categories { # Returnerer referanse med "array of hashrefs". Hver hashref inneholder variablene 'Name' og 'Id'. # Kun 'Name' er brukt. my $soap = new SOAP::Lite -> uri('http://localhost/CommunitySiteService') -> proxy('http://communitysite1.frikanalen.tv/CommunitySiteFacade/CommunitySiteService.asmx'); my $res; my $obj = $soap->SearchCategories(SOAP::Data->name('searcher' => { } ) ); unless ($obj->fault) { my @categories; $res = $obj->result; #print Dumper($res); #foreach my $category (@{$res->{'Data'}->{'Category'}}) { # print "$category->{Name} $category->{Id}\n"; #} return $res->{'Data'}->{'Category'}; } else { print join ', ', $res->faultcode, $res->faultstring; } } sub get_organizations { my $soap = new SOAP::Lite -> uri('http://localhost/CommunitySiteService') -> proxy('http://communitysite1.frikanalen.tv/CommunitySiteFacade/CommunitySiteService.asmx'); my $res; my $soap_response = $soap->GetOrganizations; unless ($soap_response->fault) { my $res = $soap_response->result; return $res->{'Data'}->{'string'}; } else { print join ', ', $soap_response->faultcode, $soap_response->faultstring; } } sub get_editors { my $soap = new SOAP::Lite -> uri('http://localhost/CommunitySiteService') -> proxy('http://communitysite1.frikanalen.tv/CommunitySiteFacade/CommunitySiteService.asmx'); my $res; my $soap_response = $soap->GetEditors; unless ($soap_response->fault) { my $res = $soap_response->result; return $res->{'Data'}->{'string'}; } else { print join ', ', $soap_response->faultcode, $soap_response->faultstring; } } sub searchvids { # Returnerer referanse med "array of hashrefs". Hashref inneholder metadata og urler # til videoer. Bruk Dumper til å titte på. my $returndata ; my $res; my $obj; my $soap = new SOAP::Lite -> uri('http://localhost/CommunitySiteService') -> proxy('http://communitysite1.frikanalen.tv/CommunitySiteFacade/CommunitySiteService.asmx'); if ($category) { $obj = $soap->SearchVideos( SOAP::Data->name('searcher' => { 'PredefinedSearchType' => $searchtype, 'CategoryName' => $category, 'Take' => 10000, } ) ); } elsif ($organization) { $organization = SOAP::Data->type(string => encode("utf8",$organization)); $obj = $soap->SearchVideos( SOAP::Data->name('searcher' => { 'PredefinedSearchType' => $searchtype, 'Organization' => $organization, 'Take' => 10000, } ) ); } elsif ($editor) { $editor = SOAP::Data->type(string => encode("utf8",$editor)); $obj = $soap->SearchVideos( SOAP::Data->name('searcher' => { 'PredefinedSearchType' => $searchtype, 'Editor' => $editor, 'Take' => 10000, } ) ); } else { $obj = $soap->SearchVideos( SOAP::Data->name('searcher' => { 'PredefinedSearchType' => $searchtype, 'Take' => 10000, } ) ); } unless ($obj->fault) { $res = $obj->result; if ( ref($res->{'Data'}->{'Video'}) eq 'HASH') { return [ $res->{'Data'}->{'Video'} ]; } return $res->{'Data'}->{'Video'}; } else { print join ', ', $res->faultcode, $res->faultstring; } } sub printheader { # Skriver ut toppen av forsiden + høyremeny med kategorier, # sorteringstyper og link til rss tilbake til # megselv?category=$category;sort=¤sort my $menu; if ($organization) { $menu = "org"; } elsif ($editor) { $menu = "ed"; } else { $menu = "cat"; } my $arg = join(";", $sor,$cat,$org,$ed); my $categories = &get_categories; my $organizations = &get_organizations; my $editors = &get_editors; my %searchtypes = ("Nyeste", "MostRecent", "Tittel", "OrderByTitle", "Topp vurderte", "TopRated", "Mest sett", "MostViewed", "Mest diskutert", "MostDiscussed"); my $search; print "Content-type: text/html; charset=UTF-8\n\n"; # Page header print < Frikanalen - med åpne standarder EOF print ""; print <
Frikanalen

TV FOR ALLE
Dette er en alternativ presentasjon av filmene som sendes på Frikanalen i Norge. Denne alternative presentasjonen benytter åpne standarder og er laget av NUUGs videogruppe.

OM FRIKANALEN LAGE TV PÅ FRIKANALEN SE PÅ FRIKANALEN
EOF # Kategorier print "

 Kategorier

"; print ""; my $all_cat = $category ? "
  • Alle
  • " : "
    • \">\" Alle
    • "; print "$all_cat"; foreach my $cat (@{$categories}) { if (defined $category && $cat->{'Name'} eq $category) { print "
    • \">\" $cat->{'Name'}
    • "; } else { print "
    • {'Name'};$sor\" >$cat->{'Name'}
    • "; } } print "
"; # Organisasjoner print "

 Organisasjoner

"; print ""; my $all_org = $organization ? "
  • Alle
  • " : "
    • \">\" Alle
    • "; print "$all_org"; foreach (@{$organizations}) { $org_escaped = uri_escape($_); if (defined $organization && $_ eq $organization){ print "
    • \">\" $_
    • "; } else { print "
    • $_
    • "; } } print "
"; # Redaktører print "

 Redaktører

"; print ""; my $all_ed = $editor ? "
  • Alle
  • " : "
    • \">\" Alle
    • "; print "$all_ed"; foreach (@{$editors}) { $ed_escaped = uri_escape($_); if (defined $editor && $_ eq $editor) { print "
    • \">\" $_
    • "; } else { print "
    • $_
    • "; } } print "
"; # Sorteringer print " 

SORTER

"; print "
    "; foreach $search (sort keys %searchtypes) { if ($searchtypes{$search} eq $searchtype) { print "
  • \">\" $search
  • "; } else { print "
  • $search
  • "; } } # Link til rss print <

    RSS for Miro

EOF } sub printbody { # Returnerer kroppen til htmltabellen. Innholdet er avhengig av $category my $videos = &searchvids($category,$organization,$editor); $video_count = (@{$videos}); # print Dumper($videos); print "
\n"; print "
    \n"; # Bruk paging. GM. 01.06.09 # foreach my $video (@{$videos}) { for (my $x = 0; $x < $videos_per_page; $x++) { my $video = (@{$videos}[$x+$offset]); my $title = $video->{'Title'}; my $id = $video->{'MetaDataVideoId'}; # Use local thumb JB. 10.03.09 my $imageuri = './thumbs/'.$id.'.jpg'; # ...hvis lokal thumb finnes. GM. 08.06.09 unless (-e $imageuri) { $imageuri = $video->{'ImageUri'}; } my $ogvurl = "$nuug_frikanalen_url$id.ogv"; my $description = $video->{'Description'}; my $length = $video->{'MetaData'}->{'Length'}; my $seconds = ($length%60); my $minutes = int(($length%3600)/60); my $hours = int($length/3600); my $videouri = "${nuug_frikanalen_url}fetchvideo.cgi\?videoId=$id"; my $ogvfile = "$id.ogv"; my $num = $id; $num =~ s/^id_//; my $uploaddate = $video->{'Details'}->{'UploadDate'}; if (-e "$videodir/$ogvfile") { print "
  • "; print "\"thumbnail\"$title\n"; if ($hours) { print "\ (Lengde: ".(sprintf('%2dt %2dm',$hours,$minutes)).')
    '; } else { print "\ (Lengde: ".(sprintf('%2dm %2ds', $minutes, $seconds)).')
    '; } print "Publisert: $uploaddate
    \n" if $uploaddate; print "$description\n" if $description; print "
  • \n"; } } print "
\n"; print "
\n"; } sub printfooter { # List argumenter for sortering, kategorier, organisasjon og redaktør my $arg = join(";", $sor,$cat,$org,$ed); # Hvis mer enn en side med videoer, skriv ut pager-links if ($video_count > $videos_per_page) { my $pagenum = (int(($video_count-1) / $videos_per_page)+1); my $previous = $page - 1; my $current = $page; my $next = $page + 1; print "
\n"; print "

 

\n"; if ($current > 0) { print "Forrige "; } if ($current == 0) { print "      1 "; } else { print "      1 "; } for (my $x = 1; $x < $pagenum; $x++) { my $p = $x + 1; if ($x == $current) { print "$p "; } else { print "$p "; } } print "     "; unless ($next == $pagenum) { print " Neste\n"; } print "
\n"; } # Skriv ut footer print <  
© 2009 Foreningen Frikanalen, design Wild Web
EOF } sub videosort { my $res = $b->{'Details'}->{'UploadDate'} cmp $a->{'Details'}->{'UploadDate'}; $res = $b->{'MetaDataVideoId'} <=> $a->{'MetaDataVideoId'} if 0 == $res; return $res; } sub generate_rss { print_rss_header(); my $videos = &searchvids($category,$organization,$editor); foreach my $video ( sort videosort @{$videos} ) { my $id = $video->{'MetaDataVideoId'}; my $videouri = "${nuug_frikanalen_url}fetchvideo.cgi\?videoId=$id"; my $ogvfile = "$id.ogv"; my $num = $id; $num =~ s/^id_//; # print STDERR Dumper($video); my $oggfilepath = "$videodir/$ogvfile"; my $date = format_rss_date($video->{'Details'}->{'UploadDate'}); if ( -e $oggfilepath ) { my $size = (stat($oggfilepath))[7]; item($video->{'Title'}, $date, $videouri, $video->{'Description'}, "$nuug_frikanalen_url$id.ogv", "application/ogg", $size, $video->{'ImageUri'}, int($video->{'MetaData'}->{'Length'})); } } print_rss_footer(); } sub format_rss_date { my ($isodate) = @_; $isodate =~ s/^(\d{4})(\d{2})(\d{2})/$1-$2-$3/; my $time = str2time($isodate); return strftime("%a, %d %b %Y %H:%M:%S %z", gmtime($time)); } sub item { my ($title, $date, $link, $desc, $enclurl, $encltype, $enclsize, $thumb, $duration) = @_; $enclurl = $link unless defined $enclurl; $title = escapeHTML($title); $desc = escapeHTML($desc); print < $title EOF print " $link\n" if $link; print " $link\n" if $link; print " $desc\n" if $desc; print " $date\n" if $date; print < EOF print " $duration\n" if ($duration); print < EOF } sub print_rss_header { my $selfurl = self_url(); my $cat = defined $category ? $category : ""; print < Frikanalen $cat http://www.frikanalen.no/ Frikanalen recordings. (c) nb Frikanalen Collected recordings Broadcasted on the public TV channel. Frikanalen post\@frikanalen.no Frikanalen, $cat Frikanalen $cat http://www.frikanalen.no/images/frikanal_logo_sv_bakgrunn_f.gif http://www.frikanalen.no/ no EOF } sub print_rss_footer { print < EOF }