3 use App::IdiotBox::Common qw(@SupportedFormats);
4 use Web::Simple __PACKAGE__;
7 use HTML::Zoom::FilterBuilder::Template;
8 use List::Util qw(first);
11 package App::IdiotBox::Announcement;
13 sub id { shift->{id} }
14 sub made_at { shift->{made_at} }
15 sub bucket { shift->{bucket} }
16 sub video_count { shift->{video_count} }
18 package App::IdiotBox::Bucket;
20 sub slug { shift->{slug} }
21 sub name { shift->{name} }
23 exists $_[0]->{video_count}
24 ? $_[0]->{video_count}
25 : $_[0]->{videos}->count
27 sub videos { shift->{videos} }
29 package App::IdiotBox::Video;
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} }
37 (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
41 join('/', $_[0]->bucket->slug, $_[0]->slug);
45 has $_ => (is => 'ro') for qw(recent_announcements buckets);
48 template_dir => 'share/html',
50 db_file => 'var/lib/idiotbox.db',
51 base_url => 'http://localhost:3000/',
52 base_dir => do { use FindBin; $FindBin::Bin },
58 ($store = $self->config->{store}) =~ /^(\w+)$/
59 or die "Store config should be just a name, got ${store} instead";
60 my $store_class = "App::IdiotBox::Store::${store}";
61 eval "require ${store_class}; 1"
62 or die "Couldn't load ${store} store: $@";
63 $store_class->bind($self);
66 sub dispatch_request {
68 sub (/) { $self->show_front_page },
70 my $bucket = $self->buckets->get({ slug => $_[1] });
72 $self->show_bucket($bucket)
75 $self->show_video($bucket->videos->get({ slug => $_[1] }));
82 my $ann = $self->recent_announcements;
85 $_->select('#announcement-list')
86 ->repeat_content($ann->map(sub {
89 $_->select('.bucket-name')->replace_content($obj->bucket->name)
90 ->select('.made-at')->replace_content($obj->made_at)
91 ->select('.bucket-link')->set_attribute(
92 'href' => $obj->bucket->slug.'/'
94 ->select('.new-videos')->replace_content($obj->video_count)
95 ->select('.total-videos')->replace_content(
96 $obj->bucket->video_count
105 my ($self, $bucket) = @_;
106 $self->html_response(bucket => sub {
107 $_->select('.bucket-name')->replace_content($bucket->name)
108 ->select('#video-list')->repeat_content($bucket->videos->map(sub {
111 $_->select('.video-name')->replace_content($video->name)
112 ->select('.video-author')->replace_content($video->author)
113 ->select('.video-link')->set_attribute(
114 href => $video->slug.'/'
122 my ($self, $video) = @_;
123 my $video_file = first {
124 -e join('/', $self->config->{base_dir}, $_)
126 join('/', $video->bucket->slug, $video->slug, $video->file_name.".$_")
128 $self->html_response(video => sub {
131 .($video_file||'NO FILE FOUND SORRY');
133 $_->select('.video-name')->replace_content($video->name)
134 ->select('.author-name')->replace_content($video->author)
135 ->select('.bucket-link')->set_attribute(
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 });
145 my ($self, $template_name, $selectors) = @_;
146 my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
147 return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
150 sub _template_filename_for {
151 my ($self, $name) = @_;
152 $self->{config}{template_dir}.'/'.$name.'.html';
157 $self->{layout_zoom} ||= HTML::Zoom->from_file(
158 $self->_template_filename_for('layout')
163 my ($self, $template_name, $selectors) = @_;
164 ($self->{zoom_for_template}{$template_name} ||= do {
166 HTML::Zoom->from_file(
167 $self->_template_filename_for($template_name)
169 ->select('#main-content')->collect_content({ into => \@body })
172 ->select('#main-content')->replace_content(\@body)
174 })->apply($selectors);
179 $self->{base_url} ||= do {
180 (my $u = $self->config->{base_url}) =~ s/\/$//;
187 unless (@ARGV == 1 && $ARGV[0] eq 'import') {
188 return $self->SUPER::_run_cli(@_);
195 "To import data into your idiotbox install, chdir into a directory\n".
196 "containing video files and run:\n".
200 $self->SUPER::_cli_usage(@_);
205 require App::IdiotBox::Importer;
206 App::IdiotBox::Importer->run($self);