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