support m4v as well as flv
[catagits/App-IdiotBox.git] / lib / App / IdiotBox.pm
index 4a11aca..2b79dee 100644 (file)
@@ -4,10 +4,13 @@ use Web::Simple __PACKAGE__;
 use Method::Signatures::Simple;
 use FindBin;
 use HTML::Zoom;
+use HTML::Zoom::FilterBuilder::Template;
+use List::Util qw(first);
 
 {
   package App::IdiotBox::Announcement;
 
+  sub id { shift->{id} }
   sub made_at { shift->{made_at} } 
   sub bucket { shift->{bucket} } 
   sub video_count { shift->{video_count} } 
@@ -30,12 +33,21 @@ use HTML::Zoom;
   sub author { shift->{author} }
   sub details { shift->{details} }
   sub bucket { shift->{bucket} }
+  sub file_name {
+    (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
+    $s;
+  }
+  sub url_path {
+    join('/', $_[0]->bucket->slug, $_[0]->slug);
+  }
 }
 
 default_config(
   template_dir => 'share/html',
   store => 'SQLite',
   db_file => 'var/lib/idiotbox.db',
+  base_url => 'http://localhost:3000/',
+  base_dir => do { use FindBin; $FindBin::Bin },
 );
 
 sub BUILD {
@@ -77,6 +89,7 @@ method show_front_page {
             my $obj = $_;
             sub {
               $_->select('.bucket-name')->replace_content($obj->bucket->name)
+                ->select('.made-at')->replace_content($obj->made_at)
                 ->select('.bucket-link')->set_attribute({
                     name => 'href', value => $obj->bucket->slug.'/'
                   })
@@ -107,7 +120,16 @@ method show_bucket ($bucket) {
 }
 
 method show_video ($video) {
+  my $video_file = first {
+    -e join('/', $self->config->{base_dir}, $_)
+  } map {
+    join('/', $video->bucket->slug, $video->slug, $video->file_name.".$_")
+  } qw(flv m4v);
   $self->html_response(video => sub {
+    my $video_url = 
+      $self->base_url
+      .$video_file;
+
     $_->select('.video-name')->replace_content($video->name)
       ->select('.author-name')->replace_content($video->author)
       ->select('.bucket-link')->set_attribute(
@@ -115,6 +137,7 @@ method show_video ($video) {
         )
       ->select('.bucket-name')->replace_content($video->bucket->name)
       ->select('.video-details')->replace_content($video->details)
+      ->select('script')->template_text_raw({ video_url => $video_url });
   });
 }
 
@@ -147,4 +170,32 @@ method _zoom_for ($template_name, $selectors) {
   })->apply($selectors);
 }
 
+method base_url {
+  $self->{base_url} ||= do {
+    (my $u = $self->config->{base_url}) =~ s/\/$//;
+    "${u}/";
+  }
+}
+
+method _run_cli {
+  unless (@ARGV == 1 && $ARGV[0] eq 'import') {
+    return $self->SUPER::_run_cli(@_);
+  }
+  $self->cli_import;
+}
+
+method _cli_usage {
+  "To import data into your idiotbox install, chdir into a directory\n".
+  "containing video files and run:\n".
+  "\n".
+  "  $0 import\n".
+  "\n".
+  $self->SUPER::_cli_usage(@_);
+}
+
+method cli_import {
+  require App::IdiotBox::Importer;
+  App::IdiotBox::Importer->run($self);
+}
+
 1;