3 use Web::Simple __PACKAGE__;
4 use Method::Signatures::Simple;
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);
46 template_dir => 'share/html',
48 db_file => 'var/lib/idiotbox.db',
49 base_url => 'http://localhost:3000/',
50 base_dir => do { use FindBin; $FindBin::Bin },
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);
65 sub (/) { $self->show_front_page },
66 subdispatch sub (/*/...) {
67 my $bucket = $self->buckets->get({ slug => $_[1] });
70 $self->show_bucket($bucket)
73 $self->show_video($bucket->videos->get({ slug => $_[1] }));
79 method recent_announcements { $self->{recent_announcements} }
81 method buckets { $self->{buckets} }
83 method show_front_page {
84 my $ann = $self->recent_announcements;
87 $_->select('#announcement-list')
88 ->repeat_content($ann->map(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.'/'
96 ->select('.new-videos')->replace_content($obj->video_count)
97 ->select('.total-videos')->replace_content(
98 $obj->bucket->video_count
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 {
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.'/' }
122 method show_video ($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(
136 { name => 'href', value => '../' }
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 });
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 ]
149 method _template_filename_for ($name) {
150 $self->{config}{template_dir}.'/'.$name.'.html';
153 method _layout_zoom {
154 $self->{layout_zoom} ||= HTML::Zoom->from_file(
155 $self->_template_filename_for('layout')
159 method _zoom_for ($template_name, $selectors) {
160 ($self->{zoom_for_template}{$template_name} ||= do {
162 HTML::Zoom->from_file(
163 $self->_template_filename_for($template_name)
165 ->select('#main-content')->collect_content({ into => \@body })
168 ->select('#main-content')->replace_content(\@body)
170 })->apply($selectors);
174 $self->{base_url} ||= do {
175 (my $u = $self->config->{base_url}) =~ s/\/$//;
181 unless (@ARGV == 1 && $ARGV[0] eq 'import') {
182 return $self->SUPER::_run_cli(@_);
188 "To import data into your idiotbox install, chdir into a directory\n".
189 "containing video files and run:\n".
193 $self->SUPER::_cli_usage(@_);
197 require App::IdiotBox::Importer;
198 App::IdiotBox::Importer->run($self);