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