af5bc38736f031e0b837e16057271585a9e371b6
[catagits/App-IdiotBox.git] / lib / App / IdiotBox.pm
1 package App::IdiotBox;
2
3 use Web::Simple __PACKAGE__;
4 use FindBin;
5 use HTML::Zoom;
6 use HTML::Zoom::FilterBuilder::Template;
7 use List::Util qw(first);
8
9 {
10   package App::IdiotBox::Announcement;
11
12   sub id { shift->{id} }
13   sub made_at { shift->{made_at} } 
14   sub bucket { shift->{bucket} } 
15   sub video_count { shift->{video_count} } 
16
17   package App::IdiotBox::Bucket;
18
19   sub slug { shift->{slug} }
20   sub name { shift->{name} }
21   sub video_count {
22     exists $_[0]->{video_count}
23       ? $_[0]->{video_count}
24       : $_[0]->{videos}->count
25   }
26   sub videos { shift->{videos} }
27
28   package App::IdiotBox::Video;
29
30   sub slug { shift->{slug} }
31   sub name { shift->{name} }
32   sub author { shift->{author} }
33   sub details { shift->{details} }
34   sub bucket { shift->{bucket} }
35   sub file_name {
36     (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
37     $s;
38   }
39   sub url_path {
40     join('/', $_[0]->bucket->slug, $_[0]->slug);
41   }
42 }
43
44 has $_ => (is => 'ro') for qw(recent_announcements buckets);
45
46 sub default_config {
47   template_dir => 'share/html',
48   store => 'SQLite',
49   db_file => 'var/lib/idiotbox.db',
50   base_url => 'http://localhost:3000/',
51   base_dir => do { use FindBin; $FindBin::Bin },
52 }
53
54 sub BUILD {
55   my $self = shift;
56   my $store;
57   ($store = $self->config->{store}) =~ /^(\w+)$/
58     or die "Store config should be just a name, got ${store} instead";
59   my $store_class = "App::IdiotBox::Store::${store}";
60   eval "require ${store_class}; 1"
61     or die "Couldn't load ${store} store: $@";
62   $store_class->bind($self);
63 }
64   
65 sub dispatch_request {
66   my $self = shift;
67   sub (/) { $self->show_front_page },
68   sub (/*/...) {
69     my $bucket = $self->buckets->get({ slug => $_[1] });
70     sub (/) {
71       $self->show_bucket($bucket)
72     },
73     sub (/*/) {
74       $self->show_video($bucket->videos->get({ slug => $_[1] }));
75     }
76   }
77 }
78
79 sub show_front_page {
80   my $self = shift;
81   my $ann = $self->recent_announcements;
82   $self->html_response(
83     front_page => sub {
84       $_->select('#announcement-list')
85         ->repeat_content($ann->map(sub {
86             my $obj = $_;
87             sub {
88               $_->select('.bucket-name')->replace_content($obj->bucket->name)
89                 ->select('.made-at')->replace_content($obj->made_at)
90                 ->select('.bucket-link')->set_attribute({
91                     name => 'href', value => $obj->bucket->slug.'/'
92                   })
93                 ->select('.new-videos')->replace_content($obj->video_count)
94                 ->select('.total-videos')->replace_content(
95                     $obj->bucket->video_count
96                   )
97             }
98           }))
99     }
100   );
101 }
102
103 sub show_bucket {
104   my ($self, $bucket) = @_;
105   $self->html_response(bucket => sub {
106     $_->select('.bucket-name')->replace_content($bucket->name)
107       ->select('#video-list')->repeat_content($bucket->videos->map(sub {
108           my $video = $_;
109           sub {
110             $_->select('.video-name')->replace_content($video->name)
111               ->select('.video-author')->replace_content($video->author)
112               ->select('.video-link')->set_attribute(
113                   { name => 'href', value => $video->slug.'/' }
114                 )
115           }
116         }))
117   });
118 }
119
120 sub show_video {
121   my ($self, $video) = @_;
122   my $video_file = first {
123     -e join('/', $self->config->{base_dir}, $_)
124   } map {
125     join('/', $video->bucket->slug, $video->slug, $video->file_name.".$_")
126   } qw(flv m4v);
127   $self->html_response(video => sub {
128     my $video_url = 
129       $self->base_url
130       .($video_file||'NO FILE FOUND SORRY');
131
132     $_->select('.video-name')->replace_content($video->name)
133       ->select('.author-name')->replace_content($video->author)
134       ->select('.bucket-link')->set_attribute(
135           { name => 'href', value => '../' }
136         )
137       ->select('.bucket-name')->replace_content($video->bucket->name)
138       ->select('.video-details')->replace_content($video->details)
139       ->select('script')->template_text_raw({ video_url => $video_url });
140   });
141 }
142
143 sub html_response {
144   my ($self, $template_name, $selectors) = @_;
145   my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
146   return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
147 }
148
149 sub _template_filename_for {
150   my ($self, $name) = @_;
151   $self->{config}{template_dir}.'/'.$name.'.html';
152 }
153
154 sub _layout_zoom {
155   my $self = shift;
156   $self->{layout_zoom} ||= HTML::Zoom->from_file(
157     $self->_template_filename_for('layout')
158   )
159 }
160
161 sub _zoom_for {
162   my ($self, $template_name, $selectors) = @_;
163   ($self->{zoom_for_template}{$template_name} ||= do {
164     my @body;
165     HTML::Zoom->from_file(
166                   $self->_template_filename_for($template_name)
167                 )
168               ->select('#main-content')->collect_content({ into => \@body })
169               ->run;
170     $self->_layout_zoom
171          ->select('#main-content')->replace_content(\@body)
172          ->memoize;
173   })->apply($selectors);
174 }
175
176 sub base_url {
177   my $self = shift;
178   $self->{base_url} ||= do {
179     (my $u = $self->config->{base_url}) =~ s/\/$//;
180     "${u}/";
181   }
182 }
183
184 sub _run_cli {
185   my $self = shift;
186   unless (@ARGV == 1 && $ARGV[0] eq 'import') {
187     return $self->SUPER::_run_cli(@_);
188   }
189   $self->cli_import;
190 }
191
192 sub _cli_usage {
193   my $self = shift;
194   "To import data into your idiotbox install, chdir into a directory\n".
195   "containing video files and run:\n".
196   "\n".
197   "  $0 import\n".
198   "\n".
199   $self->SUPER::_cli_usage(@_);
200 }
201
202 sub cli_import {
203   my $self = shift;
204   require App::IdiotBox::Importer;
205   App::IdiotBox::Importer->run($self);
206 }
207
208 1;