move to new DBIxDS code
[catagits/App-IdiotBox.git] / lib / App / IdiotBox.pm
CommitLineData
e30ed59d 1package App::IdiotBox;
2
3use Web::Simple __PACKAGE__;
4use Method::Signatures::Simple;
d7497a23 5use FindBin;
6use HTML::Zoom;
998cc52c 7use HTML::Zoom::FilterBuilder::Template;
02ea620e 8use List::Util qw(first);
d7497a23 9
10{
11 package App::IdiotBox::Announcement;
12
ebba317f 13 sub id { shift->{id} }
d7497a23 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} }
d9702c6d 22 sub video_count {
23 exists $_[0]->{video_count}
24 ? $_[0]->{video_count}
25 : $_[0]->{videos}->count
26 }
71fd1550 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} }
998cc52c 36 sub file_name {
37 (my $s = join(' ', @{+shift}{qw(author name)})) =~ s/ /-/g;
38 $s;
39 }
ebba317f 40 sub url_path {
41 join('/', $_[0]->bucket->slug, $_[0]->slug);
42 }
d7497a23 43}
44
45default_config(
71a02d85 46 template_dir => 'share/html',
47 store => 'SQLite',
48 db_file => 'var/lib/idiotbox.db',
998cc52c 49 base_url => 'http://localhost:3000/',
ebba317f 50 base_dir => do { use FindBin; $FindBin::Bin },
d7497a23 51);
e30ed59d 52
71a02d85 53sub 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
e30ed59d 64dispatch {
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 },
71a02d85 72 sub (/*/) {
d7497a23 73 $self->show_video($bucket->videos->get({ slug => $_[1] }));
e30ed59d 74 }
75 ]
76 }
77};
78
d7497a23 79method recent_announcements { $self->{recent_announcements} }
80
71a02d85 81method buckets { $self->{buckets} }
82
e30ed59d 83method show_front_page {
84 my $ann = $self->recent_announcements;
85 $self->html_response(
1a1c4f78 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)
02ea620e 92 ->select('.made-at')->replace_content($obj->made_at)
1a1c4f78 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 }
e30ed59d 103 );
104}
105
d7497a23 106method show_bucket ($bucket) {
1a1c4f78 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 });
d7497a23 120}
121
122method show_video ($video) {
02ea620e 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);
1a1c4f78 128 $self->html_response(video => sub {
998cc52c 129 my $video_url =
130 $self->base_url
6df05090 131 .($video_file||'NO FILE FOUND SORRY');
998cc52c 132
1a1c4f78 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)
998cc52c 140 ->select('script')->template_text_raw({ video_url => $video_url });
1a1c4f78 141 });
d7497a23 142}
143
e30ed59d 144method html_response ($template_name, $selectors) {
1a1c4f78 145 my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
e30ed59d 146 return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
147}
148
d7497a23 149method _template_filename_for ($name) {
150 $self->{config}{template_dir}.'/'.$name.'.html';
151}
152
e30ed59d 153method _layout_zoom {
d7497a23 154 $self->{layout_zoom} ||= HTML::Zoom->from_file(
155 $self->_template_filename_for('layout')
e30ed59d 156 )
157}
158
159method _zoom_for ($template_name, $selectors) {
160 ($self->{zoom_for_template}{$template_name} ||= do {
161 my @body;
d7497a23 162 HTML::Zoom->from_file(
163 $self->_template_filename_for($template_name)
e30ed59d 164 )
1a1c4f78 165 ->select('#main-content')->collect_content({ into => \@body })
d7497a23 166 ->run;
1a1c4f78 167 $self->_layout_zoom
168 ->select('#main-content')->replace_content(\@body)
169 ->memoize;
170 })->apply($selectors);
e30ed59d 171}
172
998cc52c 173method base_url {
174 $self->{base_url} ||= do {
175 (my $u = $self->config->{base_url}) =~ s/\/$//;
176 "${u}/";
177 }
178}
179
ebba317f 180method _run_cli {
181 unless (@ARGV == 1 && $ARGV[0] eq 'import') {
182 return $self->SUPER::_run_cli(@_);
183 }
184 $self->cli_import;
185}
186
187method _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
196method cli_import {
197 require App::IdiotBox::Importer;
198 App::IdiotBox::Importer->run($self);
199}
200
e30ed59d 2011;